CSS tutorial and reference – begin
(Some of the examples will show up better as a web page — but not so well in this printout.)

HOW TO USE THIS TUTORIAL

This page has a list of the common words in the CSS language, with examples of code and results.

To make it most useful, follow the steps below (this is how we learn programming as well!)

1. Before you start:
  • Make sure you are already comfortable with html codes as in the beginning html tutorial pages
  • Read through the files in this folder: How to learn CSS, How to fix web page problems, and the CSS FAQ
    You won't understand everything on those pages, but that's OK! The concepts will be rolling around in the back of your mind while you try these codes.
    Each time you look at the pages again, you will understand more.
2. Then: try things — HTML/CSS is doing!
  • Using your text editor, type the code examples on this page.
    (Use this template to make your web pages.)
  • Using your web browser, view the results.
  • Try changing your code and view it again.
3. Keep up your own notes and references as you learn.
  • Nobody can remember all these things! The people who seem smart, are actually the ones who keep and use references.
4. Learn more:

On this page:

On this page Contents

Some things to know:
A tag is a box: The box model | Styling the box
Two more tags: div | span
Read "The three places to talk css language" here in the How to Learn CSS page to understand this reference better! (Read the other pages in this folder too :)
The reference:
Properties for text: Fonts | Color | Spacing | Alignment | Appearance
Properties for boxes: Margin and padding | Background | Border | Size and Display
Properties for List
More to know:
class and id | doctype | more |
It's too hard!
Misc: copyright | translation | contact

A tag is a box

The box model: Think of each html tag as making a box:

p

The <p> tag opens the box, and the </p> tag closes the box.

h1

The <h1> tag opens the box, and the </h1> tag closes the box.

img

The <img /> tag opens and closes the box.

ul

li

li

The <ul> tag opens the outer box.
An <li> tag opens an inner box, and an </li> tag closes an inner box.
Then the </ul> tag closes the outer box.

Every box is an html tag. Every box has margin and padding:

margin
padding

content:
text, image, movie, ...
border

Styling the box

This means we can give "style" to the different parts of the box.

His Holiness Dalai Lama

This has three "boxes" (tags): div, img, and p.
We give the styles to the "boxes" like this:

    <div style="margin: auto; 
          margin-top: 20px;
          width: 250px;
          border: 8px solid #cc0000; 
          background-color: #ff9933;
          text-align: center;
          padding: 30px; 
          padding-bottom: 0;">
            <img src="images/dalai_lama-sm.jpg" 
                style="padding: 10px;   
                    background-color: #ffff00;
                    border: 2px solid #cc0000;" />
            <p style="font-size: 90%; color: green;">
            His Holiness Dalai Lama
            </p>
    </div>
    

