Thursday, September 18, 2008

Code of creating vertical css menus

Copy and Paste the following code in your html page. This code is tested in IE6, IE7 and firefox.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

<style type="text/css">

/* individual menu item styling */


#main-menu ul {

list-style: none;

}


#main-menu li {

list-style: none;

/*padding-bottom: 5px;*/

/*border-bottom: 1px solid #d9dbdc;*/

/*margin-bottom: 10px;*/

}


#main-menu a {

/*float: right;*/

display: block;

color: #231f20;

text-decoration: none;

border-bottom: 1px solid #d9dbdc;

padding: 5px 0px 5px 0px;

}


#main-menu a:hover {

/*padding-bottom: 5px;*/

display: block;

color: #231f20;

border-bottom: 1px solid #000000 ;

background-color:#33CC33;

}


/* menu item styling on mouse over */


#menu a:hover {

color: #6666CC;

background-color:#33CC33;

}

</style>

</head>

<body>

<div id="main-menu">

<ul>

<li><a href="#">Home</a></li>

<div></div>

<li><a href="#">About Us</a></li>

<div></div>

<li><a href="#">Services</a></li>

<div></div>

<li><a href="#">Products</a></li>

<div></div>

<li><a href="#">Consultation</a></li>

<div></div>

<li><a href="#">Contact Us</a></li>

<div></div>

<li><a href="#">Search</a></li>

</ul>

</div>

</body>

</html>

Printing in CSS

CSS2 introduced a mechanism for closer control over printing web pages that are styled using style sheets. In this section, we'll look at how this works.

There are two aspects to consider with printing. The page, and the contents of the page.

For the page, we need to consider what size it is, whether it is oriented as a portrait or landscape, what printing marks might be required, and what margin it has.

