= Basic CSS Reference = This is a quick 'cheat sheet' for CSS (Cascading Style Sheets). == Selectors == * **selector**: The selector defines which piece of the document will be selected (e.g. to select all ''

'' tags with a class of ''foobar'', you'd use ''p.foobar {}'') * **declaration**: a combination of properties and values that declare something about a selector (e.g. ''p.foobar { color: red; }'' ). The 'declaration block' is the set of all declarations for a given selector. * Basic selectors allow you to select all elements with a certain HTML tag (e.g. to get all '''' tags: ''em {}''), all elements with a specific class (e.g. ''.myClass {}''), or a single element tagged with an ''id'' attribute (e.g. ''#myElement {}''). Of course, you can mix and match; ''em.myClass {}'' would only get '''' elements with ''myClass'' (e.g. '''') * You can apply multiple class selectors in a single element selector. For example, ''.hello.there {}'' would match ONLY elements with ''class='there hello' '' (in any order). This also means that period isn't valid in a classname. * You can also create **attribute selectors**, e.g ''p[class]'' would select all ''

'' elements with a class attribute. This can be useful for selecting various types of input elements, e.g. ''input[type="radio"] {}'' or ''input[type="checkbox"] {}'' * You can create **descendant selectors**, these basically ask for a child element that is a descendant of another, e.g. ''h1 em {}'' would get all elements where an '''' tag is inside an ''

'' tag. The '''' tag can be any number of levels down; if you want to select an '''' //only// if it is the //immediate// child of an ''

'' tag, you'd use ''h1 > em {}''. * There are **pseudo classes** you can use to select things based on actions that have been taken in the document. ''a:visited {}'' is one good example; it lets you select all visited links. You can also combine these with normal classes, e.g. ''a.foo:visited {}''. Other pseudo classes include ''hover'', ''active'' and ''link''. == Sizing == * CSS has many methods for sizing objects. ''em'' is the height of the current font (as defined by font-size), ''ex'' is the height of a lowercase x (although generally is just 1/2 the font size), ''px'' is pixels. There are also absolute measurements, such as ''in'', ''cm'', ''mm'', ''pt'' (72/inch), etc. * It's generally best to use relative measurements... pixels when those are necessary (they scale to a 96dpi 'reference pixel' for printing) and ems for other measurements, esp. those that should grow or shrink. Percentages are also good, but fixed measurements such as points, centimeters or inches are not. * That's because monitors are all set to different dpi settings, and so using an absolute measurement will change the number of pixels in a given measurement. For example, if you say ''margin: 1in'' and the monitor is set to 120dpi, then you'll get a 120 pixel margin. But if your monitor is set to 96dpi, then you'll get a 96 pixel margin. == Text == * The ''font-size'' declaration lets you control the size of fonts. It is generally best to do these in either pixels or percentages (e.g. ''font-size: 12px;''). Don't forget that percentages inherit, though (e.g. 80% * 80% = 64%). * ''font-weight'' lets you control the boldness of a font. You can do this with numbers (100 to 900 in 100 increment values), but nobody does. Instead, use ''normal'' and ''bold'' or //maybe// ''bolder'' and ''lighter''. For example: ''font-weight: bold;'' * ''font-style'' can be either ''italic'' or ''oblique'' (or normal), but ''italic'' is what you want. Your browser will pull an oblique face if it can't find an italic face, but not the other way around. ''font-variant'' can be used to do cool things, like small-caps. ''text-transform'' lets you force text to all caps, to lowercase, etc. ''text-decoration'' handles junk like strikethrough; the most common use of ''text-decoration'' is ''text-decoration: none'' on an '''' element, which removes the underline from a hyperlink. * There is an overall ''font'' declaration that lets you specify multiple properties at once. It can also take a few specific keywords: ''caption'', ''icon'', ''menu'', ''message-box'', ''small-caption'', ''status-bar''. These actually represent an entire set of font properties (''font-family'', ''font-style'', ''color'', ''font-weight'', etc.) which make the font look like the system font in question. This can be useful if you're building web-apps or the like. * ''text-indent'' lets you both indent and outdent of the first line of text. * Horizontal and vertical alignment can be done with the ''text-align'', ''line-height'' and ''vertical-align'' declarations. Note that line-height only specifies //minimum// values * ''letter-spacing'' and ''word-spacing'' adjust their namesakes. They can be handy from time to time. * ''white-space'' can be used to adjust the fact that browsers normally collapse multiple spaces into one. You can use 'pre' which is like HTML's
 tag, or 'nowrap', which doesn't wrap things. And there are a few others.

== Opacity ==

 * You can set the transparency of an element with the opacity property
== Block Elements & The Box Model ==

 * CSS layouts are fundamentally based on the **CSS Box Model**, which lets you create 'boxes' (of text, empty space, graphics, etc.) on the page, and then control how they are positioned and sized. Pretty much every element on a page is affected by the box model.
 * Any given box has a content area, padding (space inside the border), a border, and then margins (space outside the border). **Width and height generally go from the inner edge** of the box, not the outer edge. So if you make a box with ''width: 100px; padding: 10px;'', the total width of the box on the page will be 120px (100px width + 10px left side padding + 10px right side padding). 
 * Any given element in an HTML document can be either 'block level' or 'inline'. Block elements are generally used for layouts, and have a linebreak both before and after the element unless otherwise specified. Inline elements are...well... inline in the content, and do not have said linebreak. 
 * ''display'' changes elements to block or inline. Therefore, if you wanted to change a ''
'' element to be inline, you'd say ''
'' Note if you change an element, you change its display property only, and not the nature of the element. This means (for example) that it doesn't become syntactically valid to wrap an
tag in an tag, even if you made the
an inline element. * ''display:none;'' completely removes the element from the flow of the document. This feature is used //all the time// in modern Web 2.0 apps to hide controls, popup boxes and other elements that should only appear once the user has interacted with the page in some way. * ''visibility: hidden;'' has a similar effect to ''display: none,'' but is a little different in that it just hides the element, leaving blank space for it in the document rather than completely removing it. * Any given element is either a non-replace element or a replaced element. Non-replaced elements are like the ''

'' tag; text goes inside the tag. Replaced elements are like the '''' tag; the tag is only a placeholder for something else that goes there. * As noted, all elements have margins and passing. However, margins/padding seems to behave a bit 'funny' for inline elements. See [[best-practices#Inline Elements]] for more. * Horizontal margins can be set to ''auto'' (which centers an element if you do it for both sides), but padding **MUST** be specified or it defaults to zero. ''auto'' can also be used inside an element (e.g. ''width: auto''). Setting horizontal margins to ''auto'' is a better way than text-align to center an element. * Vertical positioning is similar to horizontal positioning... margins can be set to ''auto'', height can be ''auto'', and padding can't. But there is a difference: ''auto'' margins center horizontally, but they get set to zero vertically. * Margins can also be negative, which makes the containing box bigger than the outside box. This can be useful in a few specific cases. * Margins/padding can also use percentages instead of absolute pixels. This can be good for scalable layouts, as you can do something like ''padding-left: 5%; width: auto; padding-right: 5%'' * ''height'' defines the height of an element. If there is more stuff than can fit in the element, you can use the ''overflow'' property to hide it, let it overflow, or add scrollbars. If ''height'' is defined as a percentage, the immediate parent element must have its height set, or else height defaults to auto. Rather annoying. * This means in order to make a full page layout, you have to set the height of both the html and body elements * Collapsing margins: vertical margins can //collapse//, which means that two vertical margins overlap so only the bigger 'applies'. This is unique to vertical margins, and doesn't affect vertical padding, horizontal margins, etc. * Lists (
    ,
  1. ,
      ) are just strange. There are some non-standard ways to control list markers etc., but these are still in flux. See [[best-practices#Lists]] for details * All elements can have borders. Borders are pretty much like you'd think they'd be... you can give them a ''border-width'', a ''border-style'', and a ''border-color''. There is also a combined property: ''border: 1px solid #000000;'' * Borders can be set to transparent: ''border-color: transparent'' This is cool if you need to replace the border when you're hovering over an item, and therefore don't want to set it to 0px when you're not (because the size would change). * Borders can have styles; there are a bunch but the only cool ones are ''solid'', ''dotted'' and ''dashed''. * You can give any element a ''min-width'' and ''min-height'', as well as a ''max-width'' and ''max-height''. IE6 doesn't honor these parameters, however. == Inline Elements == * Inline elements can be replaced or non-replaced, just like block elements. '''' is sort of the inline equivalent to the block element ''
      '' * All inline elements implicitly go in an 'inline box', which is basically as wide as the content, and as high as the content + leading. (Leading can, and usually does, happen if you have a line-height > font-size). In most cases, the content area (which is what has the text itself in it) will be smaller than the inline box. However, you can force it the other way, e.g. ''font-size: 24px; line-height: 12px''. Then the content area will spill out of the inline box, and the text may overlap other lines. * ''line-height'' is what controls the height of the lines. Basically, it's rather funky, but best practice seems to be to use either a scaling factor (line-hieght: 1.2) or base it on ems. * You can adjust the height of one element relative to another with ''vertical-align''. This can get a bit complicated, though, and the most flexible value (percentages) don't seem to work right in IE (even IE7). Valid values (that do work) are ''top'', ''bottom'', ''text-top'', ''text-bottom'', ''middle'', ''super'', & ''sub''. Note that vertical-align **ONLY** works on inline elements, not block level elements. * On inline **non-replaced** elements, padding and borders **DO NOT** alter the height or width of the content area, and thus the line-height. Therefore, you get all kinds of weird affects if you apply padding or borders to inline elements, with borders overlapping other lines, etc. Margins do not affect the top and bottom aspects of the content-area, but do affect the left and right aspects. So you can say ''margin-right: 20px;'' to indent some text, but you can't say ''margin-top: 20px'' to push it down. * However, even though padding doesn't alter the height of a line, it **does** get applied to inline non-replaced elements, which means you can make a background extend above or below a line using padding. * The ONLY things that affect line hieght are ''line-height'', ''font-size'', and ''vertical-align''. * On inline **replaced** elements (img, etc.) padding, borders and margins work more or less like box elements. == Hybrid Elements == * There are no hybrid elements, but there are a few hybrid display properties (in addition to the standard ''display: inline'' and ''display: block''). See [[http://www.quirksmode.org/css/display.html|QuirksMode]] for more. == Background Images == * Background images can be applies to just about any element, including stuff like and