* Presentation information for an entire website or collection of pages can be held in one CSS file, allowing sweeping changes to be propagated with quick changes to this one file. * Different users can have different style sheets: for example a large text alternative for visually-impaired users, or a layout optimized for small displays for mobile phones. * The document code is reduced in size and complexity, since it does not need to contain any presentational markup.
[edit] Syntax
CSS has a simple syntax, and uses a number of English keywords to specify the names of various style properties.
A style sheet consists of a list of rules. Each rule or rule-set consists of one or more selectors and a declaration block. A declaration-block consists of a list of semicolon-separated declarations in curly braces. Each declaration itself consists of a property, a colon (:) then a value.[1]
In this example are seven rules with selectors p, h2, .note, p.warning, #paragraph1, a:hover and the space-separated pair of selectors #news p. After each selector are the declarations such as color: red;, where the property color is given the value red.
In the first two rules, the HTML elements p (paragraph) and h2 (level-two heading) are being assigned stylistic attributes. The paragraph element will be rendered in Garamond font or, if Garamond is unavailable, some other serif font. The level-two heading element will be rendered in red on a white background.
The third rule shown matches those elements that have a class attribute containing the token 'note'. For example:
This paragraph will be rendered in red and bold, with a yellow background.
The fourth rule shown matches those p elements that have a class attribute containing the token 'warning'. This is in contrast to the third rule which matched all elements, not just paragraph tags, that are marked with a given class attribute (the third rule .note could also have been written as *.note).
In fact, .class selectors involve a special kind of attribute matching, as opposed to testing for equality. Since the HTML class attribute is defined as a whitespace-separated list of tokens, a class selector is evaluated against each of them separately. For example,
would apply both the note and the warning rule.
The fifth rule will match the one and only element in each HTML document whose id attribute is set to paragraph1: It will have no margin within its rendering 'box' as its margin width is set to zero. For example:
This paragraph will have no margin within its rendering 'box' as its margin width is set to zero. There can only be one of these paragraphs per page
The sixth rule defines the hover style for a (anchor) elements. By default in most browsers, a elements are underlined. This rule will remove the underline when the user "hovers" the mouse cursor over these elements.
The last rule matches those p elements that are within the document element whose id attribute has the value news. It is an example of a descendant selector.
Because of the cascading nature of CSS rules, these last p elements will inherit font-family: "Garamond", serif as with all p elements. Those that have the token warning in the value of their class attribute, will inherit that background image too.
A CSS stylesheet can contain comments. Comments are placed between the delimiters /* and */. For example /* This a CSS comment */ Comments can run over multiple lines.
[edit] Selectors & Pseudo Classes
In CSS, selectors are used to declare which elements a style applies to, a kind of match expression. Here are some examples of selector syntax:
All elements that is, using the * selector By element name e.g. for all p or h2 elements Descendants e.g. for a elements that are descendants of li elements (e.g links inside lists) the selector is li a class or id attributes e.g. .class and/or #id for elements with class="class" or id="id" Adjacent elements e.g. for all p elements preceded by h2 elements, the selector would be h2 + p Direct child element e.g. for all span elements inside p, but no span elements deeper within the hierarchy, the selector would be p > span By attribute e.g. for all the selector would be input[type="text"]
In addition to these, a set of pseudo-classes can be used to define further behavior. Probably the best-known of these is :hover, which applies a style only when the user 'points to' the visible element, usually by holding the mouse cursor over it. It is appended to a selector as in a:hover or #elementid:hover. Other pseudo-classes and pseudo-elements are, for example, :first-line, :visited or :before. A special pseudo-class is :lang(c), where the style would be applied on an element only if it is in language "c".
A pseudo-class selects entire elements, such as :link or :visited, whereas a pseudo-element makes a selection that may consist of partial elements, such as :first-line or :first-letter.
Selectors may be combined in other ways too, especially in CSS 2.1, to achieve greater specificity and flexibility (see the complete definition of selectors at the W3C Web site).
In the following example, several selectors are grouped using comma separation[1]. The rule sets the font for all HTML headings.
To use a CSS stylesheet, one would save the CSS code in a file such as example.css and then either link to it or import it from HTML or XHTML web pages using one of the two following formats:
In the first example, note that the /> syntax only applies in XHTML; if writing HTML, one would close an empty element such as this with >.
CSS styles may also be specified in the tag or attached to a specific element via the style attribute.
In applying a CSS stylesheet to an XML document, this format would be used as per the XML example below:
Cascading Style Sheets (CSS) are a collection of formatting rules that control the appearance of content in a web page. They are very useful for maintaining a web site since its appearance (controlled by properties of HTML tags) can be managed from just one file. CSS Styles also enhance your site's look, accessibility and reduces file size. Another main advantage is reusability - instead of defining the properties of fonts, backgrounds, borders, bullets, uniform tags, etc. each time you use them you can just assign the corresponding css style in the class property. You can store CSS styles directly in each document or, for more control and flexibility, in an external style sheet.
There are 3 types of CSS Styles:
Custom CSS (Class) styles: create a customized style with the set attributes. These class styles can be applied to any tag.
HTML Tag styles: redefine the formatting for a particular tag, such as
. All text formatted with the h1 tag is immediately updated.
Advanced CSS Selector styles: redefine the formatting for:
A particular combination of tags (for example, td h2 applies whenever an h2 header appears inside a table cell) and pseudo-class styles (for example, a:link, a:hover, a:visited)
A specific ID attribute (for example, #myStyle applies to all tags that contain the attribute-value pair id="myStyle")
How it works
CSS styles are defined within the
If you define the style in a external existing or new Style Sheet(recommended) you will find code similar to the following in your head content:
This code indicates that the document is linking to the external style sheet called styles.css located under the site root. If you open the style sheet in notepad you will find code similar to the code for the styles being embedded in the document. The big advantage of external style sheets is that the web site is easy to maintain and that when we make changes to the styles applied in the document, only the style sheet needs to be uploaded and not all the pages accessing it. As a practice, while using Macromedia Dreamweaver, link you template(s) to the external style sheet so that all the pages using the template can use just one style sheet.
Creating a New Style Sheet and a new CSS Style
Open your site in Dreamweaver and do the following:
Open any page of your site and select window > CSS Styles and Click the New CSS Style button (+ icon), a pop-up window will show up.
Select Class in the Selector Type. Now type your style name in the Name box (example: .bullet). Use the Tag Selector Type for redefining HTML tags or the Advanced Selector Type for hovers, id attributes or combination of tags.
Define the style in a New Style Sheet, give a name (example: styles.css) and save it under your site root. Click OK.
In the CSS Style Definition pop-up window that appears give the Formatting Attributes like Font, List, Background, Border, Positioning, etc.
Applying the New Style
To use this style just select and right click any tag in the tag selector located at the bottom left of the Document window. For the Set Class attribute select bullet from the list.
While typing the HTML code use styles by setting the class property for any tag (example: ).