![]() | |
|
The basics
|
Style Sheets — The BasicsThis page only scrapes the surface of what you can do with style sheets. If you are interested in learning more, see Designing for Web Standards by Jeffrey Zeldman, or CSS Pocket Reference by Eric A Meyer. IntroductionCascading Style Sheets (CSS) are a Web standard used to control the visual appearance of content on your Web pages. Style sheets allow you to control the look of elements on your Web pages across your whole Web site, instead of defining elements one at a time. They make your pages leaner, cleaner, and easier to maintain. For example, imagine you want to change the way the H2 headers are displayed. Without style sheets, you would add code to each H2 tag on your pages, like this: <h2><font face="Georgia, Times New Roman, Times, serif" size="+1" With style sheets, you can control the look of all the H2 tags in your Web site with one simple rule like this:
h2 {font-family: Georgia, "Times New Roman", serif;
font-size: 125%;
color: red;
padding-bottom: 0px;}
The style sheet is maintained separately from your Web page. So on your Web page, all you will see is: <h2>This is My Header</h2> Much simpler, yes? And if you ever want to change your H2 headers—make them purple instead of red, for instance—you can do that in one place—your style sheet—instead of finding and changing each H2 tag on your Web site. Adding styles to Web pagesThere are three ways styles can be added to your Web page: Inline stylesInline styles can be applied to individual HTML tags within your Web page. This can be a good method to use if you are applying a different style to just one element on your site, but it should not be used across the whole of your site, since it is not much of an improvement over the old way of doing things. <h1 style="color: red; font-family: Georgia, Times, serif; Embedded stylesA style sheet can be embedded into the
<html>
<head>
<style type="text/css">
h1 {color: red; font-size: 150%; }
p {margin-bottom: 10px; }
</style>
</head>
...
</html>
External style sheetsFor most average-sized Web sites, the best way to add styles to your Web pages
is to maintain them in a separate text document and then reference them from
your HTML page. There are two ways to do this. This first way creates a link
to the style sheet in the <head> <link rel="stylesheet" type="text/css" href="mystylesheet.css"> </head> The second way is to import an external style sheet using the <head> <style type="text/css"> @import url(mystylesheet.css) </style> </head> NOTE: Style sheets that are imported cannot be seen by older Web browsers, like Netscape 4.0. This may actually come in handy, if there are styles that you want newer browsers to see but older browsers to ignore. For much more on this "Two-Sheet Method" see Jeffrey Zeldman's article on A List Apart. Applying styles to sections of your Web pages
In all of the examples above, we show how to apply styles to an element across your whole Web site. But what if you only want your H2 headers to be red if they appear in the main body of your page, but not in a sidebar, for example? Style sheets allow you to define sections of your Web site, so that the style will only be applied to that element if it appears in that section.
|
![]() |
|