Learn about the core components of CSS2.1 and how they can be used to present your content.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Example Document</title> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <link rel="stylesheet" href="sample.css" type="text/css" media="all" /> </head> <body> <div> <p>Getting ahead in the <em>fast</em> paced world of tortoise racing.</p> </div> </body> </html>
From the CSS2.1 Specification:
"A type selector matches the name of a document language element type. A type selector matches every instance of the element type in the document tree."
Example of a Type selector:
h1 { ... }
Custom selector that matches any element with a matching class attribute, or any specific element with a matching class attribute.
Examples of Class selectors:
.myClass { ... }
div.myClass { ... }
An ID is unique to one element in an HTML/XHTML document. ID selectors are used to match an element with a matching ID attribute. Can also match a specific element with a matching ID.
Examples of ID selectors:
#myID { ... }
div#myID { ... }
If an ID can only be used once, why would I ever need use the second selector?
A Descendant selector is used to match an element that is a descendant of a defined ancestor element.
Example of a Descendant selector:
p a { ... }
This would match any a element that is a descendant of a p element.
Used intelligently, descendant selectors can relieve your HTML documents of class-itis.
The universal selector (represented by an asterisk "*") is used to match any element.
Example of the universal selector:
* { color: red; }
The above rule would make the foreground (text) color of every element in an HTML document red.
Some examples of complex selectors which incorporate one or more selector types:
html, body { margin: 0; padding: 0; }#header h1 { color: #336699; }#navigation ul li.first a { text-decoration: none; }div#reportItem p.sampleItem a:hover { color: red; }h1[title] {} or h1[title=Sample Title] {}h1 + p {}div > p {}p:first-line {} or a:hover {}Style rules can come from any or all of these sources:
Weight is determined by importance (normal or important) and origin (author, user, or user agent):
(most important to least important)
Create a final value by concatenating A, B, and C
#home div p a {}
A-B-C -> 1-0-3 = 103
body#home .content p a {}
A-B-C -> 1-1-3 = 113
There are many ways to say the same thing:
div {
border-top-width: 1px;
border-right-width: 1px;
border-bottom-width: 1px;
border-left-width: 1px;
border-top-style: solid;
border-right-style: solid;
border-bottom-style: solid;
border-left-style: solid;
border-top-color: #000000;
border-right-color: #000000;
border-bottom-color: #000000;
border-left-color: #000000;
}
Could also be written as:
div {
border: 1px solid #000;
}
For more information see Efficient CSS with Shorthand Properties by Roger Johansson.