HTML5, CSS3, and JavaScript Fourth Edition

(Ben Green) #1

CHAPTER 34. UNTANGLING HTML AND STYLE 289


directly in the HTML markup. It is better to separate it out.)


How can we best keep the CSS separate from the HTML?


34.1 Rules and Exceptions


The concept of CSS is to allow style to be established at many levels. We
start with a general rule. Then we make exceptions. Then we make excep-
tions to the exceptions.


Our modern calendar (the Gregorian) works that way. The general rule is
that each year has 365 days. The exception is leap years, which happen
every 4 years. Then the year has 366 days. BUT, every 100 years, we skip
a leap year. We are back to 365 days. BUT BUT, every 400 years, it is leap
year anyway.


With our calendar, we have a rule, and an exception, and an exception to
the exception, and an exception to the exception to the exception. Why
not?


Styles also have rules and exceptions.


You can have an overall style, say maybe black text, at the document level.
That would apply to all text in the entire document.


But you might want to break your own rule on a certain paragraph.


You can have a style, say maybe red text, at the paragraph level. It would
apply to all the text in that paragraph, even if the document specified black
text.


CSS is built on the idea that the same attribute, like text color, may be
specified several different ways. CSS looks at all the requests and decides
which request gets priority.


34.2 The Cascade


In-line beats internal. Internal beats external.


The most general solution to styling a website is to create a single style sheet
and apply it to all the webpages in the website.


This is called an external style sheet.

Free download pdf