For the content we need to worry about where pages break, and what is done with widow and orphan lines of text (we'll explain these terms below).

In CSS2, the page properties are defined by the @page rule, while several new properties help control page breaking.

The @page rule


At the heart of printing in CSS2 is the @page rule. This is similar to the @media rule, which you can learn about in the section on Media.

The @page rule defines a context for printing. Essentially it describes the paper sheet that the web page is going to be printed on.

@page rules work very much like the standard statements of a style sheet, though they take properties specific to pages that no other statement can use, and can also have margins specified using the familiar margin properties.

The properties of a page can be applied to every page, or differently to left and right hand side pages. When printing for bound publications, the margins on the left and right hand sides are usually different to take into account the center binding.


To specify properties for every page, the @page rule has the form @page {/* properties declared here */}

To specify the properties for left hand side pages, the @page rule has the form @page:left {/* properties declared here */}

To specify the properties for right hand side pages, the @page rule has the form @page:right {/* properties declared here */}


Named pages


@page rules can be named, which, in conjunction with the page property we will see below, allows particular elements to be printed on particular kinds of pages (that is pages set up in a particular way). For instance, its easy to set up an @page rule for printing figures. Suppose we want to print any figures in landscape format. We can set up a named page like this


@page figures {

size: landscape

}


The name of a page is simply placed after the @page keyword and before the properties for the rule. Below we will see how the page property tells the browser which @page rule to use to print an element.

Page specific properties


There are two sets of properties associated with how pages will actually be printed.


those that relate to the printed page itself, that is the margin properties. These can only be applied to @page rules. Here we will refer to these as Page properties.

Those that relate to how the content is displayed within those pages. These properties are the page-break-before, page-break-after, widows and orphans properties. These can be applied to any elements. Here we will refer to these as Page break properties.


margin, margin-left, margin-right, margin-top, margin-bottom


What it does

Margins are the only general properties from CSS that can be applied to pages.

The margin properties set the top, left, bottom, right and all margins respectively.

Possible values

Margins can be specified as either a percentage, a length or using the keyword auto.

Margins can be negative values.

Percentages


A percentage margin value sets the affected margin to that percentage of the width or height of the page. For instance, a margin-right: 20% sets the width of the right margin to 20% of the width of the page. margin-top: 20% sets the height of the top margin to 20% of the height of the page.

Length values


We cover length values in detail in our section on values. With length values, you can set the margin to an absolute value, or a value relative to the size of the element itself. Note that because a page has no concept of fonts, the em and ex units cannot be used to set the margin of a page.

Default values

If no margin value is specified, the margin of a page is zero.

Page breaking properties


When content is to be printed, we need to worry what happens about page breaks. Customarily, new sections begin on new pages. An author may want a page break inserted before a table or illustration.

CSS2 provides two properties which allow control over where the page will break when it is printed. These properties, page-break-before and page-break-after, can be applied to any element. They might be used to create a page break before headings of level one, or only headings of a certain class (for instance class="section").

A further problem when printing documents arises when only a few lines of a paragraph can fit at the bottom of a page (these are referred to as orphans) or only a few lines remain to be printed at the top of a page (these are referred to as widows). CSS2 provides two properties to specify what occurs with widow and orphan lines.

page-break-before

What it does


page-break-before specifies how or whether a page breaks before an element. You can specify that a page should not break before a certain element, or that it should, and that the next page should be a left or right hand page.

Possible values


page-break-before can take one of several keyword values.


auto Neither force nor forbid a page break before the element.

always Always force a page break before the element.

avoid Avoid a page break before the element.

left (For rendering to left and right pages.) Force one or two page breaks before the element so that the next page is formatted as a left page.

right (For rendering to left and right pages.) Force one or two page breaks before the element so that the next page is formatted as a right page.


Default values


If no value is specified, the page-break-before is set to auto.

Is it inherited?


An element does not inherit the page-break-before property of the element which contains it.

page-break-after

What it does


page-beak-after controls how or whether a page breaks after an element. You can specify that a page should not break after a certain element, or that it should, and that the next page should be a left or right hand page.

Possible values


page-beak-after can take one of several keyword values.


auto Neither force nor forbid a page break after the element.

always Always force a page break after the element.

avoid Avoid a page break after the element.

left (For rendering to left and right pages.) Force one or two page breaks after the element so that the next page is formatted as a left page.

right (For rendering to left and right pages.) Force one or two page breaks after the element so that the next page is formatted as a right page.


Default values


If no value is specified, the page-beak-after is set to auto.

Is it inherited?


An element does not inherit the page-beak-after property of the element which contains it.

page-break-inside

What it does


page-break-inside controls whether a page breaks inside an element. You can specify that a page should not break inside a certain element.

Possible values


page-break-inside can take one of two values.


auto Neither force nor forbid a page break inside the element.

avoid Avoid a page break inside the element.


Default values


If no value is specified, the page-break-inside is set to auto.

Is it inherited?


An element does not inherit the page-break-inside property of the element which contains it.

orphans

What it does


orphans specifies the minimum number of lines of an element that must be left at the bottom of a page when the page breaks. If fewer than this number of lines can be placed at the bottom of the page, the page breaks before the element and the entire element is printed on the following page.

Possible values


orphans takes an integer value. This value specifies the number of orphaned lines permitted.

Default values


If no value is specified, the orphans is set to 2.

Is it inherited?


An element does inherit the orphans property of the element which contains it.

widows

What it does


widows specifies the minimum number of lines of an element that must be left at the top of a page when the previous page breaks. If fewer than this number of lines can be placed at the top of the page, the page breaks before the element and the entire element is printed on the following page.

Possible values


widows takes an integer value. This value specifies the number of widowed lines permitted.

Default values


If no value is specified, the widows is set to 2.

Is it inherited?


An element does inherit the widows property of the element which contains it.

Media in CSS

An important assumption underlying CSS1 was that web pages were viewed using a personal computer with a monitor. But there are many more ways to access a web page than simply via computer and monitor. And each of these different media have their own characteristics. They may be monochrome, limited in size, they may be "aural" and read web page content aloud. They may be television based, like WebTV. And looking toward the future, we really have no idea what they will be like.

CSS2 provides a way to tailor a style sheet to the particular medium that is being used to display a page. Inside the same style sheet you can have different parts for printing, handheld devices, monitors and so on. Additionally, different style sheets can be imported depending on the medium used for browsing.

In this section we will take a look at how these work.

The @media rule


The @media rule is a subset of a style sheet specific to a particular type of medium, or group of media.

An @media rule specifies the medium or media to which it applies, then contains a number of statements, exactly like the statements we usually find in a style sheet. These statements apply only when a web page associated with this style sheet is being displayed using one of the media to which the @media rule applies.

@media rules are straightforward to construct. They comprise


the keyword @media

a list of media types, separated by commas

the statements to be applied to this group of media, contained inside curly braces "{" "}"


These statements are identical to the statements we have seen in previous sections.

For example, the following rule applies a white background and black text color to the body of page when it is being printed or when it is being displayed on a handheld device.


@media print, handheld {

background: white;

color: black

}


Types of media


CSS2 provides for the following media types, as described in the recommendation


all Suitable for all devices.

aural Intended for speech synthesizers.

braille Intended for braille tactile feedback devices.

embossed Intended for paged braille printers.

handheld Intended for handheld devices (small screen, monochrome, limited bandwidth).

print Intended for paged, opaque material and for documents viewed on screen in print preview mode.

projection Intended for projected presentations, for example projectors or print to transparencies.

screen Intended primarily for color computer screens.

tty Intended for media using a fixed-pitch character grid, such as teletypes, terminals, or portable devices with limited display capabilities. Authors should not use pixel units with the "tty" media type.

tv Intended for television-type devices (low resolution, color, limited scrollability).


Media specific importing using @import


In addition to the media rule, CSS2 allows conditional importing of style sheets based on the current medium. This means that a style sheet will only be imported when a page is being displayed in a particular medium. This is particularly useful for low bandwidth media, like handhelds, which generally are wireless based. With an imported style sheet based on the medium, only the statements specific to the particular medium need to be downloaded.

To conditionally import a style sheet based on the current medium, create a standard @import rule, but add the names of the media that will cause the style sheet to be imported directly after the rule in a comma separated list.

For instance, to import a style sheet called monochrome.css when a page is to be printed or displayed on a handheld device you would create the following @import rule.

@import url(monochrome.css) print, handheld

For more on @import rules, see the section of on types of rules in CSS.

Hints and suggestions


Media specific importing essentially achieves the same outcome as using the @media rule described above. The difference is that if you use the @import rule the user agent does not even have to download the imported style sheet unless it is relevant. This will be a very powerful technique for designing for technologies such as handheld and mobile devices. It will be very important for these devices to support the @import rule.

Generated content in CSS

If you cast your mind back you might recall our golden style sheets rule, separate appearance from content. Now, suddenly we are talking about generated content. You might be thinking, doesn't this introduce content into our style sheets? What about the golden rule? Well, it's a good question, and this area is one of controversy among style sheets experts. I won't buy into that debate here, but I want to cover the issue of generated content in CSS2 in this section.

Firstly, note that some kinds of generated content already exist in CSS1. Think about bulleted and numbered lists. These add numbering or bullets, so they do generate content to an extent. CSS2 adds more powerful ways of generating content. Here we learn how.

There are two aspects to generating content with CSS2.


Firstly we need to say where the content is to be inserted

Then we need to specify what the content to be inserted is


Where is the content to be inserted?

To answer this question we use the two new CSS2 pseudo classes we saw a little earlier. The :before and :after pseudo classes are appended to selectors, and allow the insertion of generated content before or after selected elements. For example, you might want to put something before all headings of level 1. The pseudo element selector to do this is h1:before.

What content is to be inserted?

To do this we use a new property, content.

The content property allows us to define the content that is to be inserted before or after selected elements. Content can comprise


strings of text

images and other resources

counters

keyword defined elements

the result of the unique attr() function (don't panic - more on this below!)


Let's take a look at the content property itself in detail, then we'll look at each of these different types of content as well.

content


What it does


The content property specifies the content to be added before or after an element selected by the :before and :after pseudo classes. It can only be used with these selectors.

Possible values


As mentioned above content can comprise a list of


strings of text

counters

keyword defined elements

the result of the unique attr() function


Let's look at each of these values in turn.

Strings of text

What it does

Surprisingly, using this value will insert a string of text before or after the selected element.

Syntax

Say we wanted to generate the content "Chapter " before every heading level 1.


h1:before

{content: "Chapter "}


All you need do then is place the string of text you want to be generated between double quote marks.

counter and counters

What it does

counter values are more than a little involved. Think of them as super-charged versions of numbered lists. Counters have a name, and a style. You create a counter using the counter function, then use them in conjunction with the content property to insert markers into a document. This isn't quite as confusing as it initially seems.

There are two forms of the counter function, the counter form and the counters form.

The counter function has the form counter(identifier, list-style). The identifier is the name of the counter whose value you want to insert, while the (optional) list-style specifies what style of counter you want (for instance lower-roman.) These styles are the same as those for elements of type list-style. When the browser encounters a counter function, it determines the value of that counter at this point of the document, then inserts it into the document in the nominated style. How does it determine this value? See the section on the counter property below for more detail and examples.

The counters form is very similar, but allows for multiple counters to be inserted simultaneously. The form is counters(identifier, string, list-style). This returns all counters with the name specified by the identifier at this point of the document in the given list style, separated by the string. For more on this rather sophisticated aspect of CSS2, with examples, see the counters property below.

Keyword defined elements

What it does

At the moment, keyword values are limited to allowing the insertion of various types of quote marks.


open-quote places the open quote string as defined by the quotes property for the elements

close-quote places the close quote string as defined by the quotes property for the elements

no-open-quote does not insert any content, but increments the level of nesting of the quotes

no-close-quote does not insert any content but decrements the level of nesting of the quotes


Basically it works like this. You use the content property with a keyword value to define, for example, that there should be an opening quote before a certain element. You then use the quotes property to define what type of quote marks you want to appear. If you don't define a value for the quotes property, the default is up to the user agent. This means that it may put in some sort of quote mark just because you have given content the value of open-quote, but it may not be exactly what you have in mind.

The attr() function

What it does

The last possible value for content, and unique to the content property is the attr() function. This returns the value of a named attribute of the selected element. The returned value is then inserted as a string. For example, with a link, the title of the link could be inserted after the link in the following way.


a:after {

content: attr(title)

}


This might be used in conjunction with an @media rule to specify that only when printing a page, the URL should be inserted after the link.


@media print

{a:after{

content: attr(href)

}

}


How is content added?


Now, after that long diversion, where we looked at the possible values that the content property can take, let's go back to just a couple more things about the content property itself.

Content is added as inline, block or marker elements. However the statement that creates the content can set the display of the content to be a block, or other kind of element, rather than an inline element. For example


body:after {

content: "The End";

display: block;

margin-top: 2em;

text-align: center;

}


places the text string "The End" at the foot of the page as a block, rather than as an inline element.

Default values


If no content is specified, the default value is an empty string.

Is it inherited?


An element does not inherit the content property of the element which contains it.

quotes


What it does


The actual quotation marks that will be drawn are defined by the new quotes property. This property allows different quotation marks to be created for different elements (for example when quoting different languages the correct quotation marks for that language will be inserted automatically.)

The quotes property specifies the quote marks to be used for different levels of quotation. It is used in conjunction with a keyword value for the content property we saw above. When content takes a value of open-quote and close-quote, the open and close quotes inserted are those defined by the quotes property.

Possible values


quotes are defined as a list of pairs of strings. Each pair defines respectively the opening and the closing quotation mark. The pairs are separated by spaces, as is each element of a pair. For example the following statements create different quotes for different languages, including what should happen when quotes are nested.


/* Specify pairs of quotes for two levels in two languages */

q:lang(en) { quotes: '"' '"' "'" "'" }

q:lang(fr) { quotes: "«" "»" "<" ">" }


In English, according to this statement, first level quotes are double quoted, while second level quotes are single quoted. In French, first level quotes use "«" and "»" while second level quotes use "<" and ">".

Default values


If no value for quotes is specified, the default value is determined by the browser.

Is it inherited?


An element inherits the quotes property of the element which contains it.

Counters and counter properties


counter


Counters are sophisticated and powerful, and not surprisingly more than a little complex. But be not afraid, a little thinking and they are manageable. Here's how they work.

Counters are used exclusively by the content property to insert numbering (e.g. "1.5") before or after an element. You don't need to "create" a counter, just by referring to them they come into existence. For instance, let's suppose you created the simple rule below

h1:before {content: counter(chapter)}

This tells the browser to add the value of the counter called "chapter" before every heading of level one. But what value is that? Well, if we don't set the value using the properties we will discuss in a minute, the counter value will be 0. So every heading of level one will be preceded by a 0. We don't really want that. We want something along the lines of the first heading to be preceded by a 1, the second by a 2, and so on. To do this, we use the counter-increment property. This increments the value of a counter by a given value (you can jump up by 2, down by 1 (using the value of -1), as well as simply incrementing by one.)

To use counter-increment, name the counter you want to increment using the identifier for the counter, and then the value you want to increment by. (Again, simply using the property "creates" a counter with that identifier if it doesn't already exist). In the following example, we increment the value of the counter with an identifier "chapter" for each <h1> element.

h1 {counter-increment: chapter 1}

You can of course combine the two rules into this simple rule


h1:before {content: counter(chapter);

counter-increment: chapter 1

}


This increments the counter then adds it before the <h1> element. Note the special case that if you increment and use a counter in the same rule, it is incremented before being inserted even if the content property comes before the increment property.

What does this let us achieve? Well, now, instead of marking up the headings of each chapter in our HTML based document like this


<h1>1. Abstract</h1>

...

<h1>2. Introduction</h2>


We leave out the numbering and it is done automatically. This aids the editing and writing processes, because moving sections, subsections and so on, or deleting them, or inserting new sections doesn't necessitate re-entering every number! It's particularly useful where a document might be served up in pieces based on the user's needs, or the medium they are using.

Let's look at a more complex example, using subsections. Here we will also take a look at the counter-reset property. Any nontrivial piece of documentation will almost certainly be divided into sections and subsections. We've seen how to number the top level sections (we called them chapters), but what about subsections? The particular problem is resetting the counter at the beginning of a new subsection.

Here is what we do


h1:before {content: counter(chapter);

counter-increment: chapter 1

/* this is the same as the example above */

counter-reset: subsection 0

}


In this example, we reset the counter called subsection to 0 each time we start a new section (assuming the h1 is the start of a new section). Now we need to increment the subsection counter for each subsection (heading of level 2), then insert the subsection content so that our subsections are correctly numbered. Let's take a look.


h2:before {content: counter(chapter) "." counter(subsection);

counter-increment: subsection 1

}


This increments the counter called subsection, then inserts numbering of the form "2.5" before the text of the heading.

counters


The last thing we need to learn about counters is a little more involved again. Think about lists in HTML. These can contain other lists, which can contain others, and so on ad infinitum. It's not possible in advance to know how many levels there will be, so how do we take care of this situation? Fortunately those smart people at the W3C thought of this, and provide a solution. It's a little tricky, but not rocket science. Counters have a particular "scope". When a counter is created its scope is the level at which it was created, and all that level's children. However, where a counter of the same name is created inside the scope of the initial counter, then this is treated as a separate counter, which now takes over the counting. Confusing? Let's take a look at an example, taken from the CSS2 specification.

Here's the style sheet rule that creates the counter

  ol { counter-reset: item }  li { display: block }  li:before { content: counter(item) ". "; counter-increment: item }

And here is the HTML

  <ol>                 <!-- (set item[0] to 0          -->      <li>item         <!--  increment item[0] (= 1)   -->      <li>item         <!--  increment item[0] (= 2)   -->        <ol>           <!--  (set item[1] to 0         -->          <li>item     <!--   increment item[1] (= 1)  -->          <li>item     <!--   increment item[1] (= 2)  -->          <li>item     <!--   increment item[1] (= 3)  -->            <ol>       <!--   (set item[2] to 0        -->              <li>item <!--    increment item[2] (= 1) -->            </ol>      <!--   )                        -->            <ol>       <!--   (set item[3] to 0        -->              <li>     <!--    increment item[3] (= 1) -->            </ol>      <!--   )                        -->          <li>item     <!--   increment item[1] (= 4)  -->        </ol>          <!--  )                         -->      <li>item         <!--  increment item[0] (= 3)   -->      <li>item         <!--  increment item[0] (= 4)   -->    </ol>              <!-- )                          -->    <ol>               <!-- (reset item[4] to 0        -->      <li>item         <!--  increment item[4] (= 1)   -->      <li>item         <!--  increment item[4] (= 2)   -->      </ol>              <!-- )                          -->

Each <ol> "creates" a counter called "item". Every list item inside the <ol> increments the counter, then inserts the number before itself. However, when we get to an <ol> inside another, a new counter called "item" is created. The other ones don't go away, they simply remain unchanged until their scope is again relevant. Note that they all have the same name. You might think this is a recipe for disaster, but there is a good reason for this.

Here's one last string to the counters and content bow. Imagine you want to insert the "full" number for every list item (for example 2.5.1). With the counter function you can only insert the value for one named counter. In this case it would be the value for the counter named item at the level you are currently at. How do we get all of the counters with that name which are relevant at the level we are at? With the counters function. We saw this in the section on content above, but let's revisit it. It has the form counters(identifier, string, list-style), (where the list-style is optional) and it does exactly what we want. It returns every counter in the document relevant at the point it is called, separated by the string you give, in the style specified. What do I mean by "relevant at this point?" Remember that the scope of a counter is inside the element in which it is created, including all of its children. Basically, a counter is relevant inside any of the children of the element which created it.

To cut a long story short, it's as simple as counters(item, ".")

counter-reset


What it does

The counter-reset property sets the value of a named counter.

Possible values

counter-reset takes the name of the counter to reset. Counter names are identifiers, and comprise letters, numerals and hyphens. They cannot, however, begin with numerals or hyphens. It also takes an optional integer, to which it sets the value of the identified counter.

counter-reset can also take a list of identifiers and optional integers, to set the value of several counters simultaneously. This is no mere luxury. Because of cascading, the following will set only the counter named counter2.


h1 { counter-reset: counter1 -1 }

h1 { counter-reset: counter2 99 }


In order to reset both, the following must be used.

h1 { counter-reset: counter1 -1 counter2 99 }

counter-reset can also be set to none.

Default values

If no counter-reset is specified, the default value is none.

Is it inherited?

An element does not inherit the counter-reset property of the element which contains it.

counter-increment


What it does

The counter-increment property increments the value of a named counter by a certain amount.

Possible values

counter-increment takes the name of the counter to increment. Counter names are identifiers, and comprise letters, numerals and hyphens. They cannot, however, begin with numerals or hyphens. It also takes an optional integer, by which it increments the value of the identified counter. If no integer is specified, the counter is incremented by one.

counter-increment can also take a list of identifiers and optional integers, to set the value of several counters simultaneously. This is no mere luxury. Because of cascading, the following will set only the counter named counter2.


h1 { counter-increment: counter1 -1 }

h1 { counter-increment: counter2 99 }


In order to reset both, the following must be used.

h1 { counter-increment: counter1 -1 counter2 99 }

counter-increment can also be set to none.

Default values

If no counter-increment is specified, the default value is none.

Is it inherited?

An element does not inherit the counter-increment property of the element which contains it.

Values in CSS

A glance at any of the properties sections of this guide will tell you that different properties can have different kinds of value.

You probably don't really need to know all of the following, but we include it for completeness. You might like to treat it as a reference, or read through it now to get an understanding of the basic issues, then refer for more detail when you need to.

The major kinds of value are:


length

percentage

color

url

keyword

shape


Any given property will be able to take some appropriate subset of the values described below. Sometimes they will not be able to take all aspects of a given type of value, for example, not all properties that can take length values can take negative as well as positive length values.

Note that any property can also take the value inherit. This forces a selected element to inherit the given property from its parent, even when that property is not usually inherited.

Length values


Length values can be positive or negative, have a numerical value, and must be followed by a unit of measurement. Note that while length values can be either positive or negative, several properties cannot have negative length units.

There are two basic kinds of length unit, relative and absolute. As a rule of thumb, absolute units should be used only when the physical characteristics of the output device are known. This means that units like points and pixels should be avoided for monitors (because we cannot be sure of the logical resolution (Macintoshes default to 72 dpi, Windows to 96 dpi, or screen sizes). We discuss some implications of this for design and implementation of web pages in our real world CSS section of the guide. As a starting point though, you should try to use relative length values wherever it is at all possible.

It is worth learning a little bit about each of these, as there are benefits and pitfalls in the use of each. The links section of the House of Style has pointers to a number of interesting discussions about the use of different units. A very thorough and useful coverage of these issues is given by Tod Fahrner. This is a must read for those who want to use style sheets correctly. We also discuss the issue of the appropriate use of various units in more detail in our real world CSS section.

Percentage values


Instead of using a unit of measurement many properties can also take percentage values. You'll remember that an element is contained within another element (for example, a <paragraph> will be inside the <body>). Usually percentage values specify a percentage of the parent.

For example, specifying a width property with a percentage value means the width of an element will be the given percentage of the element which contains it. For instance, p {width: 75%} means that paragraphs will be 75% the width of their container. Often this will mean the body element.

Percentages are very valuable with positioning, as when a user resizes their window, the same basic layout can be achieved for the new window size.

The form of a percentage unit is a positive or negative sign (no sign means positive units), followed by a number, followed by the percentage sign: %. For example -25%, +15% or 15%.

Color values


Backgrounds, text and borders can be assigned colors. Color values can be specified in one of three ways:


color keywords

hexadecimal colors

rgb colors


Color keywords


There are 17 color keywords, which have the following values.

These keywords are


Acqua aqua (00ffff)

Black black (000000)

Blue blue (0000ff)

Fuchsia fuchsia (ff00ff)

Gray gray (808080)

Green green (008000)

Lime lime (00ff00)

Maroon maroon (800000)

Navy navy (000080)

Olive olive (808000)

Purple purple (800080)

Red red (ff0000)

Silver silver (c0c0c0)

Orange orange (ffa500)

Teal teal (008080)

White white (ffffff)

Yellow yellow (ffff00)


CSS2 introduced 26 keywords for system colors. These specify colors that the user interfaces of various operating systems such as the MacOS or Windows 98 use for drawing objects. These colors are


activeborder Active window border.

activecaption Active window caption.

appworkspace Background color of multiple document interface.

background Desktop background.

buttonface Face color for three-dimensional display elements.

buttonhighlight Dark shadow for three-dimensional display elements (for edges facing away from the light source). Shadow color for three-dimensional display elements.

buttontext Text on push buttons.

captiontext Text in caption, size box, and scroll bar arrow box.

graytext Grayed (disabled) text. This color is set to #000 if the current display driver does not support a solid gray color.

highlight Item(s) selected in a control.

highlighttext Text of item(s) selected in a control.

inactiveborder Inactive window border.

inactivecaption Inactive window caption.

inactivecaptiontext Color of text in an inactive caption.

infobackground Background color for tooltip controls.

infotext Text color for tooltip controls.

menu Menu background.

menutext Text in menus.

scrollbar Scroll bar gray area.

threeddarkshadow Dark shadow for three-dimensional display elements.

threedface Face color for three-dimensional display elements.

threedhighlight Highlight color for three-dimensional display elements.

threedlightshadow Light color for three-dimensional display elements (for edges facing the light source).

threedshadow Dark shadow for three-dimensional display elements.

window Window background.

windowframe Window frame.

windowtext Text in windows.


Hexadecimal RGB colors


For whatever reason, the original web palette (now sRGB) used hexadecimal numbers (base 16 not base 10 numbers, and if you don't know, don't worry about it) for specifying colors. In short, these RGB colors are specified as a combination of Red Green and Blue. There can be 256 different hues of each color, (from 00 to ff - that's 0 to 255 in human speak).

CSS allows you to specify colors using this method, in the following forms:

1) The # symbol, followed by three hexadecimal numbers in the range 00 to ff. For example, #ffcc11. This is the way HTML developers know how to specify color. In this example, the red value is ff, the green value is cc and the blue value is 11.

2) The # symbol followed by three hexadecimal numbers, this time using a single digit. For example, #fc1. This is the same number as the one in the previous example, not the same as #f0c010.

Decimal RGB colors


Because humans are generally more comfortable counting to base 10 than base 16, CSS lets us use decimal rgb values. The color gamut (the total range of colors) is exactly the same, we are just specifying it in a different way.

There are two forms of this value, a numerical and a percentage form. They are interchangeable.

1) The numerical form of the color value is the following rgb(255, 255, 0). There are three numerical values, in the range from 0 to 255, one each for red, green and blue.

2) The percentage form of the rgb color value replaces the values in the range from 0 to 255 with a percentage value from 0 to 100%. For example, the above value would be written rgb(100%, 100%, 0%).

To recap, the following values are all equivalent:


green

#00ff00

#0f0

rgb(0, 255, 0)

rgb(0%, 100%, 0%)


URL values


Background images and list item images are specified with a URL. This url can be either absolute (that is, the full URL, e.g http://www.westciv.com/gifs/main.gif) or a relative path. But relative to what? If you were to think about it for a moment, you'd realize that it must be relative to the style sheet. Otherwise, the style sheet would only work for web pages in the same directory as it is. The problem is that Netscape Navigator 4.x gets this wrong, and treats the URL as relative to the web page! This is rectified in Netscape 6, but needs to be kept in mind when working with Netscape 4. This bug can actually be used to advantage to "hide" style sheets from Netscape 4, but have them accessible to any other browsers.

URLs have a straightforward form:

url(the url)

So, Absolute URLs are written like this:

url(http://www.westciv.com/gifs/main.gif)

Relative URLs, similarly, are written:

url(../gifs/main.gif)

Keyword values


Many different properties can take keyword values. A keyword value is simply a single word that is translated into a numerical value by a browser. The keywords available for a specific property will be appropriate to that particular property.

Some keyword values are relative (to the parent element). For example


bold and bolder for font-weight

small and smaller for font-size


Shape values


The uncommon shape value (it is only used currently for the clip property, introduced as part of CSS2) defines a shape. At present, there is simply a rectangle shape.

The form of the rectangle shape is:

rect( top left bottom right )

Each of top, left, bottom and right can be either a length value, or the keyword auto. auto means, at least in the clipping context, that the value of this position of the clip is the same as that of the element itself.

This value is probably going to be redefined because it doesn't follow the same basic model as the box model of CSS1, where element positions are defined by a top and left corner, along with a width and a height.

In addition, further shapes are planned.

User interface properties in CSS

CSS2 introduced user interface properties, which allow the shape of the cursor to be set based on the object that it is hovering over, and to define the outline of an element. When the cursor is inserted in a field, and keystrokes will be directed to that field, it is said to have the focus. It is usual for some kind of outline to indicate this fact.

CSS2 also introduced a series of color keywords which refer to system values such as the color of window borders. You can learn more about these in the section on color values.

Lastly, CSS2 introduced the system font concept, which allows you to specify fonts based on those used by the system for various kinds of object, such as menu items and the default text for window items. For more on this, see the section on text style properties.

cursor


What it does


This property sets the shape of the cursor when the mouse is hovering over a selected element.

Possible values


The cursor can be specified as either one of several keyword values, or as a URL that points to a file of type .cur (a cursor resource file).

The possible keyword values for the cursor property, as spelt out in the CSS2 specification are


auto The browser determines the cursor to display based on the current context.

crosshair A simple crosshair (e.g., short line segments resembling a "+" sign).

default The platform-dependent default cursor. Often rendered as an arrow.

pointer The cursor is a pointer that indicates a link.

move Indicates something is to be moved.

*-resize Indicates that the edge is to be moved. * is replaced with N, S, E, W, NE, NW, SE, SW for each of the edges (north, south, east, west and so on).

text Indicates text that may be selected. Often rendered as an I-bar.

wait A cursor to indicate that the program is busy and the user should wait. Often rendered as a watch or hourglass.

help Help is available for the object under the cursor. Often rendered as a question mark or a balloon.


Default values


The default cursor value is auto, which corresponds to the current way in which browsers draw cursors.

Is it inherited?


Child elements inherit the cursor of their parent elements. Remember, inherited properties can be overridden.

outline


What it does


This property sets the outline around an element, usually used to indicate that the element has the keyboard focus, that is it will receive keystrokes. Elements do not need to have the focus to have an outline, however, good user interface principles should be followed. As a general rule, users expect the outline to indicate that the outlined element has the focus.

This property is particularly helpful when designing forms for web pages.

Possible values


outline is a shorthand for three properties described below, outline-style, outline-width and outline-color. It takes a list of width, style and color values (as described for the individual properties below.)

Default values


The default value is the same as for each of the values described below.

Is it inherited?


Child elements do not inherit the outline of their parent.

outline-color


What it does


outline-color specifies the color to be used for drawing the outline around an element. Unlike a border, the color of each edge of an outline is the same.

Possible values


outline-color is specified by a color. We discuss color values in detail in our values section. In addition, the keyword invert can be used.

invert inverts each of the pixels in the border. This ensures that an outline is visible regardless of background color.

Default values


If no color is specified for the outline, the outline is invert, which means the outline will be the inverted color of the background.

Is it inherited?


The outline-color of an element is not inherited from its parent element.

outline-style


What it does


In addition to width and color, the outline of an element can have a style. There are several possible styles.

Possible values


Outline styles are specified using keywords.

The outline-style keywords are


none

dotted

dashed

solid

double

groove

ridge

inset

outset


Default values


If no outline-style property is specified, the style of an outline is none.

Is it inherited?


Elements do not inherit the outline-style of their parent elements.

outline-width


Possible values


Outline widths can take either keyword values, or length values.

The keyword values that can be used are


thin

medium

thick


There is no specification for what these keywords mean, so different browsers will probably draw them as different thicknesses. The only guarantee is that thin is not thicker than medium which is no thicker than thick.

Length values are discussed in detail in our section on values. Using relative length values you can create borders that are in proportion with the size of an element, or its contents.

Default values


If no outline-width is set, the thickness of an outline is medium.

Is it inherited?


An element does not inherit the outline-width of its parent element.

Element type properties in CSS

If you have delved deeply into HTML, you will know that there are a number of different types of element. The main types are inline, block and list item. By default, all elements are one of these types. For example, a paragraph is a block element, while a link is an inline element. With CSS, your style sheet can redefine the display type of an element. So, if you wish, you can make a paragraph display as an inline element, or link display as a block.

In addition, you can specify the display properties for different types of element. For example you can set the type of bullet for a list item.

The element type properties are:


display

white-space

list-style-type

list-style-image

list-style-position

list-style


CSS 2 introduced a number of new properties which control how tables and their captions are drawn.


border-collapse

border-spacing

caption-side

empty-cells

table-layout


display


What it does


The display property determines how an element will be drawn on the page. We discuss the basic ways an element can be drawn (block, inline and list-item) more fully in the Type Selector section. There are also a number of more complex values that essentially mean you can make any element display as if it were one of the elements associated with tables.

Possible values


The display property takes one of several keyword values, the core of which are


none

block

inline

list-item

inline-block

run-in


A value of block, inline or list-item mean that the element will have the specified display type, regardless of the kind of default display that the HTML specification gives this kind of element. As we saw above, this means we can make a paragraph an inline element, or a link a block element. The value inline-block is a little bit different. It means that an element will be drawn as a block box, but this box is flowed as if it were inline. So it will end up looking like a replaced element, for example an image. The run-in value creates either block or inline boxes, depending on context. Then, properties will apply to run-in boxes depending on whether their final status is inline or block.

A value of none means that the element will not be visible, nor will it have any effect on the layout of the page. Essentially it is not there. Compare this with the visibility property, where an element is not visible, but is taken into account by the browser when it is laying out the page.

CSS 2 introduced several new values for display related to tables. In CSS 2 any element (say a paragraph) can be a table or part of a table. With HTML this may not make much sense (as there is already the <table> element, table columns, cells, etc.) However, other languages, such as XML, do not have such elements. In those languages, CSS tables become absolutely necessary for displaying tabular data. The table related values for display are


table - a 'table' is the outer container for all tables. It defines a rectangular block element that contains any number of row groups, rows, column groups, columns, and/or captions, all arranged in an irregular grid pattern. Any rendering objects other than these should be displayed outside the rectangular grid. (HTML analog: table)

inline-table - an 'inline-table' is identical to a 'table' element, except that it is treated as an inline replaced element in the context surrounding the 'inline-table' element.

table-column-group - a 'table-column-group' is a container for a number of table columns. This allows the designer to set presentation properties on a group of columns, for example by placing borders around a group of columns. (HTML analog: colgroup)

table-column - a 'table-column' is a grouping of all cells in a particular vertical column. (HTML analog: col)

table-row-group - a 'table-row-group' is a container for a number of table rows. This allows the designer to set presentation properties on a group of rows, for example by placing borders around a group of rows. (HTML analog: tbody)

table-header-group - A 'table-header-group' is a special type of 'table-row-group' that is always displayed at the top of the table, above all other rows and row-groups, but below any captions. (HTML analog: thead)

table-footer-group - A 'table-footer-group' is a special type of 'table-row-group' that is always displayed at the bottom of the table, below all other rows and row-groups, but above any captions. (HTML analog: tfoot)

table-row - a 'table-row' groups all cells in a particular horizontal row. (HTML analog: tr)

'table-cell' - a 'table-cell' is the rectangular object to be arranged in the table grid. Table cells are only allowed inside rows. (HTML analog: td)

table-caption - a 'table-caption' is a special type of table cell that appears as a row or column of its own in a table. (HTML analog: caption)


Default values


While the theoretical default value of display is inline, browsers essentially treat an element as having the recommended display type if no display type is specified. These recommendations are part of the HTML specification.

Is it inherited?


An element does not inherit the display value of its parent.

Hints and suggestions


Note that browsers are permitted to ignore the display property.

white-space


What it does


The white-space property applies only to block elements, and specifies what should be done by the browser with extraneous whitespace (tabs, returns, extra spaces and so on). Whitespace characters in HTML should not affect the appearance of a web page, rather, browsers should ignore returns and tabs, and collapse more than one whitespace character to a single space for display purposes. With the white-space property, you can specify how the browser in fact handles whitespace characters.

Possible values


The white-space property can take one of the following keyword value: normal, pre, and nowrap.

A white-space of normal means that whitespace will be handled in the traditional manner. Any tabs, returns and extraneous spacing will be ignored.

A white-space of pre keeps all whitespace as it appears in the element. This is the equivalent to the HTML element <pre>. Essentially, the element is treated as being preformatted.

A whitespace of nowrap means that the contents of the element will only wrap (break to a new line) when an explicit line break - <br>- is in the contents. The content of the element will not wrap to a new line simply because the line does not fit in the page horizontally.

Default values


If no white-space value is specified, the white-space of an element is normal.

Is it inherited?


An element does inherit the white-space value of its parent element.

list-style-type


What it does


The list-style-type property lets you determine what marker (or bullet point) if any is associated with a list item.

Possible values


The list-style-type takes one of the following keyword values:


none


specifies that there should be no bullet beside list items


disc

circle

square


specify three different kinds of list item bullet.

There are also numerous different numbering systems for ordered list items.

Simple


decimal

decimal-leading-zero

lower-roman

upper-roman

lower-alpha

upper-alpha

lower-greek

lower-alpha

lower-latin

upper-alpha

upper-latin


Advanced


hebrew

armenian

georgian

cjk-ideographic

hiragana

katakana

hiragana-iroha

katakana-iroha


The CSS2 specification specifies these different numbering systems as follows


decimal (decimal numbers, beginning with 1)

decimal-leading-zero (decimal numbers padded by initial zeros [e.g., 01, 02, 03, ..., 98, 99])

lower-roman (lowercase roman numerals [i, ii, iii, iv, v, etc.])

upper-roman (uppercase roman numerals [I, II, III, IV, V, etc.])

hebrew (traditional Hebrew numbering)

georgian (traditional Georgian numbering [an, ban, gan, ..., he, tan, in, in-an, ...])

armenian (traditional Armenian numbering)

cjk-ideographic (plain ideographic numbers)

hiragana (a, i, u, e, o, ka, ki, ...)

katakana (A, I, U, E, O, KA, KI, ...)

hiragana-iroha (i, ro, ha, ni, ho, he, to, ...)

katakana-iroha (I, RO, HA, NI, HO, HE, TO, ...)


Alphabetic systems are specified with:


lower-latin or lower-alpha (lowercase ascii letters [a, b, c, ... z])

upper-latin or upper-alpha (uppercase ascii letters [A, B, C, ... Z])

lower-greek (lowercase classical Greek alpha, beta, gamma, ... [έ, ή,ί, ...] - the characters in the preceding brackets may not display correctly)


Default values


If no list-style-type value is specified, a list item has a list-style-type of disc.

Is it inherited?


A list item inherits the list-style-type value of its parent.

Hints and suggestions


Note - only elements with a display property value of list-item can have list-style-type properties.

list-style-image


What it does


CSS lets you specify an image by URL as the marker (bullet point) for a list item. list-style-image specifies this image by URL.

Possible values


A list-style-image can be specified as either a URL, or by the keyword none. Our section on values details URL values.

Default values


If no list-style-image value is set, an element has a list-style-image of none.

Is it inherited?


An element does inherit the list-style-image of its parent. To stop an element inheriting the list-style-image of its parent, set its list-style-image to none.

Hints and suggestions


As with other list style properties, only elements with a display value of list-item (explicitly set, or as recommended) may have a list-style-image value.

list-style-position


What it does


Lists can be drawn either with the contents of the element wholly indented (this is a list-style-position of outside), or with only the first line indented, and subsequent lines with the same left alignment as the marker (a list-style-position of inside).

Possible values


list-style-position can be specified as either of two keywords, inside and outside.

A position of inside makes the second and subsequent lines of a list item left aligned with the marker, not the left of the first line of text.

A position of outside is the traditional way for list items to be aligned. The text on second and subsequent lines aligns with the left of the text on the first line.

Default values


If no list-style-position is specified, the position is outside.

Is it inherited?


An element inherits the list-style-position of its parent.

Hints and suggestions


Remember, as with other list-style properties, list-style-position only applies to elements with a display value of list-item.

list-style


What it does


list-style is a shorthand property that allows you to specify and set all list style property values with one property.

Possible values


Any of the values that are possible for the list style properties in this part are permissible for list-style.

For example, the following


list-style-type: disc

list-style-image: url(../gifs/bullet.gif)

list-style-position: inside


is equivalent to the single

list-style: disc url(../gifs/bullet.gif) inside

Default values


For the default values of each list style property, see the individual properties above.

Is it inherited?


Each list-style property is inherited from its parent element.

Hints and suggestions


As with all list style properties, the list-style shorthand applies only to elements with a display value of list-item or which are part specified of the HTML recommendation as list items.

border-collapse


What it does


border-collapse applies only to elements which have a display value of either table or inline-table. This means they must either be <table> elements or elements with a display property set to the value table or inline-table.

There are two common models of table border behavior. border-collapse specifies how adjacent borders are drawn in a table.

In one model adjacent borders of table elements collapse into a single border. This will be familiar from most word processing applications.

In the other common model borders are drawn for each cell, and cell spacing separates the borders. This is how table borders are drawn by default in HTML.

Possible values


border-collapse takes one of two values: collapse and separate.

collapse specifies that the table should be drawn using the first model described above.

separate specifies that the second model should be used.

Default values


If no border-collapse is specified, it is treated as being separate.

Is it inherited?


An element does not inherit the border-collapse of its parent.

Hints and suggestions


See our Tables and CSS section for more on implementing this property in conjunction with other table properties.

border-spacing


What it does


border-spacing applies only to elements which have a display value of either table or inline-table. This means they must either be <table> elements or elements with a display property set to the value table or inline-table.

This property specifies the spacing between adjacent cells of the table.

Possible values


border-spacing can take either a single length value or a pair of length values.

A single length value specifies the same spacing between each edge of each adjacent cell in a table.

With two length values, the first specifies the horizontal spacing between adjacent cells, while the second specifies the vertical spacing between adjacent cells.

Default values


If no border-spacing is specified, the initial value is 0.

Is it inherited?


An element does inherit the border-spacing of its parent.

Hints and suggestions


See our Tables and CSS section for more on implementing this property in conjunction with other table properties.

caption-side


What it does


When you create an HTML table you should provide a label for this table using the <caption> element. This element should be placed inside the <table> element, immediately after the start tag. This way, non-visual user agents will have no doubt about the nature of the table's content. But obviously there's nothing here about where the caption should actually be drawn by visual user agents, such as browsers. The caption-side property then allows you to specify where this table caption should be drawn, with respect to the table itself. Note though that the <caption> element always remains an inline element, inheriting (inheritable) characteristics of the table which contains it.

Possible values


The caption-side property can take the following values, the effects of which should be pretty self explanatory.


top

bottom


Default values


The default value for this property is top - so if you simply place a <caption> element inside your <table>, and don't specify a caption-side property in the style sheet, the caption will be drawn as a block element above the table.

Is it inherited?


The caption-side property is inherited.

empty-cells


What it does


The empty-cells property can be applied to a <td> elements when their containing table has the property border-collapse: separate applied to it. empty-cells allows you to specify whether borders and backgrounds will be drawn for cells which have no visible content. This includes


empty cells

cells with visibility: hidden


Possible values


empty-cells can take the following values.


show - borders will be drawn around empty cells

hide - no borders will be drawn around empty cells


Default values


If no empty-cells is specified, the initial value is show.

Is it inherited?


An element does inherit the empty-cells of its parent.

table-layout


What it does


There are two possible ways in which a browser can determine how to layout a table. The first is to ignore any content, and simply used the widths and heights specified for rows, cells and columns.

The second, which is much slower, is to recalculate the row and column and cell sizes based on their content. This can take numerous 'passes' through the content to fully calculate the table layout. This is how browsers usually draw HTML tables.

The table-layout property enables the specification of which of these approaches is taken to laying out a table.

Possible values


table-layout takes one of two values: auto and fixed.

fixed specifies that the table should be drawn using the first model described above.

auto specifies that the second model should be used.

Default values


If no table-layout is specified, it is treated as being auto.

Is it inherited?


An element does not inherit the table-layout of its parent.

Page layout properties in CSS

Introduction to positioning with CSS


One of the most significant limitations of HTML has been the inability to lay pages out in the same way they are laid out on paper. It can be possible, although not always, to lay out pages using tables and transparent 1 pixel gifs, but this both violates our principle of separating content from appearance, and is guaranteed to cause maintenance headaches until the end of time, as new browser versions fail to lay out the pages exactly as they worked in older browsers. This method really is something to be avoided, and now that we have page layout properties courtesy of CSS, there really is no excuse for using it.

So how are page layout and style sheets related? The original cascading style sheets recommendation included very little related to page layout. With CSS1


the width and height of an element could be specified

margin and padding could be specified on an element

text-indent could be applied to the first line of an element

float and clear provided the means to create columnar layout (up to a point)


Definitely lacking from this list is the ability to specify where the top or left hand side of an element should be, either relative to where it would otherwise be in the flow of the document, or in relation to the top left hand corner of the page.

To address this lack of positioning, CSS2 provided far more sophisticated positioning properties. We cover all these, as well as some background concepts and terms here.

The big picture


So what is missing from HTML when it comes to page layout? Let's think about desktop publishing for a moment. Software like Quark Express and PageMaker (and even less powerful page layout applications) generally allows the user to take blocks of content (text and or images) and place them directly on a page at any location. Blocks can overlap one another, and the front most block obscures blocks underneath it. With HTML, we can't do these things.

CSS2 provides the ability to do these kinds of "page layout" things.

A note about "layers"


Before we continue, it's best that we address an important source of misunderstanding, namely the idea of layers. Most developers will have heard of layers, and many will have developed some erroneous ideas about them. Let's try to clarify matters a little.

In the simple sense, there is no such thing as a layer. In a more complex sense, a layer is a metaphor, but layers themselves don't exist. But if you include Netscape's proprietary extensions to HTML, then yes Virginia, there is such a thing as a layer. Let me try and explain all this mess.

When we use the term layer, we generally mean an HTML container (or element) that can be placed at some specific location on a web page. Applications such as DreamWeaver use the term in this way. The first confusion that arises about layers in this sense is that it's often thought that a layer is a div and can only be a div . This isn't so. A layer in the sense of an HTML element placed somewhere on a web page can be any kind of element (a paragraph, a list item, whatever). Well at least in theory. In practice, Netscape Navigator 4 will let you position paragraphs and some other elements using CSS2, while Internet Explorer 4 and 5 really does think that a layer is a div. But more of this later.

So, hopefully we've cleared up the first and second causes of uncertainty. There is no such thing as a layer HTML element, rather, layer is a term for any HTML element that is positioned on a web page using CSS2.

However, if you take Netscape's word for it, there is such a thing as an HTML layer, namely the <layer> element. This was a Netscape extension to HTML, that has never been adopted by Internet Explorer, and is certainly not recommended HTML. Simply ignore it, and it should go away. Indeed it has already done so, as Netscape 6 does not support this element.

Having hopefully cleared up these possible stumbling blocks, let's get down to understanding how positioning works.

And <div>s?


The <div> is the generic block element. You can place other elements such as <paragraph>s inside <div>s, and then the whole block can easily be positioned on the page. But just because you use a <div> does not mean you have to give this <div> position properties at all. And, on the other hand, you don't need to use a <div> in order to use positioning - you can use positioning for any HTML element. Note though that some older browsers only supported positioning properties when they were applied to the <div> element.

Ways of positioning


The CSS2 recommendation provides a model for how elements can be positioned. Essentially there are four different ways in which an element can be positioned on a page, with a couple of complicating factors.

Static positioning


Static positioning is easy. It's how pages are already laid out by a web browser. A web browser takes an HTML file, parses it into its elements, applies style from a style sheet (or if there is none, from a default style sheet) and then "flows" the elements onto the page. The position that an element, say a paragraph, has in this flow is its static position. We'll need to remember static positioning when we get to relative positioning below.

Absolute positioning


Absolute position is what we are all so excited about. In a nutshell, it lets a developer say where the top left hand corner of an element should be located with respect to its parent element. It is not with respect to the window. Don't get absolute positioning confused with relative positioning, which we'll look at below.

Fixed positioning


Fixed position is closely related to absolute position. When an element is absolutely positioned, it's positioned with respect to the element which contains it. When the page is scrolled, the element also scrolls. Wouldn't it be nice if we could position elements with respect to the top of the window, so that regardless of how the page is scrolled, the elements remain fixed? You guessed it, fixed positioning provides precisely this feature. With fixed positioning and the <object> element in HTML 4, we can do away with frames forever, yet gain all the advantages, with none of the disadvantages of the FRAME construct.

Relative positioning


Relative positioning is probably a little unfortunately named. Positioning relative to what? A lot of people jump to the conclusion that relative positioning is when you specify a position with respect to the parent element, and absolute positioning is when the position is specified with respect to the top left hand corner of the page. But as we have just seen, this is not how it works at all.

OK, so what is relative? In essence, relative positioning places an element with respect to where it would statically be positioned. When you relatively position an element, as a developer you are saying to a browser: "hey, take this paragraph, and put it 10 pixels down and 10 pixels to the right of where it would normally be."

There is another complicating factor. What if we have a relatively positioned parent element, with an absolutely positioned child element? By analogy with absolute positioning you'll probably guess that the absolutely positioned child element will be placed with respect to the top left hand corner of the parent element.

The issue of positioning isn't particularly difficult, I trust, but it is a bit tricky. I hope this irons out the issue of the different kinds of positioning available in CSS2. Below we'll look at how to put these into practice.

Positioning enables very sophisticated page layout, but as with any powerful technology, it is complicated at times.

The page layout properties are:


position

top

left

bottom

right

width

min-width

max-width

height

min-height

max-height

z-index

visibility

overflow

float

clear

clip


position


What it does


If you want to position an element you have to specify not just a set of coordinates that say where you want it to be, you also have to specify what these coordinates are with respect to. The position property is used to determine what the coordinates are with respect to, or how the element will be drawn. The actual position (the where) is specified using the top, left, bottom and right properties which we cover below.

Possible values


Elements can be positioned in one of four ways: statically, relatively absolutely or fixed. The value is specified by one of four keywords: static, relative, absolute and fixed.

These values are explained in detail above, but to recap:

When the position of an element is static, the browser will draw the element in the usual place. Essentially, this is the traditional positioning used with HTML.

When the position of an element is relative, it is placed with respect to its natural position, that is, with respect to where it would be if statically positioned.

For example, with a relative position, a top of 20px and a left of 20px (we'll look at the top and left properties very shortly), an element is placed 20px to the right and 20px down from where it would naturally, or statically be located.

With absolute positioning, an element is placed in a specified location with respect to its parent element. If the parent element is the <body>, the location of the element may appear to be with respect to the window. However, when the page is scrolled it should be apparent that it is not: the element will move so it is no longer (for example) 20px from the top of the window. However, it is in fact still 20px from the top of the <body>.

fixed positioning is different. Here the element really is positioned with respect to the window. This means if the page is scrolled, the element remains onscreen, fixed with respect to the top and left of the window.

Applications like Dreamweaver and GoLive create <div>s with absolute positioning, plus values for top and left, width and height, to build page layout. This is because, as you'll see in the browser support information on this page, for a long time this was the only way you could use CSS positioning and expect support in even the most recent browsers. The level of support, however, has improved.

Default values


When no position property is specified, an element has a static position.

Is it inherited?


An element does not inherit the position property of its parent.

Hints and suggestions


Take some time to consider the differences between the different types of positioning.

top


What it does


The top property specifies where the top of an element will be placed. Remember, though, to position an element you must also specify a value for its position property.

top is an offset from either


the top edge of the natural location of the element (for position: relative)

the top of the parent element (for position: absolute)

the top of the window (for position: fixed)


See position above for more on what the individual values mean.

Negative top values move an element up the page, positive top values move the element down the page.

For elements with a position: static, top has no effect.

Possible values


The top of an element can be specified using length values, percentage values, or using the auto keyword.

Length values

When the top is specified using a length value, the element is offset up or down the page with respect to a location determined by the position value of the element.

We cover length values in detail in our section on values.

Percentage values

When top is specified as a percentage value, the element is offset up or down the page by this percentage of the height of its parent element.

We cover percentage values in detail in our section on values.

Auto

A top of auto places the element with no offset up or down the page.

Default values


If no top is specified, the top of the element is auto.

Is it inherited?


An element does not inherit the top of its parent, however, an element may be affected by the positioning of its parent element.

Hints and suggestions


top specifies where an element appears in conjunction with the position property, which specifies how the element will be positioned with respect to its parent or the page. You will need to have a fair understanding of how the position property works to ensure that you achieve your desired outcomes. We cover the position property in detail above.

left


What it does


The left property specifies where the left of an element will be placed. Remember, though, to position an element you must also specify a value for its position property.

left is an offset from either


the left edge of the natural location of the element (for position: relative)

the left of the page or the left of the parent element (for position: absolute)

the left of the window (for position: fixed)


See position above for more on what the individual values mean.

Negative left values move an element leftwards along the page, positive left values move the element rightwards.

For elements with position: static, left has no effect.

Possible values


The left of an element can be specified using length values, percentage values, or using the auto keyword.

Length values

When left is specified using a length value, the element is offset to the left or to the right along the page from a location determined by the position value of the element.

We cover length values in detail in our section on values.

Percentage values

When left is specified as a percentage value, the element is offset to the left or to the right along the page by this percentage of the width of its parent element.

We cover percentage values in detail in our section on values.

Auto

A left of auto places the element with no offset leftwards or rightwards on the page.

Default values


If no left is specified, the left of the element is auto.

Is it inherited?


An element does not inherit the left of its parent. However, the left of an element can be affected by the position of its parent.

Hints and suggestions


left specifies where an element appears in conjunction with the position property, which specifies how the element will be positioned with respect to its parent or the page. You will need to have a fair understanding of how the position property works to ensure that you achieve your desired outcomes. We cover the position property in detail above.

bottom


What it does


The bottom property specifies where the bottom edge of an element will be placed. Remember, though, to position an element you must also specify a value for its position property.

bottom is an offset from either


the bottom edge of the natural location of the element (for position: relative)

the bottom of the parent element (for position: absolute)

the bottom of the window (for position: fixed)


See position above for more on what the individual values mean.

For elements with a position: static, bottom has no effect.

Possible values


The top of an element can be specified using length values, percentage values, or using the auto keyword.

Length values

When the top is specified using a length value, the element is offset up or down the page from a location determined by the position value of the element.

We cover length values in detail in our section on values.

Percentage values

When bottom is specified as a percentage value, the bottom edge of an element is offset up or down the page by this percentage of the height of its parent element.

We cover percentage values in detail in our section on values.

Auto

A bottom of auto places the element with no offset up or down the page.

Default values


If no bottom is specified, the bottom of the element is auto.

Is it inherited?


An element does not inherit the bottom of its parent.

Hints and suggestions


bottom specifies where an element appears in conjunction with the position property, which specifies how the element will be positioned with respect to its parent or the page. You will need to have a fair understanding of how the position property works to ensure that you achieve your desired outcomes. We cover the position property in detail above.

right


What it does


The right property specifies where the right edge of an element will be placed. Remember, though, to position an element you must also specify a value for its position property.

right is an offset from either


the right edge of the natural location of the element (for position: relative)

the right of the parent element (for position: absolute)

the right of the window (for position: fixed)


See position above for more on what the individual values mean.

For elements with a position: static, right has no effect.

Possible values


The right of an element can be specified using length values, percentage values, or using the auto keyword.

Length values

When right is specified using a length value, the right edge of the element is offset to the left or to the right along the page from a location determined by the position value of the element.

We cover length values in detail in our section on values.

Percentage values

When right is specified as a percentage value, the right edge of the element is offset to the left or to the right along the page by this percentage of the width of its parent element.

We cover percentage values in detail in our section on values.

Auto

A right of auto places the right edge of an element with no offset leftwards or rightwards on the page.

Default values


If no right is specified, the right of the element is auto.

Is it inherited?


An element does not inherit the right of its parent.

Hints and suggestions


right specifies where an element appears in conjunction with the position property, which specifies how the element will be positioned with respect to its parent or the page. You will need to have a fair understanding of how the position property works to ensure that you achieve your desired outcomes. We cover the position property in detail above.

width


What it does


Not surprisingly, this property specifies how wide an element will appear on the page.

Possible values


width can be specified as a percentage, as a length value, or as auto.

Percentage values

When a width is specified as a percentage, its width is this percentage of the width of its parent. We cover percentage values in detail in our section on values.

Length values

A width can be specified as a length. Lengths are covered in detail in our section on values.

Auto

A width of auto is the default width of an item. This means it will be as wide as it needs to be to fully display its content.

Default values


If no value is set for the width of an element, its width is set to auto.

Is it inherited?


An element does not inherit the width of its parent.

Hints and suggestions


width was specified as part of the original CSS1 recommendation. It is modified in part by the CSS2 recommendation.

Using percentage values or relative length values such as em creates flexible pages that can be comfortably viewed on a wide range of devices.

min-width


What it does


This property allows you to specify a minimum width for an element. When the width of an element is calculated, should it be less than a value specified for min-width, the value for min-width will be used.

Possible values


min-width can be specified as a percentage, as a length value, or none.

Percentage values

When a min-width is specified as a percentage, its min-width is this percentage of the width of its parent. We cover percentage values in detail in our section on values.

Length values

A min-width can be specified as a length. Lengths are covered in detail in our section on values.

Default values


If no value is set for the min-width of an element, its min-width is set to none.

Is it inherited?


An element does not inherit the min-width of its parent.

max-width


What it does


This property specifies allows you to specify a maximum width for an element. When the width of an element is calculated, should it be greater than this property, max-width specifies the width of the element.

Possible values


max-width can be specified as a percentage, as a length value, or none.

Percentage values

When a max-width is specified as a percentage, its max-width is this percentage of the width of its parent. We cover percentage values in detail in our section on values.

Length values

A max-width can be specified as a length. Lengths are covered in detail in our section on values.

Default values


If no value is set for the max-width of an element, its max-width is set to none.

Is it inherited?


An element does not inherit the max-width of its parent.

height


What it does


The height property enables you to specify the height of an element in a number of ways.

Possible values


height can be specified as a percentage, as a length value, or as auto.

Percentage values

When a height is specified as a percentage, its height is this percentage of the height of its parent. We cover percentage values in detail in our section on values.

Length values

A height can be specified as a length. Lengths are covered in detail in our section on values.

Auto

A height of auto is the default height of an item. This means it will have only the height it needs to fully display its content.

Default values


If no value is set for the height of an element, its height is set to auto.

Is it inherited?


An element does not inherit the height of its parent.

Hints and suggestions


height was specified as part of the original CSS1 recommendation. It is modified in part by the CSS2 recommendation.

min-height


What it does


This property allows you to specify a minimum height for an element. When the height of an element is calculated, should it be less than a value specified for min-height, the value for min-height will be used.

Possible values


min-height can be specified as a percentage or as a length value.

Percentage values

When a min-height is specified as a percentage, its min-height is this percentage of the height of its parent. We cover percentage values in detail in our section on values.

Length values

A min-height can be specified as a length. Lengths are covered in detail in our section on values.

Default values


If no value is set for the min-height of an element, its min-height is set to 0.

Is it inherited?


An element does not inherit the min-height of its parent.

max-height


What it does


This property allows you to specify a maximum height for an element. When the height of an element is calculated, should it be greater than this property, max-height specifies the height of the element.

Possible values


max-height can be specified as a percentage, as a length value, or none.

Percentage values

When a max-height is specified as a percentage, its max-height is this percentage of the height of its parent. We cover percentage values in detail in our section on values.

Length values

A max-height can be specified as a length. Lengths are covered in detail in our section on values.

Default values


If no value is set for the max-height of an element, its max-height is set to none.

Is it inherited?


An element does not inherit the max-height of its parent.

z-index


What it does


Because CSS allows the absolute positioning of elements on a page, we need to take care of the situation where elements overlap. The "stacking order" is specified using the z-index property. It determines which elements will appear in front of others when they overlap.

Possible values


z-index is specified either by an integer, or by the keyword auto.

Integer Values

Where z-index is specified using an integer, given two elements with a common parent, the element with the higher z-index is in front of the one with the lower z-index.

There are however some complicating factors. An element's z-index is not applicable to the whole page, but only within that element's containment hierarchy. This means that the z-index of parent elements is important in determining which element obscures another. Let's try an example.

Suppose you have an absolutely positioned image with a z-index of 10 in a paragraph of z-index 1, and an image of z-index 2 in another paragraph of z-index 5. If the two images overlap, which obscures the other? In this case it is the image with the lower z-index, because its parent paragraph has the higher z-index. So the paragraph with the higher z-index obscures the one with the lower z-index, and as a consequence the contents of the paragraph with the higher z-index obscure the contents of the paragraph with the lower z-index, regardless of the z-indexes of the contents.

It's logical, if a little convoluted.

Another subtle aspect is the question of how to make the contents of an element be closer to the front of the page, or further from the front of the page than the element itself. When the z-index of an element is less than zero, then the parent element is closer to the front of the page, when the z-index of an element is more than zero, the element is closer to the front of the page.

Auto

When the z-index is specified as auto, the browser determines the place of an element in the stacking order according to a relatively complicated set of rules, but in a nutshell, it uses the order in which the elements appear in the HTML code of the page.

Default values


Where no z-index is specified, the z-index of an element is auto.

Is it inherited?


An element does not inherit the z-index of its parent element. However, the stacking of elements is relative to parent elements. That is, where we have two elements, both with child elements, then all of the child elements of the parent with the higher z-index are in front of all of the child elements of the other parent element.

Hints and suggestions


Although the rules associated with stacking seem complicated, they are in fact the intuitive way for stacking to work. If there are two elements, each with child elements, then it makes sense that all of the children of the front-most element would be in front of all of the children of the element behind.

This same rule can be applied recursively, so that within an element, if more than one child itself has child elements, then the children of the front-most child are all in front of the children of the other child. And so on ad infinitum.

visibility


What it does


While it might not at first seem to make much sense to make an element of a page invisible, in conjunction with Javascript, showing and hiding elements when a user clicks, or moves a mouse over an element, or even based on time, can be a powerful tool. The visibility property lets you make an element either visible or invisible.

Possible values


visibility can be specified as one of three keywords, visible, hidden and inherit.

A visibility of visible and hidden are straightforward.

A visibility of inherit specifies that an element should have the same visibility as its parent.

Default values


If no visibility is specified, an element has a visibility of inherit.

Is it inherited?


An element only inherits its parent's visibility if the element's visibility is set to inherit.

Hints and suggestions


An element with a visibility of hidden still affects the drawing and layout of a page. The page is still laid out as if the element were visible. If you would like page to be laid out as if the element were not there at all (and for the element to also be invisible) then the display property which we discuss elsewhere has a possible value of none, which has the described effect.

overflow


What it does


Because the width and height of elements can be set by an author, we may have the situation where an element is not big enough to fit its contents. What do we do when this occurs? For images, traditionally the image is scaled to fit the size of the element. CSS introduces the overflow property to let you specify how a browser should display an element where its contents do not all fit into its width and height.

Possible values


overflow can take the keyword values visible, hidden, scroll, and auto.

An overflow of visible means that the browser should increase the actual width and/or the height of the element to display all of its contents. If the element has a background color or image this arguably should not be extended behind the content.

An overflow of hidden means that the browser should 'clip' the contents, displaying only the contents which are visible within the specified width and height.

An overflow of scroll means that the browser should place scrollbars on the element whether or not the contents of the element have overflowed.

An overflow of auto means that the browser should add scrollbars as needed to enable users to scroll horizontally and/or vertically to show hidden contents of the element.

The last two values should be familiar to those who have worked with frames in HTML. Frames can have a scroll specified never, always and auto. These correspond to hidden, scroll and auto.

Default values


If no overflow is specified, the overflow of an element is visible.

Is it inherited?


An element does not inherit the overflow of its parent.

Hints and suggestions


Theoretically, with the overflow property, the need for frames is greatly reduced. Unfortunately, as of the time of writing, most browsers, particularly older ones, do not fully support the overflow property.

float


What it does


Of all the CSS positioning properties, float and clear are probably the two that most users will have had least exposure to.

float has a simple effect, but follows what can be a quite complicated set of rules. The effect of the float property is to take an element out of the flow and place it to the left or right of other elements in the same parent element.

Possible values


float can be specified by one of three keywords: none, left and right.

A float of none means that an element flows as it would naturally in the flow of its page.

A float of left means that an element is detached from the natural flow of the page, and is treated as a block element to the left of the rest of the contents of its parent element.

A float of right means that an element is detached from the natural flow of the page, and is treated as a block element to the right of the rest of the contents of its parent element.

Default values


If no float is specified, elements have a float of none.

Is it inherited?


An element does not inherit the float of its parent.

Hints and suggestions


Floating can be a tricky concept, although the basic effect is quite straightforward. Sadly too, float has only recently become well supported by browsers. In conjunction with clear, float can be used to make text and image flow around each other in the same way as they do in paper-based publications.

clear


What it does


The clear property can be used in conjunction with float to specify whether and or where an element allows floating alongside it. You can specify whether an element can have elements floated to its left, to its right, or at all. When an element does not allow floating on a side, where an attempt is made to float an element to that side, the element appears below the floated element.

Possible values


clear can be specified as any one of the keywords none, left right or both.

A clear of none means that an element allows floating on either side.

A clear of left means that an element does not allow floating on its left.

A clear of right means that an element does not allow floating on its right.

A clear of both means that an element does not allow floating on either it's left or its right.

Default values


If no clear is specified, an element has a clear of none, allowing floating on both sides.

Is it inherited?


An element does not inherit the clear of its parent.

Hints and suggestions


float and clear are properties that will become, when properly supported by browsers, very useful tools for creating pages which adapt to their readers' screens. Until recently, the support is limited and buggy, especially for complex cases. In conjunction with float, clear can be used to make text and image flow around each other in the same way as they do in paper-based publications.

clip


What it does


The clipping area is that part of an element that is actually drawn, regardless of the size or location of an element. By specifying an element's clip it is possible to show only part of the element at a time. I would classify this as one of the advanced features of CSS, with powerful but specialized uses.

Possible values


Currently, CSS allows the clip of an element to be specified as either a shape or by the keyword auto.

At present, the only kind of shape that is defined is the rectangle. We discuss the shape value more fully in our section on values.

A clip of auto means that the clipping area will match the extent of the element, that is its width and its height.

Default values


Where no value is specified, the clip of an element is auto.

Is it inherited?


An element does not inherit the clip of its parent, but see hints below for how the clip and the parent's clip work together to determine the actual region where the element will be drawn.

Hints and suggestions


While the clip specifies the clipping region for the element, the actual region in which the element is drawn is the intersection between the area in which the parent will be drawn and the clip of the element.

Note that the clip property can only be applied to elements that have a position value of absolute or fixed.

Padding properties in CSS

With cascading style sheets, any element can have padding. Padding is the space between the edge of an element (its border) and its contents. There can be padding to the left, top, bottom and right of any element. The padding properties are:


padding

padding-top

padding-left

padding-bottom

padding-right


padding, padding-top, padding-left, padding-bottom, padding-right


What it does


The padding properties set the size of the top, left, bottom, right and all paddings respectively.

Possible values


Padding can be specified as either a percentage, a length or using the keyword auto.

Padding can not take negative values. If you want to do this, you probably should be using the margin properties.

Percentages

A percentage padding value sets the affected padding to that percentage of the width of the parent element. For instance, a padding-right: 20% sets the width of the right padding to 20% of the width of the element which contains the element. This is true even for padding-top and padding-bottom.

Length values

We cover length values in detail in our section on values. Essentially though there are two types of length value. There are absolute values such as px. If you use one of these, the actual length of the padding will always be the same. You design much more flexible pages if you use a relative value, like em. If you use a relative value the size of the padding will always be proportional to the calculated size of the content of the element. This is to be encouraged.

Default values


If no padding value is specified, the padding of an element is 0. That is, there is no space between the edge of an element and the contents of the element.

Is it inherited?


An element does not inherit the padding property of the element which contains it.

Hints and suggestions


Padding can be used to achieve similar effects to the positioning properties and margins.

Margin properties in CSS

With cascading style sheets, any element can have a margin. A margin is the space between an element and the elements to its top, left, bottom and right. The margin properties are:


margin

margin-top

margin-left

margin-bottom

margin-right


margin, margin-top, margin-left, margin-bottom, margin-right


What it does


The margin properties set the size of the top, left, bottom, right and all margins respectively.

Possible values


Margins can be specified as either a percentage, a length or using the keyword auto.

Margins can be negative values.

Percentages

A percentage margin value sets the affected margin to that percentage of the width of the parent element. For instance, a margin-right: 20% sets the width of the right margin to 20% of the width of the element which contains the element. Note that this is true even for margin-top and margin-bottom.

Length Values

We cover length values in detail in our section on values. Essentially though there are two types of length value. There are absolute values such as px. If you use one of these, the actual length of the margin will always be the same. You design much more flexible pages if you use a relative value, like em. If you use a relative value the size of the margin will always be proportional to the calculated size of the content of the element. This is to be encouraged.

Default values


If no margin value is specified, the margin of an element is zero. That is, there is no space between an element and adjacent elements. Unfortunately most browsers do tend to set default margins on elements such as paragraphs. You can overcome this by specifically setting the margin for these types of element to 0.

Is it inherited?


An element does not inherit the margin property of the element which contains it.

Hints and suggestions


Margins can be used to achieve similar effects to the positioning properties which we discuss below and padding (we talk about padding next).

You can center an element in its parent, without using text-align on the parent element by giving the element a left-margin and a right-margin of auto.

Using percentage values for your margins is highly recommended for the design of flexible, accessible web pages. A percentage value for a margin ensures that the actual drawn size of the margin will always remain proportional to the size of the viewing portal.

Border properties in CSS

Using cascading style sheets any element may have a border, of varying widths, colors and styles. Elements can even have every border (top, left, bottom, right) a different style, width and color.

The border properties are:


border-width

border-top-width

border-right-width

border-bottom-width

border-left-width

border-color

border-style

border

border-top

border-right

border-bottom

border-left


Borders really are simple, but more than any other set of properties, they have numerous shorthand ways of setting values, which can at first appear to make them quite complicated. For this reason we've written Unscrambling the border mess below, to help you get started with an easy to understand way of creating borders.

Another result of the numerous shorthands is that the exact nature of the browser support for the different properties is difficult to express in one single place. Each of the properties has the usual information about how well they are supported individually. As well, though, we have some general information at the bottom of the page which clarifies (hopefully!) some of the more tricky issues involved in making borders actually work.

Unscrambling the border mess


As we've just seen, and a glance at the list of border properties will demonstrate, there is an almost bewildering array of border properties, many of which are shorthand ways of saying the same thing. For example, to give the top border a width, you could use any of border-top-width, border-width, border-top and border.

As a general rule, you can do everything there is to do with border properties using the following straightforward approach.

To define the same border properties for each side of the element, use the border property. For example, to give each side of an element the same thin, red solid border, we'd use the following declaration

{border: thin red solid}

To define a border property for a given side of an element, use the border-* property. For example, to give the top edge of an element a thin, red solid border, and the bottom edge a thick black dotted border we'd use the following declaration


{border-top: thin red solid;

border-bottom: thick black dotted}


It really is that simple. See below for details of what values you can give a border for its


width

color

style


For completeness, we discuss each of the possible properties and shorthands in turn.

border-width, border-top-width, border-left-width, border-bottom-width, border-right-width


What it does


These properties let you set the width of the respective edges individually or all at once.

Possible values


Border widths can take either keyword values, or length values.

The keyword values that can be used are


thin

medium

thick


There is no precise specification for how these keywords should be drawn, so different browsers will probably (and as of writing indeed do) draw them as different thicknesses. The only guarantee is that thin is not thicker than medium which is no thicker than thick.

Length values are discussed in detail in our section on values. Using relative length values you can create borders that are in proportion with the size of an element, or its contents.

Default values


If no width is set, the thickness of a border is medium. Note that this does not mean that all elements have medium borders unless otherwise defined, In order for an element to have a border, it must be given a border style. Once you give it a style, it will then have a default width of medium.

Is it inherited?


An element does not inherit the thickness of its parent element's border.

Hints and suggestions


The border-width property can be used to set the value of each edge separately. To do this, use more than one value for the property. You can use between one value and four values for the border-width property.


One value sets all borders to the same width.

Two values set the top and bottom edges to the first value and the left and right to the second.

Three values set the top edge width to the first value, the left and right edge widths to the second value, and the bottom edge width to the third value.

Four values set the top edge width to the first value, the left edge width to the second, the bottom edge width to the third and the right edge width to the fourth.


border-color


What it does


Unlike border widths which we have just seen, border-color is set using a single property. However, that doesn't mean you can't set each edge to a different color. As with the border-width shorthand property we just looked at, border-color enabled different edge colors by using more than one value.

Possible values


border-color is specified by between one and four color values. We discuss color values in detail in our values section.

You can use border-color to give different colors to each edge of an element.


One color value specifies the same color for each edge.

With two color values, the first value specifies the color of the top and bottom edges, the second specifies the color of the left and right edges.

With three color values, the first value specifies the color of the top edge, the second the color of the left and right edges and the third the value of the bottom edge.

With four color values, the four color values specify respectively the color of the top, left, bottom and right edges.


Default values


If no color is specified for the border, the border has the same value as the element itself, that is the value specified by the color property. Note that this does not mean that all elements have a default border of the same color as the color of their content. It means that if you set a border-style only, your border will be drawn with a default color the same as the color of the element.

Is it inherited?


The border-color of an element is not inherited from its parent element.

Hints and suggestions


As with border widths (and all border properties) support for different edges is far from perfect in Netscape prior to version 6.

border-style


What it does


In addition to width and color, the border of an element, or its individual edges can have a style.

border-style is the basic border property. In order for a border to be drawn at all, you must define a border-style. Once you define a border-style, you can if you wish let the other properties, border-width and border-color, be set at their default values. Generally, though, you will probably want to define all three aspects of your borders.

There are several possible styles, though not all are currently supported by major browsers.

Possible values


As with border-color, different border-styles can be assigned to different edges of a border using the same syntax we saw for colors.

You can use border-style to give different styles to each edge of an element.


One border style value specifies the same border style for each edge.

With two border style values, the first value specifies the border style of the top and bottom edges, the second specifies the border style of the left and right edges.

With three border style values, the first value specifies the border style of the top edge, the second the border style of the left and right edges and the third the value of the bottom edge.

With four border style values, the four border style values specify respectively the border style of the top, left, bottom and right edges.


The border-style keywords are


none

dotted

dashed

solid

double

groove

ridge

inset

outset


Rather than describe them, investigate each by way of example. You'll find that many appear the same in most browsers, because support for this property is not yet 100%.

Default values


If no border-style property is specified, the style of a border is none.

Is it inherited?


Elements do not inherit the border-style of their parent elements.

Hints and suggestions


As with other border properties, support for specifying different edges of a border is far from perfect in major browsers.

You should also keep in mind that some browsers do not support every kind of style. Usually, where a style keyword is not supported, the browser draws the border as solid.

border, border-top, border-left, border-bottom, border-right


What it does


As mentioned at the beginning of this section, there are confusingly many, and inconsistently different shorthand ways of specifying border values. These five properties let you specify with one property any or all of the border-style, border-width and border-color values for each edge respectively, or the entire border.

Possible values


The shorthand properties simply take the same possible values as the full properties. As with the long hand way of describing borders, if you only give a value for style the border will be drawn with a default width and color. To specify more than one property value with a shorthand property, simply separate the values by a space.

For example, to specify the color, width and style of the top edge of a border of an element you could use the following single property

{border-top: red thick ridge}

Default values


The default values of each type of property are discussed in the relevant sections above.

Is it inherited?


None of the border values of a parent element are inherited by child elements.

Hints and suggestions


Keep in mind that not all styles are supported by each browser, nor do all browsers fully support different values on different edges of each border.

Background properties in CSS

Background properties affect how the background of an element appears. Unlike with standard HTML, with CSS any element can have a background image or color. Background images can repeat vertically, horizontally, both (the traditional tiling effect of background images in the <body> of HTML pages) or not at all (enabling easy "watermark" effects.) The way the background changes when you scroll a page can also be determined with CSS. A background image can either scroll while the page scrolls (traditional HTML appearance) or remain fixed while the text scrolls. In short, CSS goes far beyond the background attributes of HTML.

The background properties are


background-color

background-image

background-attachment

background-repeat

background-position

background


background-color


What it does


background-color, not surprisingly, specifies the color of the background of an element. Any element can be given a background color with CSS.

Possible values


background-color is specified as either a color value, or as transparent.

For more on color values, see our section on values.

A background-color of transparent ensures that whatever is behind an element is visible, rather than obscured by the background of the element. This is particularly useful with positioned elements, which might overlap two or more elements.

Default values


If no background-color is specified, then the element has a transparent background.

Is it inherited?


background-color is not inherited by an element from its parent. However, not specifying a background-color, or setting it to transparent ensures that the color of the element or elements behind shines through.

Hints and suggestions


It is recommended that if you set a background-color for an element, you should also set its color. This ensures that there will not be an illegible clash between a text color set by user preferences against a background color set in your style sheet.

As mentioned in the Browser support notes below, Netscape 4.x had an irritating bug which meant that the background color did not completely fill the entire box of the element, only its content. So for example, if you set a background-color for headings, only the individual words in the heading would be backed by color, leaving unsightly gaps in between. There is a simple workaround for this though which needn't affect how the element will appear in any other browser. Give the selector for this element both a width and a border. You can use a width of auto if you don't want it to be any wider than its content. Likewise you can give a border-style of none, if you don't want the element to actually have a border. The problem in Netscape 4 should be cleared up by doing this.

background-image


What it does


With traditional HTML based web pages, only an entire page can have a background image. Some browsers allow some elements, for example table cells, to have background images, but this is non standard.

CSS lets any element have a background image. As we will shortly see, the background image can be used in more ways than the simple tiled backgrounds of traditional web pages.

Possible values


background images are specified as either a URL of the image to be used for the background, or by the keyword none.

For more detail on URLs see the section in the discussion on values.

Default values


If no background-image is specified, an element has a background-image of none.

Is it inherited?


background-image is not inherited from the parent element. Which makes a lot of sense if you think about it.

Hints and suggestions


The URL for the background-image is relative to the style sheet, not to the HTML document. While this ought to be clear when you think about it, Navigator 4 does not work correctly, treating the URL relative to the HTML document. This is fine if the style sheet is embedded, but doing this defeats several of the major purposes of style sheets.

There's a simple solution though if you do choose to use a linked style sheet: in the style sheet specify the location of the background image using an absolute URL.

It is also recommended that you specify a background-color as well as a background-image. This ensures that if the image is unavailable (or images are off by the user), the foreground text color does not blend with the background, and text remains legible.

background-attachment


What it does


With traditional background images in the body of HTML documents, when the page is scrolled, the image scrolls too. With background images and CSS, you can specify that the background image should not scroll, but rather remain fixed as the page scrolls.

Possible values


background-attachment can take one of two values, scroll and fixed.

scroll specifies that the background should scroll as the page scrolls (this is the current behavior of browsers when a page with a background image scrolls)

fixed specifies that the background image should not move as the page scrolls. Effectively the foreground text, images and other content scroll over a stationary background.

Default values


If no background-attachment value is specified, the default value is scroll, which reproduces the behavior of major browsers.

Is it inherited?


background-attachment is not inherited.

Hints and suggestions


In practice, only body elements are affected by the background-attachment property. If and when the overflow property is well supported by major browsers, the background-attachment property of those elements will become important.

background-repeat


What it does


With the traditional background image associated with the body of a page, where the background image is smaller than the width and/or height of the page, browsers "tile" the image (that is repeat the image horizontally and vertically).

CSS allows you to specify whether and how a browser treats background images when the width and height of the image is less than that of the element.

Possible values


background-repeat can take the following values


repeat

repeat-x

repeat-y

no-repeat


repeat has the effect of tiling the background image horizontally and vertically, like the traditional background image in web pages.

repeat-x tiles an image horizontally, but not vertically.

repeat-y tiles the image vertically but not horizontally.

no-repeat makes the image appear once and not tile either horizontally or vertically.

Default values


If no background-repeat value is set, the default value of this property is repeat. This reproduces the effect of the traditional background image.

Is it inherited?


background-repeat is not inherited from the parent element.

Hints and suggestions


Elements other than the body can be given a background image, and so may also have a background-repeat.

background-position


What it does


With a traditional HTML based background image, the image is placed in the top left hand corner of the page, and tiles from there.

With CSS, we can specify where a background image is placed using the background-position property.

If the image repeats (horizontally, vertically or both) then it repeats from this location. If there is a single image, it is placed using the background-position property.

Possible values


background-position is among the more complicated properties. There are several ways of specifying an image position. Positions may be specified using


pairs of percentage values, length values or keywords

individual keywords


Pairs of percentage values

When a position is specified using a pair of percentage values (for example, background-position: 0% 0%) the first value relates to the position of the top of the image, while the second relates to the position of the left of the image. The actual mechanism is best explained with examples.

Suppose we have the following property: (background-position: 15% 25%). This means that the point 15% from the top of the image is aligned with the point 15% from the top of the element. The point 25% from the left of the image is aligned with the point 25% from the left of the element.

This is a little tricky. There are more straightforward ways of specifying the position.

Pairs of length values

When a position is specified using a pair of length values, (for example background-position 20px 20px) the first value specifies the position of the top of the image relative to the element, while the second value specifies the position of the left of the image relative to the element.

The way in which this works is somewhat simpler than percentages. Again, let's look at an example.

With the following property (background-position 20px 30px) the top of the image is positioned 20px from the top of the element for which it is a background, while the left of the image is placed 30px from the left of the element.

If you are wondering quite how this differs from percentage values, see the emphasized text in this part, and compare it with the emphasized text in the part above which describes how percentage values work.

In our section on values we cover exactly what lengths are in detail.

Pairs of keywords and individual keywords

Rather than relying on length values, or percentage values, keyword values can be used. Keywords have a similar effect to percentages.

The keywords which can be used to specify position are


top

left

center

right

bottom


Rather than go into laborious detail, here is the explanation from the recommendation


'top left' and 'left top' both mean the same as '0% 0%'.

'top', 'top center' and 'center top' mean the same as '50% 0%'.

'right top' and 'top right' mean the same as '100% 0%'.

'left', 'left center' and 'center left' mean the same as '0% 50%'.

'center' and 'center center' mean the same as '50% 50%'.

'right', 'right center' and 'center right' mean the same as '100% 50%'.

'bottom left' and 'left bottom' mean the same as '0% 100%'.

'bottom', 'bottom center' and 'center bottom' mean the same as '50% 100%'.

'bottom right' and 'right bottom' mean the same as '100% 100%'.


Default values


If no background-position is specified, this is the equivalent of a background-position of 0% 0%, that is, the top left of the background image is placed in the top left of the element.

Is it inherited?


An element does not inherit the background-position of its parent.

Hints and suggestions


The background-position property lets you create watermark effects by placing a single image in the center of an element, for example in the center of a page. It is a tricky, but powerful property.

Not every type of element can have a background-position, even though any type of element can have a background-image. Only block elements and replaced elements can have this property. For more on the different types of element, see Types of Element.

background


background is a shorthand property that lets you set any or all background properties at once.

The background property takes exactly the same types of values as the properties described in this section. To specify more than one property using the background shorthand, the various background values are separated simply by spaces.

As a simple example, this background property

background: url(http://www.westciv.com/gifs/bg1.gif) fixed no-repeat top center

is the equivalent of


background-image:url(http://www.westciv.com/gifs/bg1.gif);

background-attachment: fixed;

background-repeat: no-repeat;

background-position: top center;

Text layout properties in CSS

This group of properties affect how text itself is laid out on a page. These are properties specific to text, providing control over things like line height and letter spacing. In conjunction with the page layout properties (which apply to elements and their position on a page more generally), they provide quite sophisticated layout of web content. At present, though, CSS does not provide the level of page layout as found in page layout applications such as Quark Express and PageMaker. As it is often said, CSS is not desk top publishing for the web.

While text appearance controls in HTML are very limited, CSS provides control over traditional typographic elements such as letter spacing (kern), word spacing (track) and line spacing (leading).

The text layout properties are:


letter-spacing

word-spacing

line-height

vertical-align

text-indent

text-align

direction

unicode-bidi


letter-spacing


What it does


The letter-spacing property gives you control over the space between characters, what in traditional typography is known as kern.

Possible values


letter-spacing can be specified in any length unit. Length units are discussed in our section on values. For tighter kern, that is to make characters appear more closely together than is the default, negative letter-spacing can be specified.

In addition, the keyword value normal can be used to specify letter-spacing.

Default values


By default text has a letter-spacing of normal.

Is it inherited?


Elements have the same letter-spacing as their parent element. The letter-spacing value of normal overrides an inherited value.

Hints and suggestions


With a letter-spacing of normal, browsers may adjust letter-spacing values to correctly justify text. With an explicit letter-spacing value, browsers will not adjust letter-spacing.

A relative letter-spacing value, like em or ex, rather than an absolute value. e.g. pts, ensures that the look of the text is more or less the same regardless of the actual font and size in fact used by the browser to draw the text. For more, see our section on length values.

word-spacing


What it does


Traditionally in typography, control over the space between words, what is known as track, has been an important element in page design. Loose tracking tends to cause pages to river, reducing legibility. Rivering is the phenomenon of white space forming vertical columns down a page. This tends to draw the reader's eye down the page, rather than across it. Tight tacking, on the other hand impairs legibility as readers strain to make out words as a whole. We tend to read words as units in themselves, rather than as strings of characters. Tight tracking makes it more difficult to do this.

word-spacing gives control over the track of text.

Possible values


word-spacing can be specified in any length unit. Length units are discussed in our section on values. For tighter track, negative word-spacing can be specified.

In addition, word-spacing can be specified as normal.

Default values


word-spacing by default is normal.

Is it inherited?


The word-spacing of an element is the same as that of its parent element. Setting the word-spacing of an element to normal overrides any inherited word-spacing.

Hints and suggestions


As with letter-spacing, relative rather than absolute word-spacing is recommended. For more, see our section on length values.

line-height


What it does


The line-height property sets the distance between adjacent lines. Strictly, the distance is between the baselines of the adjacent lines. The baseline is the imaginary horizontal line on which characters such as A, B and so on sit. Letters such as y descend below the baseline, to the descent line.

In traditional typography, the space between lines is referred to as leading (pronounced like the metal). The line-height property gives you control over leading.

If you are interested, it's pronounced leading, because lead was traditionally used in rows to separate lines of text. The typesetter would "lead" the text. Glad you asked?

Possible values


line-height can be specified in a number of ways, by multiples, percentages, length units or using the keyword normal.

Multiples

The line-height can be specified as a multiple of the size of the font of the element. Multiples are simply numbers with no units. For example, with a font-size of 10pt, and a line-height of 1.2, the space between the baseline of adjacent lines will be 1.2x10=12pt.

Percentages

Our section on percentages discusses percentage values in detail.

Percentage values for line-height are calculated as a percentage of the font-size of the element. So when a line-height is specified as a percentage value, a similar multiplication occurs to that which we saw above in Multiples. The factor of 1.2 we saw in multiples above translates as a percentage of 120%.

With a font size of 12pt, and a line-height of 200%, the resultant line height (space between baselines) is 12x2=24pt.

Lengths

Our section on values discusses length values in detail.

Rather than setting a multiple of the current font-size, length values nominate a specific value for the line-height. This value can be a relative value such as em or ex, or an absolute value such as pt or cm.

See the section on length values for more on the use of relative and absolute length.

normal

In addition, the line-height can be specified as normal. This overrides otherwise inherited values.

Default values


If no line-height value is specified or inherited, the line-height of an element is normal.

Is it inherited?


The line-height of an element is inherited from the element which contains it. There are however a couple of complicating factors to keep in mind. The following is entering the realm of trainspotters, so feel free to ignore it.

If a line-height is specified as a multiple, then a child element inherits the factor, not the resultant value. In our example above, we saw that a font-size of 10pt, with a line-height of 1.2 results in a distance of 12pts between the baseline of adjacent lines. Now, a child element would inherit a line-height multiple of 1.2, rather than a line-height value of 12pts. So, if a child element had an 8pt font size, the resultant line-height of that element would be 1.2x8=9pt, not the 12pt line height of the parent element.

On the other hand, if the line-height of an element is specified as a percentage or a length value, any child element inherits the value, not the multiple.

Hints and suggestions


As with other properties which can take relative and absolute values, take a look at our discussion of length values in the section of values for suggestions about when each type is appropriate.

vertical-align


What it does


Elements often appear on the same line, or lined up horizontally across a page. The vertical-align property gives control over how elements align vertically across the page.

Possible values


vertical-align can be specified either by one of a set of keywords, or by percentage values.

Keywords

vertical-align can be specified using one of a set of keywords. These keywords work in one of two distinct ways. One group of keywords works relative to the parent element, while the other works relative to the line on which an element appears. In many cases this is essentially the same thing.

The keyword values which are relative to the parent element are


baseline - aligns the baseline of the element with the baseline of its parent element. If an element does not have a baseline, the bottom is used.

middle - aligns the vertical midpoint of an item with the baseline of the parent plus half the x-height of the parent.

sub - subscripts the element.

super - superscripts the element.

text-top - aligns the top of an element with the top of the font of its parent element.

text-bottom - aligns the bottom of an element with the bottom of the font of its parent element.


The keyword values which are relative to the line on which an element appears are


top - aligns the top of the element with the top of the tallest element on the line on which it appears.

bottom - aligns the bottom of the element with the baseline of the font of the line on which it appears.


Percentage values

Specifying vertical-align as a percentage value gives rise to a quite complicated situation. The baseline of the element is raised above the baseline of its parent element. By how much? By that percentage of the element's line-height.

For example, {vertical-align: 20%} with an element that has a line-height of 10pt, the baseline of the element will be raised 2 points above the baseline of its parent element.

You can lower the baseline of an element below the baseline of its parent by using negative percentage values.

Default values


If no vertical-align value is set, the vertical-align of an element is baseline.

Is it inherited?


vertical-align is not inherited.

Hints and suggestions


Percentage values are good ways of controlling how images and other non text elements align with text.

text-indent


What it does


Traditionally, in many documents, the first line of every paragraph is slightly indented. text-indent enables you to control how the first line of any element is indented, or outdented.

Possible values


text-indent can be specified using either percentage or length values. See our section on values for detailed descriptions on each.

You can control outdent by giving text-indent negative values, however, different browsers may handle negative values in different ways.

When a percentage value is used, the indent or outdent is that percentage of the width of the parent element.

Default values


If no value is specified or inherited for the text-indent property, no indent or outdent is applied, essentially the property is set to 0.

Is it inherited?


An element has the same indent as its parent element.

Hints and suggestions


A line is not an element as such, and it can vary as the width of a page (window) varies. The text-indent property affects only the first line of an element. To indent an entire paragraph or other element, you can


place it inside a blockquote (which is what most HTML tools do, but is against the style sheet dictum of separating appearance from content, and so is to be avoided, unless the indented text is in fact a block quote)

use the margin property to give a left margin

use the padding property to give a similar effect

use the left property to set the left position of the paragraph


The margin property is to be recommended, as this is in fact what the property exists for.

Note that not all elements can be given a text-indent property. Only block elements take text indentation. For more on block elements, see the section on type selectors.

text-align


What it does


text-align allows you to specify how the contents of an element should be aligned. text-align can only be applied to block elements. For more on block elements, see the section on type selectors.

Possible values


text-align can be one of


left

right

center

justify (to double-justify the text both left and right)


Browsers do not have to support the justify value, and generally treat justify the same as left. (For right to left languages such as Arabic, they may treat justify as right.)

Default values


There is no default value for the text-align property. It is the business of a browser to determine how text should be aligned where no alignment has been specified by a style sheet. This might be determined by the writing system, or by user preferences.

Is it inherited?


An element inherits the alignment of its parent element.

Hints and suggestions


When an element has a text-align specified, it is the contents of the element that are affected by the alignment. For example,

table {text-align: center}

centers the contents of table, not the table itself in its parent. In fact then, because text-align is inherited, the actual content of the table cells should appear centered.

To center the element itself, either center the contents of its parent element using text-align, or use the left-margin and right-margin properties, setting each to auto.

The alignment of the contents of the element is relative to the width of the element, not to the whole page.

While fully justifying text on paper is a strong convention that is generally considered to aid legibility, this is not necessarily true on the web. Because determining line breaks for fully justified text which does not "river" is far from simple, the "quick and dirty" approach to this problem that many browsers take often leaves pages less legible than left justified text. Where readability is a significant issue (particularly for large blocks of text), a value of justify is not recommended.

direction


What it does


CSS2 introduced the direction property. direction specifies the base direction for text. In a number of writing systems, unlike the roman system used in western European languages, text flows from right to left. This property allows the direction to be explicitly specified.

Possible values


direction can be either rtl or ltr.


rtl means that text should flow from right to left

ltr means that text should flow from left to right


Default values


If no direction value is set, the default value is ltr.

Is it inherited?


An element inherits the direction of its parent element.

unicode-bidi


What it does


This is a specialist area of CSS2 that we don't really need to go into. If you are working with unicode, bidirectional fonts, you'll probably be aware of the issues involved. In short, this property aids in controlling the directionality of unicode text.

Possible values


unicode-bidi can be one of


normal

embed

bidi-override


Default values


If no unicode-bidi value is set, the default value is normal.

Is it inherited?


An element does not inherit the unicode-bidi of its parent element.

Text style properties in CSS

The text style properties affect how text appears on a page. While appearance based HTML gave you some control over font, font color, font size and font weight, CSS extended these, and gave much more sophisticated control.

CSS2 revision 1 provides the following properties:


color

font-weight

font-family

font-size

font-variant

font-style

text-decoration

text-transform


color


What it does


This property sets the foreground color of an element. Essentially this means the text color.

Possible values


The color property, not surprisingly, takes color values. For a detailed description of color values, see our section on values.

Default values


There is no specified default color value. The color chosen when this property is not set is dependent on the browser. The value that a browser uses by default is likely to be black (#000), but remember, most browsers allow users to specify default text characteristics, and it is the user's preference which may well be used as the default value for color.

Is it inherited?


Child elements inherit the color of their parent element.

Hints and suggestions


It is recommended that if you set a background-color for an element, you should also set its color. This ensures that there will not be an illegible clash between a text color set by user preferences against a background color set in your style sheet.

Non US speakers of English please note that colour is not an accepted variant spelling for the purposes of CSS syntax.

font-weight


What it does


The weight of a font is how bold it is. The font-weight property affects how heavily text is drawn on a page.

Possible values


font-weight may take two kinds of values, keywords and numerical values.

You may use the following absolute keywords: normal, bold,

or the following relative keywords: bolder, lighter.

bolder specifies that the text should be one degree bolder than the text of the parent element.

lighter specifies that the text should be one degree lighter than the text of the parent element.

font-weight can also be specified using numerical values: 100, 200, 300, 400, 500, 600, 700, 800, 900.

Each value is one degree bolder than the previous. normal is equivalent to 400. bold is equivalent to 700.

Default values


By default, the font-weight of an element is normal.

Is it inherited?


Elements have the same absolute font-weight as their parents. This means an element inherits the numerical value of its parent, or the keyword values of normal or bold. If the parent element's font weight is a relative value, say bolder, the child will be drawn with the computed font-weight of its parent.

Hints and suggestions


As you can see from the browser support information, while font-weight has largely been supported by browsers for a long time, particular implementations have varied. It is safe to use the value bold to create text that will appear bold, and normal to create text that will appear not bold. Beyond this though, there will always be some degree of variation in how values are actually drawn in user agents.

font-family


What it does


If you are familiar with the <font face="font name"> element in HTML, you'll find that the font-family property works similarly. This property specifies a hierarchical list of preferred fonts that a browser should use to draw the element.

A browser will use the first font in the list which is installed on the system it is running on.

Because none of the desired fonts may be on the system a browser is running on, CSS introduces the idea of a font family. A font family is a generic name for a type of font. The specified font families are:


serif (e.g. Times)

sans-serif (e.g. Helvetica)

cursive (e.g. Zapf-Chancery)

fantasy (e.g. Western)

monospace (e.g. Courier)


CSS2 introduced the concept of system fonts. The font-family in CSS2 can be specified as one of several system fonts, that is one of the fonts that the system uses for its major components, such as menu items and window titles. When a system font is chosen, the font family, size, weight and style that the system uses for this kind of item (for example for menu items) are all used.

Possible values


The font-family property is a list of font names and font family names. Each item in the list is separated by a comma.

Font names should be capitalized. So for example, write Ariel, not ariel. When the name of a font is more than one word, it should be quoted, as in "Times New Roman".

System fonts can be one of the following


caption Used for (captioned controls (e.g. buttons, drop-downs, etc.).

icon Used to label icons.

menu Used in menus (e.g., dropdown menus and menu lists).

messagebox Used in dialog boxes.

smallcaption Used for labeling small controls.

statusbar Used in window status bars.


Note that when one of these values is used, this affects the choice of font, along with the style, size and weight of the font. If these values are also set, they are overridden by the style, size and weight that the system uses for drawing the chosen element.

Default values


There is no specified default value for font-family. It is up to browsers to choose a default font. As with other properties, users can often specify their own default font in a browser.

Is it inherited?


An element will have the same font as its parent element, unless it has a font-family property of its own.

Hints and suggestions


The font-family property helps you go a long way to ensuring that your text looks a lot like what you want it to on various platforms, as each operating system usually has a default set of fonts, which largely differ from those of other users. By using a list of fonts, you can provide alternatives for the major operating systems. It is recommended that you choose a generic font family as the last option in the font-family list. This ensures some semblance of your design being displayed in the reader's page.

Keep in mind that there are a number of free fonts designed specifically for onscreen reading that are becoming widely distributed. These include Verdana, Minion Web, Georgia and others. These might be good options.

font-size


What it does


The font-size property lets you specify how large text appears on a web page. With HTML, you are restricted to 7 relative sizes that have no relationship with traditional type measurements such as points, ems, exs and picas. Nor do HTML text sizes even have any relationship with pixels.

CSS allows you to specify the size of font in any of these measures, and in a number of other ways.

Possible values


Length values

The font-size property can specify the size of a text element in several units. You can specify font sizes in the following units:


points

ems

exs

picas

pixels

inches

millimeters

centimeters


In our section on values, we cover each of these in some detail.

Keyword values

In addition, font-size can be specified using both relative and absolute keywords.

The following are the relative keywords: larger and smaller

larger specifies a font-size one degree larger than the parent element.

smaller specifies a degree smaller than the parent element.

As with all keyword values the exact appearance is determined by the browser, and the same text may appear differently in different browsers.

The absolute keywords are:


xx-small

x-small

small

medium

large

x-large

xx-large


Again, it is up to the browser to determine the exact appearance of text that has a keyword font-size property applied to it.

Default values


By default, text appears with a font-size of medium. Keep in mind, it is a matter for browsers to determine what this size in fact appears like.

Is it inherited?


Yes - the size of text is the same as that of the element which contains it, unless a font-size is applied to it.

Hints and suggestions


The choice of how to specify font-size is quite tricky. Note that specifying size in absolute values such as points and pixels is very problematic. 9pt text on a Windows computer might look acceptable, but may be illegible on a Macintosh, due to their differing logical resolutions. Rather than go into detail here, you might like to take a look at this discussion of text sizes by a renowned web page developer, Tod Fahrner.

Note that the keyword medium will not necessarily be the same as the default text size.

font-variant


What it does


The font-variant property lets you specify that text should appear in small capitals.

Possible values


font-variant can take the value of normal or small-caps.

normal specifies that text should appear using capital or lowercase letters as it appears in the source.

small-caps specifies that lowercase letters (but not capitals) should appear as small capital letters. These are capital letters that have more or less the same size as the lowercase letters. Letters which are capitalized in the source appear as they would normally.

Default values


By default, text appears with a font-variant of normal.

Is it inherited?


The variant of text is inherited from a parent element.

font-style


What it does


In traditional typography, there are several different styles of font. With CSS, you can specify the style of the font that is to be applied to an element, using the font-style property.

The three styles of font supported by CSS are normal, italic and oblique. Without going into any detail, normal font is the standard form of a font, referred to as "roman" in traditional typographic terminology. italic and oblique font-styles will in all likelihood appear the same. It is beyond the scope of this discussion to go into detail about why, and what exactly the difference is.

Possible values


The font-style property can take one of three values: normal, italic and oblique.

Default values


If not otherwise specified, the font-style of an element is normal.

Is it inherited?


The font-style of an element is that of its parent element, unless a font-style is specified for the element.

Hints and suggestions


You probably think of italic as a style (or cue), as this is the way that desktop publishing categorizes the effect. CSS in part uses more traditional typographical terminology and concepts, in addition to DTP. Italic is often thought of in the same breath as underlining, and other visual effects. The text-decoration property, discussed below, provides control over a number of these kinds of effect.

text-decoration


What it does


The text-decoration property enables you to specify a number of effects that might be thought of as styles. With text-decoration you can apply the following styles to text: none, underline, overline, line-through, blink.

An element can have either a text-decoration of none, or any number of the other styles.

Possible values


text-decoration can be either none, or a list of one or more of underline, overline, line-through, blink. If there is more than one value in the list, values are separated by spaces.

Default values


By default, the text-decoration of an element is none.

Is it inherited?


Technically, text-decoration is not inherited. In practice, it is.

Confused? If you aren't interested, assume that text-decoration is inherited. If you are interested in what I mean, read on.

text-decoration spans an entire element, and so affects child elements. Even though they don't strictly inherit the property, they will be affected by it.

The value underline for the property text-decoration has a special quality which makes it different to the other values for this property. When a parent element has the value underline, children of that parent should also be underlined, even when those children are given a text-decoration value of none.

Also when a child element inherits underline and the child is a different color from its parent then the underlining should be the same color as the parent, regardless of the color of the child.

Hints and suggestions


Many of what we might think of as styles (thinking in word processing terms) are lumped into this property. If you are looking for other related styles, you might look at font-weight for bold, font-style for italic, and font-family to specify the font in which an element appears.

text-transform


What it does


text-transform allows you to specify the capitalization of text. There are three types of capitalization, and as well you can specify that an element not be affected by text transformation.

Possible values


The text-transform property allows you to ensure that an element conforms to a specific type of capitalization. The three types of capitalization supported are


capitalize, where every word is capitalized, ie the first letter of each word is in capital letters

uppercase, where every letter is in uppercase

lowercase, where every letter is in lowercase


Regardless of the actual capitalization of the text in the source, the browser transforms the text to conform to the desired capitalization.

Alternatively, you can specify a text-transform of none. This ensures that inherited text-transform values have no effect.

Default values


By default, an element has a text-transform of none.

Is it inherited?


Elements have the text-transform value of their parent element.

Hints and suggestions


Many properties, among them text-transform, have a possible value of none. These values exist to let you override the effect of inherited values.