Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

internal .css code scope

 
David Mutansan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my html file, I first import some external css file ("file1.css"). Suppose the external css has a defined section called ".div_part1, .div_part2, .div_part3 { color: black; ....}" Then in this html, I define an internal section <style> ...  </style> after the external import  and within this <style> sector I override ".div_part1, .div_part2, .div_part3 {color: red; ....}"

Based on basic css rule, on this html the "color" part for " .div_part1, .div_part2, .div_part3" will take "red" since it is overriden.  Now, the question is ---

If I have 2nd html file, it also imports the same "file1.css".  In this html, it does not define anything to override the ".div_part1, .div_part2, .div_part3" stuff.   Then can I be confident whatever defined in the <style> section in the first html is only scoped to the first html file itself, it will never to accessible from the 2nd html file ?  in other words, in the 2nd html, when it needs ".div_part1, .div_part2, .div_part3 " color, it will still be black, right ?
 
Munnou Rao
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can be confident that the styling in the <style> section of the first HTML file will not affect the second HTML file.

Here’s why:

CSS rules defined in a <style> section inside an HTML file are only scoped to that specific HTML document. They apply to the elements of that page alone and won’t have any impact on other pages unless those pages import the same styles. So in your case, since the second HTML file doesn’t have any overriding internal CSS for .div_part1, .div_part2, .div_part3, it will continue to use the styles from the external CSS (file1.css), meaning the color for those classes will be black, as defined in the external file.
 
reply
    Bookmark Topic Watch Topic
  • New Topic