CSS, a Cheat-sheet by Yumiko Huang and Deb Wolfe
CSS
─ cheatsheet ─

Cascading style sheets, or css files, can be used to modify HTML elements in HTML files. This technique for shining up web pages was introduced with HTML 4.0. Each HTML tag looks for a modifier in the file specified in an HTML link tag of relation = "stylesheet" and type "css/text". The effect of the stylesheet line is to modify the HTML tag.

The relationship between the stylesheet line and the HTML tag type doesn't have to be one to one. If that were true, we may as well specify all the style in the HTML tags. To display all paragraphs in arial font, we specify that in the stylesheet rather than in the HTML document. If we want our list-items to also display in arial font, we can promote the stylesheet font-family line to a body css line and take it out of the paragraph css line. To override the body font-family specification for certain special paragpraphs, enter another css line, say with a p-fw class, and specify the font-family of Courier.

In this way, the HTML modification is cascading. First it looks in the outermost layer in this case, body. Then it looks to the next layer of p for paragraph. We haven't specified a font for p alone so that takes the body's font-family of Arial. For a p with class of p-fw, we refer to the specification in the stylesheet of font-family Courier and override the body specification of Arial.

Positioning

fixed

position: fixed;

An element with a fixed position is positioned relative to the browser window, and will not move even if the window is scrolled.

relative

position: relative;

A relative positioned element is positioned relative to its normal position. The content of relatively positioned elements can be moved and overlap other elements, but the reserved space for the element is still preserved in the normal flow.

absolute

position: absolute;

An absolute position element is positioned relative to the first parent element that has a position other than static. If no such element is found, the containing block is html. When elements are positioned outside the normal flow, they can overlap other elements.The z-index property specifies the stack order of an element (which element should be placed in front of, or behind, the others).

Text

color

color: #000;

Modify the textual parts of element by set the color, text-align and text-ident properties.
The color can be assigned to the text of an element in three ways:

  • Hex: a value like "ff0000". The order is such that #RRGGBB specify the components of color.
  • RGB: a value rgb(255,0,0) will be set red since the first two places (RR) are specified with FF but the rest are set to 0.
  • Name: a color name like red. There are 17 standard colors and 123 more making 140 all together.

Text Example Text Example Result

text-align

text-align: center;

Set the horizontal (left to right) orientation of the text with the text-align property. For example, it may make sense to center the headers on the page. The text can be set according to one of four attributes.

  • Center: goes in the middle of the current horizontal space attributed to the element. For example, if a column is assigned to take up 30% of the page, the text-align: center element will go 15% from the left.
  • Left: keeps the text on the left side of the surrounding element. This is the default. Only specify left when a surrounding element has center or right and that previous text-align needs to be overwritten with a new text-align of left.
  • Align: takes the horizontal space currently allocated to the textual part of the element and outputs the letters with space in between such that every line takes up the same amount of horizontal space as the line preceeding. This has the effect of making both the left and right margins straight.

text-indent

position: absolute;

Set the left offset of the first line of text using the text-indent property. This literary practice is rarely used even in blog posts.

Background

margin

margin: 0 auto;

Background elements can be addressed with the margin, padding and gradient properties.
The margin property assures that there is free space between elements. Margin is transparent and will not cause a background to display. The shorthand property is used to specify all four directions in order. Starting from the top, go in a clockwise direction: Top, right, bottom, left.
There are four main modifiers for the margin property.

  • Auto: The browser default calculation.
  • Length: The margin in px, ct, or cm. px is the most popular unit of measure.
  • %: The percent of the width of the containing element.
  • inherit The margin inherited from the parent element.
Text Example Text Example Result

padding

padding: 10px 20px;

The padding property assures that there is free space around an element. For example, to keep text from bumping up against a horizontal rule preceeding the element, specify the top padding property. Specifying a margin would not be sufficent to keep the rule away from the text since margin starts outside the border of the element. Padding includes the space inside the border.

line-gradient

background: linear-gradient(red, blue);

To create a linear gradient you must define at least two color stops. Color stops are the colors you want to render smooth transitions among. You can also set a starting point and a direction (or an angle) along with the gradient effect.

radical-gradient

background: radial-gradient(red, green, blue);

A radial gradient is defined by its center. To create a radial gradient you must also define at least two color stops. By default, shape is ellipse, size is farthest-corner, and position is center.