CSS unitsWhen specifying numerical or length values for CSS properties such as PixelsPixels are the tiny dots that make up your computer screen and are the most accurate unit you can use for this medium. If you set a width to 100 pixels it will be displayed exactly like that, no more and no less. To set values in pixels you simply append a px right after the value. CSS pixel unit:img {width:100px}
Relative valuesRelative values are often used for CSS relative value unit:body {font-size:13px}
p {font-size:2em} Above code sets the If we use 1.9em instead, the paragraph value will result in 24.7 pixels. Because a computer screen can only display whole pixels the web browser will attempt to round the value to either 24 pixels or 25 pixels which can sometimes cause inconsistencies between browsers. PercentagesPercentages work pretty much the same way as relative values. 100% takes the current value unchanged, 200% doubles it and so on. Percentages are set by appending a %. CSS percentage unit:body {font-size:13px}
p {font-size:200%} ZeroWhen a value is set to zero there's no need to state a unit. CSS value zero:p {border-width:0}
Other unitsThere are some more rarely used units such as pc (picas), cm (centimeters), mm (millimeters), in (inches) and pt (points). Usage guidelines1. Relative values for fontsA website ought to be flexible and resizable, including fonts (by holding down the Ctrl key and moving your mouse wheel). Some older versions of Internet Explorer refuse to resize fonts if you specify the 2. Pixels only if neededThe general rule of thumb is to use relative values and percentages wherever possible to ensure a flexible layout across all media. Though pixels are ok if you need to position things very precisely on-screen. |