Css Presentation
Css Presentation
Mark Branom
IT Services Technology Training
650.725.1717
markb@stanford.edu
http://www.stanford.edu/people/markb/
This handout accompanies classroom instruction provided by the IT Services Technology Training group at Stanford University.
While it is not intended as a stand-alone tutorial, it may be helpful for review of materials taught in the course.
To properly see the effects of the Style Sheets, your visitors will need to use a web
browser that is version 4.0 or newer. Fortunately, using CSS does not cause web
pages to break when viewed on older browsers; however, the styles won’t appear
as defined. Since most people these days are using Internet Explorer 6, Netscape
7, or Firefox 1 or newer, most browsers can properly display CSS.
CSS-aware browsers apply their own stylesheet for every HTML element as the
first set of rules in the cascade, and this set of rules form the default display for
every element. For example, most browsers treat the <p> tag as a block element,
as though there were the explicit declaration p { display: block;} By using
CSS, you modify the default settings by overriding these implicit styles with an
explicit declaration (for more on the block display, see slide 17).
By using CSS, you can also control text formatting and location on the page.
Using CSS can eliminate the need for tables as a layout tool. With CSS, logos can
be created using just text, instead of having to rely on graphics. These changes
make pages more accessible to a wider audience.
CSS Specifications:
• CSS 1: http://www.w3.org/TR/REC-CSS1-961217.html
• CSS 2: http://www.w3.org/TR/CSS2/
• CSS 2.1: http://www.w3.org/TR/CSS21/
Differences between CSS 1, CSS 2, and CSS 2.1:
• Between CSS 1 and CSS 2: http://www.w3.org/TR/CSS2/changes.html
• Between CSS 2 and CSS 2.1: http://www.w3.org/TR/CSS21/changes.html
Cons
• Different browsers may interpret Style Sheets in different ways
• Some styles may not be seen at all on some browsers
Additionally, multiple selectors that have the same styles can be grouped by separating them with commas:
h1, h2, h3 { color: darkblue; font-style: italic;}
Contextual selectors allow you to specify that something will change, but only when it is used in conjunction
with something else. With the following style, strong will be displayed in red, but only when it occurs
within li within ul.
ul li strong { color: red;}
Elements being modified by contextual selectors need not appear immediately inside one another (using
this style, blah would still be red text: <ul><ol><li><strong> blah </strong></li></ol></ul>).
Direct child selectors allow you to specify that something will change, but only those that are immediately
inside of another element. With the following style, only those strong elements that are directly inside of
an h1 will be purple; no strong tags deeper within the sheet will be purple.
h1 > strong { color: purple;}
Adjacent selectors allow you to specify that something will change, but only when preceded by something
else. With the following style, only those links (a) that are preceded by an h2 will be green.
h2 + a { color: green;}
Elements being modified by adjacent selectors appear immediately after one another. Using this style, this
link would be green: <h2>Visit Stanford!</h2><a href="http://www.stanford.edu">click here</a>.
This link would not: <h2>Visit Stanford! <a href="http://www.stanford.edu">click here</a></h2>.
You can also group selectors by attribute. With the following style, centered h2 tags (<h2 align="center">)
will be surrounded by a dotted border:
h2[align="center"] { border: dotted;}
HTML document, using the <link> tag method HTML document, using the @import and @media method
<head> <head>
</style>
</head>
Note: !important does not work with all properties in Internet Explorer.