Elements of Style
- Selector (the html tag, i.e. p, b, li, tr, etc.)
- Declaration (the style to be assigned to that tag)
- Property (the type of style, i.e. font-size, background, left-margin, etc.)
- Value ( amount of that style to apply, i.e. 14pt, blue, 2em, none, etc.)
To link to an external style sheet, include the following tag nested inside the html head:
‹link rel="stylesheet" href="location/filename.css" type="text/css"›
Font & Text Styles
Specifying how fonts are displayed
{ font-family: }
- font name (i.e. courier, "Times New Roman", arial)
- use lowercase spelling
- put quotes around more-than-one-word names; use single quotes for inline declarations
- for grouped declarations, put font-family last
- serif
- sans-serif
{ font-size: }
- points, ems, pixels
- keywords
- percentage (based on value of parent element
↑ Return to the top of the page ↑
{ font-weight: }
(bold and values if availabe in font)
{ font-style: }
(italics)
{ font-variant: }
(small-caps)
{ text-transform: }
(uppercase, lowercase, capitalize, none)
{ text-decoration: }
- underline
- overline
- line-through
- blink
- none--use this to remove the underline from hyperlinks
{ font: }
(shorthand for assigning font-size, line-height & font-family all at once)
- ex. li { font:12pt/16pt courier }
- must specify all three
↑ Return to the top of the page ↑
Paragraph alignment & spacing
{ word-spacing: }
(add space between words)
{ letter-spacing: }
(add space between letters)
{ line-height: }
(add space between lines)
- number (multiples of font size)--relative
- length unit (specific size)--fixed
- percentage (percent of font size)--relative
↑ Return to the top of the page ↑
{ text-align: }
(controls horizontal alignment of paragraph)
- left
- center
- right
- justify
{ vertical-align: }
(controls vertical alignment relative to parent element)
{ text-indent: }
(creates a first-line indent)
↑ Return to the top of the page ↑
Alignment
The Float Property
The float property allows you to flow or wrap text around any block-level element (html tags that define new paragraphs on their own, i.e. p, h1, blockquote, ul, etc.) or replaced element (i.e. images, plug-ins, etc.). This can be very useful for page layout and design because it allows for more flexibility with less coding than html tables.
The two float properties are defined as follows:
- { float: }
- left--aligns the block-level element to the left of the page and forces following elements to wrap around to the right of it.
- right
- none--no floating/inline
- { clear: }
browser makes sure that the specified side is clear of all floating elements before it displays the element (below and to the left of any nearby floating elements) - none--no clearing
- left--clear the left side, but allow other elements to float on the right
- right
- both
For a complete table listing all of the CSS properties available, visit the World Wide Web Consortium at www.w3.org.
↑ Return to the top of the page ↑
The CSS Box Model
CSS puts an invisible container around every block-level or replaced element. Each box has padding, border, and margin properties that can be styled. Margins refer to the space outside the border, and padding refers to the space inside the border.
Margins, borders, and padding can be set independently on each side, using any unit of measurement. This is especially helpful in fine-tuning the look of a page by precisely controlling the spacing around each element.
In addition, borders can also be styled with the width, color, and style properties.
↑ Return to the top of the page ↑
Margins
- defines space outside the border
- possible to set each margin independently
- some browsers sum adjacent margins, others ignore one
- use any unit of measurement or percent
{ margin-top: }
{ margin-bottom: }
{ margin-left: }
{ margin-right: }
{ margin: }
Borders
Width:
- defines the thickness of the border
- possible to set each side independently
- use any unit of measurement or keywords: thin, medium, thick
{ border-top-width: }
{ border-bottom-width: }
{ border-left-width: }
{ border-right-width: }
{ border-width: }
↑ Return to the top of the page ↑
Color:
- border takes on color of element unless specified
- set individual colors for each side by listing in order:
top, right, bottom, left
ex. p { border-color: #666699 #FF0033 #000000 #FFFF99 }
- use named browser colors or hexadecimal
{ border-color: }
Style:
- set the type of border to display
- set individual styles for each side by listing in order:
top, right, bottom, left
ex. p { border-style: solid dotted dashed none }
{ margin-style: }
- solid
- double
- dotted
- dashed
- groove
- ridge
- inset
- outset
- none
Padding
- defines space the between the element and the border
- possible to set each side independently
- use any unit of measurement or percent
{ padding-top: }
{ padding-bottom: }
{ padding-left: }
{ padding-right: }
{ padding: }
↑ Return to the top of the page ↑
Colors & Backgrounds
Colors can be specified in three ways using Cascading Style Sheets: with names browser colors, hexadecimal codes, and RGB values. This last option is especially helpful for users of graphic software, as this eliminates the need to translate RGB values into hexadecimal code. CSS also allows for a shorthand in hexadecimal codes that allows for three decimal places rather than 6.
The color and background properties are defined below.
{ background-color: }
- adds a solid color behind any element
- specify by color name, hex, or rgb
- transparent (clear/no background)
{ background-image: url(*.gif) }
- adds an image behind any element
- put the relative or absolute path to the file in the parenthesis
- none (overrides any inherited background image)
- can combine background-image and -color
- cannot specify image size
{ background-repeat: }
- specify whether the background of an image should tile
- repeat (tile horizontally and vertically to fill background of the element)
- repeat-x (tile image horizontally only)
- repeat-y (tile image vertically only)
- no-repeat (displays background image once--no tiling)
{ background-attachment: }
- scroll (background image scrolls with the page contents)
- fixed (background image will not scroll, but remains fixed to the window)
- works only on the body tag (page background)
{ background-position: }
- position where the background image will begin displaying
- top (aligns background image with top of foreground element's box)
- bottom (aligns with bottom of the box)
- left (positions image along left side of box)
- right (positions along the right side of box)
- center (center horizontally if used before another keyword, vertically if after)
- <length value> (any unit of measurement)
- <percentage>
{ background: }
- define any or all background characteristics: color, image, tiling method, scrolling versus fixed status, and position
↑ Return to the top of the page ↑
Layout with CSS
The Position Property
CSS allows you to precisely position any element on the page. Using the position property, an element can be positioned according to the "normal flow" (static), relative to its normal position in the normal flow (relative), or absolutely, meaning it has been taken out of the normal flow.
Once an element has been positioned, its placement can then be specified in the page with measurements taken from the left, top, right, and/or bottom of the browser window (absolute postitioning) or relative to its normal position in the flow of the document (relative psotioning).
It is now possible with cascading style sheets to specify the amount of space an element should occupy, rather than filling the browser window as needed. CSS also allows for specification of how to display content that is too large for its containing box, as well as how to make an element invisible or crop a part of it. This feature is especially useful when combined with dynamic scripting that allows the element or cropped part to appear when the end user performs an action (i.e. mouseover).
When positioning elements on the page, it is possible to create overlapping elements. To control the order of where items appear in the stack, the z-index property can be specified on any and all relatively and absolutely positioned elements.
{ position: }
- static (normal)
- relative (positioned relative to its natural position in the document's flow)
- absolute (positioned independent of any other object on the page)
- fixed
{ left: }
- amount of space between element and left edge of browser window
- <length value> (any unit of measurement)
- <percentage>
{ top: }
- amount of space between element and top of browser window
- <length value> (any unit of measurement)
- <percentage>
{ right: }
- <length value> (any unit of measurement)
- <percentage>
{ bottom: }
- <length value> (any unit of measurement)
- <percentage>
↑ Return to the top of the page ↑
{ width: }
- control width of element box
- <length value> (any unit of measurement)
- <percentage>
- works only on absolutely positioned elements
{ height: }
- control height of element box
- <length value> (any unit of measurement)
- <percentage>
{ overflow: }
- specify how to display content that is too big for the containing box
- visible (all content displayed, even if outside declared boundaries of box)
- hidden (browser clips off whatever content goes outside the box)
- scroll (content will be clipped, but browser will always display a scrollbar)
- auto (content will be clipped, but browser will display a scrollbar if necessary)
{ visibility: }
- specify whether an element is visible or invisible
- visible (element is visible)
- invisible (element is invisible)
- object takes up the same space in the browser window even when invisible
{ clip: }
- crop the visibility of an element
- rect(<top>, <right>, <bottom>, <left>)
- works only with absolute positioning
- combine with visibility & DHTML to hide & make objects visible with rollovers
{ z-index: }
- specify the order in which to display overlapping elements
- integer
- works with relative and absolute positioning only
