Jegsworks: Free Online Computer Lessons Logo:Jegsworks Jan's Illustrated Computer Literacy 101


Home > Resources & Examples > Jan's CSS > Borders
Icon: Arrow - Previous pagePrevious    NextIcon: Arrow - Next page

Jan's CSS: Borders

With CSS borders you can control the width and color of a border plus other characteristics like rounded corners and shadows.

You can set the border for each side separately if you wish. That is particularly helpful when you need a dividing line on just one side.

Syntax:

For a simple border you can use a shorthand property that combines three properties:

border: width style color;

  • Border-width is the thickness of the border in pixels.
  • Border-style is solid, dashed, dotted, groove, inset, outset, or none.
  • Border-color can be an HTML color like #ff0000, an RGB color like rgb(255,0,0), or a named color like red.

For example, the BORDER property below creates a dotted border around its element:

border: 4px dotted blue;

If you want to set the characteristics separately:

border-width:3px; border-style:dashed; border-color:rgb(110,110,0);

If you want to set a characteristic for just one side, instead of border: use border-left: , border-right: , border-top:, or border-bottom:


 Examples: Border Types




Simple border



Rounded Corners



Shadow

 

border:3px solid #dcdcdc; border:2px solid #dcdcdc;
border-radius:25px;
border:2px solid #dcdcdc;
box-shadow: 10px 10px 5px #888888;

 
 Inset
 

 
 Groove
 



Outset
border:10px #dcdcdc inset; border:16px #dcdcdc groove; border: 8px #dcdcdc outset;
margin:30px;

Image of the results:

Screen shot of borders examples


New for CSS3-- Border Image: Too cool to leave out! This property is not supported yet by all of the common browsers, but most have their own version. In the sample code below, the property names with leading hyphens like -webkit-border-image tell the browser to use its own version of the border-image property. The image usually has a transparent center plus you have to set a border-width that is wide enough to hold the design. The repeat property can have values round, stretch, or repeat. The middle image on the side is used for the repeat.

Syntax:

border-image: source slice width outset repeat;

Original image used for the border with transparent center:  Image used in the border-image property

This DIV has an image border - round.

border-image:url(frame-leaf.gif) 50 round;

This DIV has an image border - stretch.

border-image:url(frame-leaf.gif) 30 stretch;

This DIV has an image border - repeat.

border-image:url(frame-leaf.gif) 30 repeat;

Full Code for the first example:

.borderwithimage-round {border:15px solid transparent;
     padding:10px 20px;
     margin-bottom:20px;
     -webkit-border-image:url(frame-leaf.gif) 50 round; /* Safari */
     -o-border-image:url(frame-leaf.gif) 50 round; /* Opera */
     border-image:url(frame-leaf.gif) 50 round;
}


Image of border-image effects above:

Screenshot of DIV with border-image

You do not always get the same effect in every browser. So be careful to check what the major browsers do with your border-image choices.