CSS is the way we tell a browser how to display our XHTML document. We write a list of rules describing the colour of the text, the size of the boxes— what goes where and what it looks like.
To write a CSS document, conceptually, we take the structure of an HTML document and lay it out so that:
Becomes...
body
{ property:value; }
#header
{ property:value; }
#header h1
{ property:value; }
ul.navigation
{ property:value; }
#content
{ property:value; }
#content p
{ property:value; }
#footer
{ property:value; }
HTML pages are like tupperware on a shelf: boxes nested inside each other. Everything nested inside the body tag inherits the properties and values assigned to body, unless you say different. Everything inside #container inherits the properties of body and #container, and so on. CSS, then, cascades down the page, sort of like champagne filling a pyramid of glasses.
Before we go any further, let's explore some terms:
Jane (sex: female; age: 30; status: single; background: archeology;)
Becomes
#content{ background: #fff; color: #000; border: 1px dotted #666;}
To be complete, every property must have a value defined and vice versa. This property+value pair goes {inside the curly brackets} and each pair is separated by a semi-colon, just like complete clauses in a sentence.
#selector { property:value; }
ul.navigation { property:value; }
table { property:value; }
Looking again at the example HTML document, see that the selector in a CSS declaration is actually a path. You can select every paragraph everywhere:
p { color:red; }
Or you can specify only certain paragraphs, in certain places, by writing out the path:
p { color: black; }
body #content p { color:red; }
You can read more about specificity later. For now, know that CSS is basically making pretty boxes and arranging them on a page. It uses a very clear jargon that's almost English, so it's easy to understand and to intuit further.
On our archive, we use hex codes to define colours. Hex codes are a set of six letters (a-f) and digits (0-9) that make up a colour. #000000 is black. #ffffff is white. Everything else is somewhere in between.
We use ems as the general unit of measurement. 1 em is 100% of your font size; if your font size is 16px, 1 em is 16px.
1 em square:
Em is a scalable measurement, so if your visitor needs to zoom in or scale up the text, your layout is less likely to go madly wrong. Em the measurement is different from the HTML tag <em>, which means emphasis. Learn about our "Em Scale".
HTML elements come in two flavours: block-level and inline.
Elements like <a href="http://...">links</a> and <em>emphasis</em> run along in a line, like text does.
Elements are either block or inline by default,* but in CSS we can decide how we want to display every single element on the screen. So we can make the list items <li> of an unordered list <ul>, which is a block containing a stack of blocks, into a line of elements very easily, by making the declaration: ul li {display:inline;}
Think of each #selector as a box. We can make it stack up, give it a background colour (or an image or both), a border, and we can give that border a colour and a thickness and a style. We can decide how big each box is and much blank space is going to be around it (margin) and inside it (padding). We can position (float, top, left) each box on the page. We can make the text in the box a particular size, colour, font, and make it bold or italic or asscaps; we can align it to the left or the right or centre it. We can do anything! (Almost anything.)
So CSS is a short profile of how your page is presented:
a.otw { background: #900; color:#fff; border:2px solid #000; font: 100 small-caps 1.5em 'gill sans',sans-serif; text-decoration:none; padding:0.15em }