The Three Ways to Style Your DocumentThere are three ways to add CSS to your Web pages, inline, embedded, and linked from a separate CSS style sheet. The only one that really makes any sense in terms of developing Web sites is to link your XHTML pages to a CSS style sheet, but we will examine the other two as well, as they can be useful while developing your pages. A style sheet is an entirely separate file from your XHTML file, and contains only CSS. A style sheet can be shared by an infinite number of XHTML pages, which helps ensure a consistent look from page to page and allows edits made to a style to be instantly reflected across an entire site. Inline StylesInline styles (also known as local styles) are added to a tag using the XHTML style attribute, like this <p>This paragraph simply takes on the browser's default paragraph style.</p> <p style="font-size: 25pt; font-weight:bold; font-style:italic; color:red;">By adding inline CSS styling to this paragraph, we can override the default styles.</p> <p>And now we are back to a regular default paragraph without any inline styles.</p> which looks like this Figure 2.1. Inline styles are only applied to the tag to which they are attached.Here are some things you need to know about inline styles:
Embedded StylesYou can place a group of CSS styles in the head of your XHTML documentthese are known as embedded styles (or page styles). The terms "embedded styles" and "page styles" are often confused as the former is the official name and the latter is the more commonly used namebut they are synonymous. Embedded styles work like this <head> <title>Inline Styles example</title> <meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Language" content="en-us" /> <style type="text/css"> /* <![CDATA[ */ h1 { font-size: 16pt; font-weight:bold;} p {color:blue;} /* ]]> */ </style> </head> a comment */
The style tag tells the browser it is about to encounter code other than (X)HTML; the tag's attribute states that the code is CSS. (If you want to include JavaScript in the head of your document instead of CSS, use a style tag with the "text/javascript" attribute). Here are some comments on embedded (or page) styles:
Linked StylesYou can place styles in a separate document (a style sheet) that links to multiple pages so that the styles have global (site-wide) scopethe styles defined in this style sheet can affect every page of your site, not just a single page or a single tag. This is the only method of the three that truly separates the presentational styles from the structural markup. If you centralize all your CSS styles in a style sheet in this way, Web site design and editing becomes much easier. For example, if you need to make changes that affect the whole site ("The client wants all the body text to be blue, not black."), doing so is as quick and painless as modifying one CSS style. This is certainly much easier than the pre-CSS task of modifying every font attribute of every paragraph tag in every page of the site. You can link your style sheet to as many XHTML pages as you wish with a single line of code in the head of each XHTML page: <link href="my_style_sheet.css" media="screen" rel="stylesheet" type="text/css" /> Then the styles are applied to each page's markup as the page loads. Note that, in the above link tag, the media attribute is defined as "screen", meaning the style sheet is designed for the screen, which currently means Web browsers. (Certain user agents look for particular media attributes that best suit their display capabilities; possibilities here include: all, projection, handheld, print, aural. See a full list on the W3 Schools site (www.w3schools.com/css/css_mediatypes.asp). Any browser that loads the page uses the style sheet the link tag indicates. But by adding a second link tag with the media attribute of "print", you can offer a second style sheet that the browser will use when printing. A style sheet for printing might hide navigation and other elements that don't make sense when the content goes to paper. If you create a second style sheet for printing, its link tag might look like this <link href="my_style_sheet_print.css" media="print" rel="stylesheet" type="text/css" /> So now that you know what style sheets are, let's look at how you write style sheet rules, and how concepts like Inheritance, Specificity, and The Cascade control how these rules affect your markup.
|