XML technologies / CSS / CSS Cookbook / Images / Adding borders to images

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

You want to add a border to an image.

Solution

Use the property border for the element img (see the following figure):

img {
 width: 300px;
 border: 6px double #666;
 background: #fff;
 padding: 6px;
}

A border is added to the image

Figure: A border is added to the image.

Discussion

If you make the image a link, the property border can make the presentation more elaborate. Using the pseudo-class :hover changes the border style when the pointer is over the image (see the next figure):

img {
 width: 30px;
 border: 4px double #666;
 background: #fff;
 padding: 4px;
}
a:hover img {
 border-style: solid;
 background: #999;
}

The CSS border acts like a real picture frame. Its styles can change dynamically as the pointer moves over the image. Using four pixels of padding on the img element also allows further colour changes inside the frame. Two declaration blocks can therefore produce rich visual effects.

Combining background colours and borders creates interesting rollover effects

Figure: Combining background colours and borders creates interesting rollover effects.

See also

The recipe Removing default image borders on removing image borders.

  

 

 

 

back to top