Read on to learn how to use these styles!
(I promise they won't always look so confusing :)


explanation of this reference

"property name"

Outlined properties

are the most used —
learn these first.

"possible values"

Values in orange color are the values allowed for a property.

Values in angle brackets, such as <family-name> or <length> are to be replaced with an actual value such as verdana or 500px

"example code"

To keep things clear, i am only showing one property for each example.
In real pages you can:

Use many properties for one element.
Use any property that would make sense for that element.

"example
display"

Here will be what should show in the browser.

"default"

The value that the web browser will use if you don't specify anything for this property.
("n/a" means there will be no default value.)

"Use in"

The elements that would use this property.

"Percentages
refer to"

If the style uses percent measurement, what it is a percent of.
("n/a" means "not applicable" — percentage measurements are not used here.)

Here are the main codes that we use in CSS. Try them in your web page.

properties for text: Fonts
property name possible values example code example display default use in percentages
refer to
font-family any <family-name>, such as verdana or "times roman"
or
<generic-family>
[ serif, sans-serif, cursive, fantasy, monospace]
<p style="font-family: Times, serif;">
Here is a paragraph in Times font.
</p>
<p style="font-family: 'Times roman', serif;">
If you use a font name that has more than one word,
use quotes.
</p>

Here is a paragraph in Times font.

If you use a font name that has more than one word, use quotes.

The font set in the user's browser. Any element that holds text. n/a
font-style [ normal, italic ]
<p>
We will learn some 
<span style="font-style: italic;">very useful</span>
css coding.
</p>

We will learn some very useful css coding.

The font size set in the user's browser. Any element that holds text. n/a
font-weight [ normal, bold, 100, 200, 300, 400, 500, 600, 700, 800, 900 ]

(The numbers are supposed to give shades of light to dark, but not all browsers do this yet. So we usually just use the bold property.)

<p>
We will learn some 
<span style="font-weight: bold">very useful</span>
css coding.
</p>

We will learn some very useful css coding.

normal Any element that holds text. n/a
font-size [ <absolute size>, <relative size>, <length>, <percentage> ]

Examples:
absolute size: 12px
relative size: 1em
percentage: 60%

(Using a % measurement is most useful in all browsers.)
<p style="font-size: 80%;">
We usually use some kind of relative size
for our font size.
Then our reader can make the size bigger and
smaller in the browser if they want to, 
to be more useful to their needs.
</p>

We usually use some kind of relative size for our font size. Then our reader can make the size bigger and smaller if they want, to be more useful to their needs.

100% Any element that holds text. The original base size of the font.
 
properties for text: Color
property name possible values example code example display default use in percentages
refer to
color <color>

(such as #000000 or #ffffcc)

<p style="color: #669966;">
It is better to use color codes (like #669966)
rather than color names (like green)
because browsers will understand 
the codes better, and everyone will see 
the color you want.
</p>

It is better to use color codes (like #669966) rather than color names (like green) because browsers will understand the codes better, and everyone will see the color you want.

... Any element that holds text. (This property is like "font-color".) n/a
 
properties for text: Spacing
property name possible values example code example display default use in percentages
refer to
line-height [ normal, <number>, <length>, <percentage> ]

(Using a decimal number is most useful in all browsers.)

(See the CSS faq page, section on class and id for how to use the "class" below.)

p.spacer {
  line-height: 1.3;
}
p.more-spacer {
  line-height: 1.9;
}
<p>
The "normal" or default line-height 
is about 120% (1.2), depending on the browser.
</p>
<p class="spacer">
The line-height property
sets the distance between the baselines
of the lines of text. (This is called 
"leading" in paper publishing.)
</p>
<p class="more-spacer">
Just as on paper, having some spacing
between the lines makes it easier to read.
</p>

The default line-height is 100% to 120%, depending on the browser.

The line-height property sets the distance between the baselines of the lines of text. (This is called "leading" in paper publishing.)

Just like on paper, having some spacing between the lines makes it easier to read. This may be even more important on the web, since reading on the screen is harder on the eyes than reading from paper.

100% to 120%, depending on the browser Any element that holds text. The original base size of the font.
word-spacing [ normal, <length> ]

(Using the relative measurement em is most useful in all browsers.)
<h3>
Here is a normal h3.
</h3>
<h3 style="word-spacing: .5em;">
Here is h3 with some extra space 
between the words.
</h3>

Here is a normal h3.

Here is h3 with some extra space between the words.

... Any element that holds text.  
word-wrap [ normal, break-word ]
<p>
Here-is-a-long-much-too-long-word-that-goes-on-and-on-with-no-word-wrap
</p>
<p style="word-wrap: break-word;">
Here-is-a-long-much-too-long-word-that-goes-on-and-on-but-wraps-with-word-wrap
</p>

Here-is-a-long-much-too-long-word-that-goes-on-and-on-with-no-word-wrap

Here-is-a-long-much-too-long-word-that-goes-on-and-on-but-wraps-with-word-wrap

... Any element that holds text.  
letter-spacing [ normal, <length> ]

(Using the relative measurement em is most useful in all browsers.)
<p>
Here is a normal paragraph.
</p>
<p style="letter-spacing: .2em;">
Here is a paragraph with some extra space 
between the letters.
</p>

Here is a normal paragraph.

Here is a paragraph with some extra space between the letters.

... Any element that holds text. The original base size of the font.
white-space [ normal, pre, nowrap ]

(See the CSS faq page, section on class and id for how to use the "class" below.)

p.one {
  white-space: pre;
}
p.two {
  white-space: nowrap;
}
<p>
The <code>pre</code> value is 
like the     &lt;pre&gt;     tag in html. 
It makes the browser display all 
    white space.
</p>
<p class="one">
However, the html 
    &lt;pre&gt;     tag displays in a
        monospace font, and the 
            <code>pre</code> value does not. 
</p>
<p class="two">
The <code>nowrap</code> value 
    ignores white space
        and line breaks, and will not    wrap     the lines.
</p>

The pre value is like the <pre>
tag in html. It makes the browser
display all white space.

However, the html <pre> tag displays in a monospace font, and the pre value does not.

The nowrap value ignores white space and line breaks, and will not wrap the lines.

normal Any element that holds text. n/a
text-indent [ <length>, <percentage> ]
p {
  text-indent: 3em;
}
<p>
This will make some indentation to appear in the 
first line of text. ...
</p>

This will make some indentation to appear in the first line of text. Values can be specified in any unit of length, or as a percentage of the line length.

none Any element that holds text. The original base size of the font.
 
properties for text: Alignment
property name possible values example code example display default use in percentages
refer to
text-align [ left, right, center, justify ]
p {
  text-align: center;
}
<p>
This property is used only to align things <i>inside</i> 
a block (paragraph, header, table cell, etc.).
We will learn other ways to align the block itself.
</p>

This property is used only to align things inside a block tag (paragraph, header, table cell, etc.). We will learn other ways to align the block itself. (See the HTML tutorial page, section on the "three kinds of tags", for more about block tags.)

left Any element that holds text. It will center images inside the element also. n/a
vertical-align [ baseline, bottom, middle, sub, super, text-bottom, text-top, top, <percentage> ]
p {
  vertical-align: bottom;
}
<p>
This property is used only for content of block tags.
</p>

This property is used only for content of block tags.

... Any block element. n/a
 
properties for boxes: Border

The border appears outside the padding, and inside the margin.

property name possible values example code example display default use in percentages
refer to
border <border-width> <border-style> <color>

This shows the short way to set all three in one declaration. See below for the individual properties.

p {
  border: 1px solid #cc0000;
  padding: 4px 8px 4px 8px;
}
<p>
We can do lots of nice things with borders.
</p>

We can do lots of nice things with borders.

... All elements n/a
border-width Usually as px
table {
  border: 4px groove #cc6633;
}
<table>
<tr valign="top">
<td>
<img src="images/bullit1.gif" />
</td>
<td>
With css we can easily make a border around a table,
but no borders for the cells.
</td>
</tr>
</table>
image here With css we can easily make a border around a table, but no borders for the cells.
border-style [ solid, dashed, dotted, double, groove, ridge, inset, outset ]
border-color <color>

(such as #000000 or #cc0000)

border-top border-right border-bottom border-left <border-width> <border-style> <color>

Again, we use the short way to set all three properties in one declaration. See above for the individual properties.

p {
  border-left: 4px double #cc0000;
  padding: 4px 8px 4px 8px;
}
<p>
We can do lots of nice things with borders.
</p>

We can do lots of nice things with borders.

...    
...
 
properties for boxes: Size and Display
property name possible values example code example display default use in percentages
refer to
width [ <length>, <percentage>, auto ]

(See the CSS faq page, section on class and id for how to use the "class" below.)

p.one {
  width: 70%;
}
p.two {
  width: 10em;
}
p.three {
  width: 150px;
}
<p class="one">
This paragraph should be 70% of the 
containing block. ...
</p>
<p class="two">
An "em" is the width of a letter "m" in that font".
This paragraph should be 10 "ems" wide.  ...
</p>
<p class="three">
This paragraph should be 150 pixels wide. ...
</p>

This paragraph should be 70% of the "containing block" (the box it is inside). The paragraph width should change if the browser window (and block) get narrow or wide.

An "em" is the width of a letter "m" in that font". This paragraph should be 10 "ems" wide. The width should resize if you make the browser font size bigger or smaller.

This paragraph should be 150 pixels wide. The paragraph width will depend on your screen resolution (how big your screen pixels are).

... All elements except inline elements and table rows. width of the containing block.
height [ <length>, <percentage>, auto ]

We very rarely use "height". It is better to let the box stretch to its height depending on what is inside it.

(See the CSS faq page, section on class and id for how to use the "class" below.)

p.one {
  height: 20%;
}
p.two {
  height: 8ex;
}
p.three {
  height: 100px;
}
<p class="one">
The height of this paragraph should be 20% 
of the height of the containing block. ...
</p>
<p class="two">
An "ex" is the height of a letter "x" in that font.
This paragraph should be 8 "ex" high. ...
</p>
<p class="three">
This paragraph should be 100 pixels high. ...
</p>

Note: I put borders around each paragraph so you can see the paragraph height. ('height' does not actually make borders!)

The height of this paragraph should be 20% of the height of the containing block. The paragraph height should change if the block gets shorter or taller.

An "ex" is the height of a letter "x" in that font. This paragraph should be 8 "ex" high. The height should resize if you make the browser font size bigger or smaller.

This paragraph should be 100 pixels high. The paragraph height will depend on your screen resolution (how big your screen pixels are).

... All elements except inline elements and table columns (cells). height of the containing block.
clear [ none, left, right, both ]

This property says which sides of a box may not be next to an earlier floating box.

(See the CSS faq page, section on class and id for how to use the "class" below.)

p.under-image {
  clear: both;
}
<img src="images/dalai_lama.jpg" 
   alt="The Dalai Lama" />
<p class="under-image">
The image is set to "float: right".
Normally, the paragraph text will float to the 
side of the image.
So we use "clear" to make it go at the bottom. 
</p>
His Holiness

The image is set to "float: right". Normally, the paragraph text will float to the side of the image. So we use "clear" to make it go at the bottom.

... Block-level elements n/a
display [ block, inline, list-item, none ]

Remember that all boxes (html tags) are either block or inline.
With the display property we can make
block elements (such as <p>) display inline (side by side).
Or we can make inline elements (such as <a>) display
vertically (like blocks).

a {
  display: block;
  border-bottom: 1px solid #cccccc;
  background-color: #ffccff;
}
<a href="how_to_learn_css.html">How to learn css</a>
<a href="css_tutorial.html">CSS Tutorial</a>
<a href="how_to_fix_css_problems.html">How to fix css problems</a>
<a href="4-css_faq.html">CSS FAQ</a>
CSS Tutorial How to fix problems How to learn html HTML FAQ ... All elements. n/a
 
properties for List
selector name possible values example code example display default use in percentages
refer to
list-style-type [ disc, circle, square, decimal, lower-roman, upper-roman, lower-alpha, upper-alpha, none ]
ul {
  list-style-type: square;
}
<ul>
<li>
something
</li>
<li>
another thing
</li>
</ul>
  • something
  • another thing
disc li element, or any element with display: list-item n/a
list-style-image [ url(), none ]
ul {
   list-style-image: url(bullit1.gif);
}
<ul>
<li>
something
</li>
<li>
another thing
</li>
</ul>
  • something
  • another thing
none li element, or any element with display: list-item n/a
 
More information

This page has only the most common properties and their values. There are more!

  • This links page lists some good websites about css.
 
This is too hard!!

"I don't understand about 'values' and 'properties'."
"It's too much reading."

  • We write code. We read code. Coding is about writing and reading.
  • I am not teaching "easy" — i am teaching html/css for people who want to learn how to build real websites.
  • I promise you that php and mysql will be even harder.
  • The good news is, the concepts and ways of learning here, are used in programming!
  • But — if you don't want these things, then that is ok! This may not be the right thing for you. Better to realise that now than later, yeah?