BACKGROUND COLOR
body {background-color:#b0c4de;}
Example
body { background-image:url('img_tree.png'); background-repeat:no-repeat; background-position:right top; }
Try it yourself
h1 {background-color:#6495ed;} p {background-color:#e0ffff;} div {background-color:#b0c4de;}
BACKGROUND IMAGE
body {background-image:url('[Link]');} body {backgroundimage:url('[Link]');}
Background - Shorthand property
As you can see from the examples above, there are many properties to consider when dealing with backgrounds. To shorten the code, it is also possible to specify all the properties in one single property. This is called a shorthand property. The shorthand property for background is simply "background":
Background Image - Repeat Horizontally or Vertically
body { background-image:url('[Link]'); }
body { background-image:url('[Link]'); background-repeat:repeat-x; }
Example
body {background:#ffffff url('img_tree.png') no-repeat right top;}
Try it yourself
Background Image - Set position and no-repeat
When using a background image, use an image that does not disturb the text. Showing the image only once is specified by the background-repeat property:
TEXT FORMATTING
This text is styled with some of the text formatting properties. The heading uses the text-align, texttransform, and color properties. The paragraph is indented, aligned, and the space between characters is specified. The underline is removed from the "Try it yourself" link.
Example
body { background-image:url('img_tree.png'); background-repeat:no-repeat; }
Try it yourself
Text Color
The color property is used to set the color of the text. In the example above, the background image is shown in the same place as the text. We want to change the position of the image, so that it does not disturb the text too much. The position of the image is specified by the background-position property: With CSS, a color is most often specified by:
y y y
a HEX value - like "#ff0000" an RGB value - like "rgb(255,0,0)" a color name - like "red"
Look at CSS Color Values for a complete list of possible color values.
The default color for a page is defined in the body selector.
Example
a {text-decoration:none;}
Example
body {color:blue;} h1 {color:#00ff00;} h2 {color:rgb(255,0,0);}
Try it yourself
It can also be used to decorate text:
Try it yourself
Example
For W3C compliant CSS: If you define the color property, you must also define the background-color property.
h1 h2 h3 h4
{text-decoration:overline;} {text-decoration:line-through;} {text-decoration:underline;} {text-decoration:blink;}
Text Alignment
The text-align property is used to set the horizontal alignment of a text. Text can be centered, or aligned to the left or right, or justified. When text-align is set to "justify", each line is stretched so that every line has equal width, and the left and right margins are straight (like in magazines and newspapers).
Try it yourself
It is not recommended to underline text that is not a link, as this often confuses users.
Text Transformation
The text-transform property is used to specify uppercase and lowercase letters in a text.
Example
h1 {text-align:center;} [Link] {text-align:right;} [Link] {text-align:justify;}
It can be used to turn everything into uppercase or lowercase letters, or capitalize the first letter of each word.
Example
Try it yourself
[Link] {text-transform:uppercase;} [Link] {text-transform:lowercase;} [Link] {text-transform:capitalize;}
Try it yourself
Text Decoration
The text-decoration property is used to set or remove decorations from text. The text-decoration property is mostly used to remove underlines from links for design purposes:
Text Indentation
The text-indentation property is used to specify the indentation of the first line of a text.
Example
p {text-indent:50px;}
link */ a:hover {color:#FF00FF;} /* mouse over link */ a:active {color:#0000FF;} /* selected link */
Try it yourself
Try it yourself
Font Family
p{font-family:"Times New Roman", Times, serif;}
When setting the style for several link states, there are some order rules:
Font Style
[Link] {font-style:normal;} [Link] {font-style:italic;} [Link] {font-style:oblique;}
y y
a:hover MUST come after a:link and a:visited a:active MUST come after a:hover
Common Link Styles
In the example above the link changes color depending on what state it is in. Lets go through some of the other common ways to style links:
Font Size with Pixels
h1 {font-size:40px;} h2 {font-size:30px;} p {font-size:14px;}
Font Size With Em
h1 {font-size:2.5em;} /* 40px/16=2.5em */ h2 {font-size:1.875em;} /* 30px/16=1.875em */ p {font-size:0.875em;} /* 14px/16=0.875em */
Text Decoration
The text-decoration property is mostly used to remove underlines from links:
Use a Combination of Percent and Em
body {font-size:100%;} h1 {font-size:2.5em;} h2 {font-size:1.875em;} p {font-size:0.875em;}
Example
a:link {text-decoration:none;} a:visited {text-decoration:none;} a:hover {text-decoration:underline;} a:active {text-decoration:underline;}
Try it yourself
Styling Links
Links can be styled with any CSS property (e.g. color, font-family, background, etc.). Special for links are that they can be styled differently depending on what state they are in. The four links states are:
Background Color
The background-color property specifies the background color for links:
y y y y
a:link - a normal, unvisited link a:visited - a link the user has visited a:hover - a link when the user mouses over it a:active - a link the moment it is clicked
Example
a:link {background-color:#B2FF99;} a:visited {background-color:#FFFF85;} a:hover {background-color:#FF704D;} a:active {background-color:#FF704D;}
Try it yourself
Example
a:link {color:#FF0000;} link */ a:visited {color:#00FF00;} /* unvisited /* visited y
The CSS list properties allow you to:
Set different list item markers for ordered lists
y y
Set different list item markers for unordered lists Set an image as the list item marker
If you want the image-marker to be placed equally in all browsers, a crossbrowser solution is explained below.
Crossbrowser Solution List
In HTML, there are two types of lists: The following example displays the image-marker equally in all browsers:
Example
y y
unordered lists - the list items are marked with bullets ordered lists - the list items are marked with numbers or letters
With CSS, lists can be styled further, and images can be used as the list item marker.
Different List Item Markers
The type of list item marker is specified with the liststyle-type property:
ul { list-style-type: none; padding: 0px; margin: 0px; } li { background-image: url([Link]); background-repeat: no-repeat; background-position: 0px 5px; padding-left: 14px; }
Try it yourself
Example
Example explained:
ul.a {list-style-type: circle;} ul.b {list-style-type: square;} ol.c {list-style-type: upper-roman;} ol.d {list-style-type: lower-alpha;}
Try it yourself
For ul:
o o y
For li:
Set the list-style-type to none to remove the list item marker Set both padding and margin to 0px (for cross-browser compatibility) Set the URL of the image, and show it only once (no-repeat) Position the image where you want it (left 0px and down 5px) Position the text in the list with paddingleft
o o o
Some of the values are for unordered lists, and some for ordered lists.
An Image as The List Item Marker
To specify an image as the list item marker, use the liststyle-image property:
List - Shorthand property
It is also possible to specify all the list properties in one, single property. This is called a shorthand property. The shorthand property used for lists, is the list-style property:
Example
ul { list-style-image: url('[Link]'); }
Try it yourself
Example
ul { list-style: square url("[Link]"); }
The example above does not display equally in all browsers. IE and Opera will display the image-marker a little bit higher than Firefox, Chrome, and Safari.
Try it yourself
Examples of inline elements:
When using the shorthand property, the order of the values are:
y y
<span> <a>
y y y
list-style-type list-style-position (for a description, see the CSS properties table below) list-style-image
Changing How an Element is Displayed
Changing an inline element to a block element, or vice versa, can be useful for making the page look a specific way, and still follow web standards. The following example displays list items as inline elements:
Hiding an Element - display:none or visibility:hidden
Hiding an element can be done by setting the display property to "none" or the visibility property to "hidden". However, notice that these two methods produce different results: visibility:hidden hides an element, but it will still take up the same space as before. The element will be hidden, but still affect the layout.
Example
li {display:inline;}
Try it yourself
Example
[Link] {visibility:hidden;}
Try it yourself
The following example displays span elements as block elements:
Example
span {display:block;}
Try it yourself
display:none hides an element, and it will not take up any space. The element will be hidden, and the page will be displayed as the element is not there:
Example
[Link] {display:none;}
Try it yourself
CSS Display - Block and Inline Elements
A block element is an element that takes up the full width available, and has a line break before and after it. Examples of block elements:
y y y
<h1> <p> <div>
An inline element only takes up as much width as necessary, and does not force line breaks.