Archive for the ‘Cheat Sheets’ Category

File Under: Cheat Sheets, CSS

CSS Units of Measure

Throughout this CSS reference guide, abstract values (notably values involving units of measure) appear between angle brackets:<color>. They should be replaced by actual values as indicated in the key below.

Length Units

<length> indicates a number followed by a unit of measure:24px.

The number can be an integer or a decimal fraction, and can be preceded by + or -.

Units can be absolute or relative:

Absolute: mm, cm, in, pt, pc (millimeters, centimeters, inches, points, picas)

Relative: em, ex, px (the element’s font height, the element’s x-height, pixels)

Font size may be defined in points, pixels, inches, or centimeters (pt, px, in, cm) or as a percentage.

<absolute-size> can be:xx-small, x-small, small, medium, large, x-large, xx-large.

<relative-size> can be:larger, smaller.

Percentage Units

<percentage> indicates a number followed by a % sign:50%.

In the text-indent, margin, padding, and width properties, percentage values are relative to the width of the parent element.

In the font-size property, percentage values are relative to the font size of the parent element.

In <color> values, percentages can be used to express RGB values.

Color Units

<color> can represent either <color-name> or <rgb> values, as defined below:

<color-name> can be: aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, yellow

<rgb> can be:

#<hex><hex><hex>

rgb(<number>, <number>, <number>)

rgb(<percentage>,<percentage>,<percentage>)

<hex> represents a hexidecimal value, <number> a decimal value, and <percentage> a percentage.

File Under: Cheat Sheets, CSS

Linking Stylesheets

Local (inline) stylesheet declarations, specific to a single instance on a page, can be used instead of <font> tags to specify font size, color, and typeface and to define margins, leading, etc.

<p style="font size:small; color:red; font-weight:bold; 
font-family:Arial, Helvetica, non-serif">
This is a local stylesheet declaration. 
</p> 

Global (embedded) stylesheet declarations, applicable to an entire document, are defined within the <style> and </style> tags, which precede the <body> tag in the HTML document and are usually placed in the header.

To embed a global stylesheet in your HTML document:

<html>
    <head>
    <title>Title</title>
    <style type="text/css">
    <!--
    [STYLE INFORMATION GOES HERE]
    -->

    </style>
    </head>
    <body>
    [DOCUMENT BODY GOES HERE]
    </body>
    </html>

Linked stylesheet declarations use a single stylesheet (in a separate file, saved with the .css suffix) to define multiple pages. A typical .css file is a text file containing style rules, as here:

P {font-family:non-serif; font-size:medium; color:red}
H1 {font-family:serif; font-size:x-large; color:green}
H2 {font-family:serif; font-size:large; color:blue}

To apply a .css stylesheet (“style.css” in the example below) to an HTML page, a <link> tag is added to the page header:

<head>
<link rel="stylesheet" href="style.css" type="text/css">
</head>

Inheritance

In cases where local, global, and linked style definitions conflict, the most specific stylesheet will generally take precedence:local overrides global, global overrides linked. Similarly, inline style attributes override ID, ID overrides class, and class overrides stylesheet-defined HTML elements.

File Under: Cheat Sheets, CSS

How CSS Works

CSS overrides the browser’s default settings for interpreting how tags should be displayed, letting you use any HTML element indicated by an opening and closing tag (including the <p> tag) to apply style attributes defined either locally or in a stylesheet.

Stylesheets contain rules, composed of selectors and declarations that define how styles will be applied. The selector (a redefined HTML element, class name, or ID name) is the link between the HTML document and the style. There are two different kinds of selectors:types (HTML element tags) and attributes (such as class and ID names).

A CSS declaration has two parts,

a property ("color") and a value ("red"). 

The basic syntax of a rule

selector {property 1:value 1; property 2:value 2} " 

An example (containing two declarations, as above)

P {font-size:8pt; color:red}

CSS is not picky about whitespace, linebreaks, or extra semi-colons, so you can format complex CSS files to make them more manageable;

P {
   font-size: 8pt; 
   color: red;
}

In addition, you can apply the same style rules to more than one element simultaneously by comma-separating them;

H1, H2, H3 {
   font-size: 8pt; 
   color: red;
}
File Under: Cheat Sheets, CSS

CSS Guide

What Is CSS?

CSS (cascading stylesheets) is a simple mechanism for controlling the style of a web document without compromising its structure. By separating visual design elements (fonts, colors, margins, and so on) from the structural logic of a web page, CSS gives designers the control they crave without sacrificing the integrity of the data, thus maintaining its usability in multiple environments.

In addition, defining typographic design and page layout from within a single, distinct block of code — without having to resort to image maps, <font> tags, tables, and spacer GIFs — allows for faster downloads, streamlined site maintenance, and instantaneous global control of design attributes across multiple pages.

Client-side support for the various CSS properties is uneven, even among browsers that support stylesheets.

File Under: Cheat Sheets, CSS

CSS Examples

Individual Properties
Style rules

Comments
p {font-family:Helvetica, Geneva; Of those listed, the first available font will be applied.
font-style:italic;

Text will be rendered as italic.
font-weight:bold; Text will be rendered as bold.
font-size:10pt; Text will be rendered as 10-point type.

color:#00FFFF; Text will be rendered to the hex color value #00FFFF.
background-color:blue; The element’s background color will be rendered as blue.
background-image:url(images/foo.gif)} The element will have the background image(s) specified.

.foo {text-decoration:underlined}

Text in the class “foo” will be underlined.
{text-transform:lowercase} Text in the class “foo” will be rendered as lowercase.

#foo

{text-align:center} Text with the ID “foo” will be center-aligned.
{text-indent:1em} Text with the ID “foo” will be indented 1 em.

p {margin-top:12px; The element’s top margin will be 12 pixels.
margin-bottom:18px; The element’s bottom margin will be 18 pixels.

padding-right:8pt; The element’s right padding will be 8 points.
padding-left:4pt} The element’s left padding will be 4 points.

body {border-color:red; Borders will be red.
border-style:solid}

Borders will be solid.
Block-level and replaced elements
img {width:20px; The element’s width will be 20 pixels.
height:40px; The element’s height will be 40 pixels.
float:left} Text will flow to the left of the element.

p {clear:left} Moves the element below a floating element placed on the left.
Shorthand Properties
Style rules Comments
p {font:10pt bold non-serif} Text will be rendered as 10-point bold non-serif type.
{background:black} The element’s background color will be black.
{margin:25px} All of the element’s margins will be 25 pixels.
{margin:20px 10px 20px 10px} Top and bottom margins will be 20 pixels; left and right margins will be 10 pixels.

body {padding:2px}

Padding will be 2 pixels on each side.
{padding:8px 4px 8px 4px} Top and bottom padding will be 8 pixels; left and right padding will be 4 pixels.
{border:2px solid red} There will be a red, solid border on all sides of the element.

Position (CSS – P)
Style rules Comments
#foo

{position:absolute; top:15pt; left:15pt} The element’s absolute position will be set 15 points from the top and 15 points from the left.
{visibility:hidden} The element’s contents will be hidden.

.foo {z-index:2} The element’s z-index will be two.

p

{overflow:visible} The element’s contents will be rendered (visible) even if they exceed its height or width.
{overflow:hidden} The element’s contents will be clipped to its height and width, and no scrolling mechanism will be provided.