XML technologies / CSS / Advanced websites with CSS / A simple CSS layout / Positioning page elements / Border properties

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

Border properties are more complex than padding or margin because they affect both spacing between objects and its appearance. Borders are normally visible. Their properties work similarly to margin properties, with some important differences.

A border separates padding from margin and has style, width and color. The default style is none, the width medium (Note: Netscape 4 instead defaults to 0, so no border appears unless explicitly defined), and the color matches the bordered element’s default color.

The property border-style controls border appearance and accepts several values. The table shows them and their support in major browsers.

Table: CSS border styles.

Definition

CSS specification

Browser support

Example

double

CSS1

All CSS-capable browsers

double

groove

CSS1

All CSS-capable browsers

groove

inset

CSS1

All CSS-capable browsers

inset

none

CSS1

All CSS-capable browsers

none

outset

CSS1

All CSS-capable browsers

outset

ridge

CSS1

All CSS-capable browsers

ridge

solid

CSS1

All CSS-capable browsers

solid

dashed

CSS1

Netscape 7, Mozilla, IE 6/Win, IE 5/Mac

dashed

dotted

CSS1

Netscape 7, Mozilla, IE 6/Win, IE 5/Mac

dotted

hidden

CSS2

Netscape 7, Mozilla, IE 6/Win, IE 5/Mac

hidden

The value hidden means the same as none except in HTML tables. The border stays invisible. Full details of border values appear in CSS property reference.

W3C does not precisely specify how border styles should look, so results can vary between browser versions and operating systems. Generally, however, browsers render these properties predictably, satisfactorily and sensibly, like other border properties.

Set border width with the four individual properties border-top-width, border-right-width, border-bottom-width and border-left-width or the shorthand border-width. Use relative or absolute units such as pixels, em, percentages or inches, or the CSS keywords thin, medium and thick. Browsers interpret these slightly differently, but the difference is at most one pixel.

Exact border measurements
Pixels are best for exact border widths. They are the key unit in pixel-perfect layouts, where the property border-width can be important.

Set border color with the shorthand border-color or individual properties border-top-color, border-right-color, border-bottom-color and border-left-color.

As explained in Specifying colors, colors can use hexadecimal RGB (#ff9900), abbreviated RGB (#f90), numeric RGB (rgb (102, 153, 0)) or a standard color name (red).

The shorthands border-style, border-width and border-color also accept multiple values.

Another shorthand is probably the most widely used. The property border sets border style, width and color on all four sides at once. Most borders need four identical sides, and this is the simplest approach.

This rule gives any element of class warning an even red border three pixels wide on all sides:

.warning {
  border: 3px solid red;
}

  

 

 

 

back to top