How to Use the CSS Border Shorthand Property
See CSS: Tips and Tricks for similar articles.
Shorthand properties can take multiple values, allow you to write less code, and provide smaller files for the user to download. Find out how to use the CSS border shorthand property in these steps.
- The shorthand for
borderis as follows:selector { border: border-width border-style border-color; } - You can also apply the shorthand for
borderindividually toborder-top,border-right,border-bottom, andborder-left. For example, the following syntax demonstrates the shorthand structure applied to aborder-topproperty:
The CSS initial values for theselector { border-top: border-width border-style border-color; }borderproperty are shown in the following table:Property Initial Value border-widthmediumborder-stylenoneborder-colorinherited from a colorproperty - In addition to the
borderproperty, there are a few other shorthand properties, as indicated in the previous table. They areborder-color,border-style, andborder-width. Their structure is different from theborderproperty. The structure is actually similar tomarginandpaddingproperties in that you set the value for each side, as shown in the following syntaxselector { border-color: Top Right Bottom Left; border-style: Top Right Bottom Left; border-width: Top Right Bottom Left; } - The following example demonstrates the use of the
bordershorthand property:
This code renders the following:<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>CSS Border Shorthand</title> <style type="text/css" media="screen"> div { margin: .75em; padding: .5em; } div.one { border: 1px solid blue; /* This sets the border value to a solid blue border with a width of 1px. */ } div.two { border-top: 1px solid blue; /* This sets a solid blue border with a width of 1px on the top . */ border-right: 2px solid blue; /* This sets a solid blue border with a width of 2px on the right. */ border-bottom: 3px solid blue; /* This sets a solid blue border with a width of 3px on the bottom. */ border-left: 4px solid blue; /* This sets a solid blue border with a width of 4px on the left. */ } div.three { border: solid; /* This border uses the initial values for color and width. */ } div.four { border: 1px #00f; /* No style is set for this border so it will not display. */ } </style> </head> <body> <div class="one">The border around this text will be solid blue with a width of 1px.</div> <div class="two">The border around this text is using shorthand for each individual side.</div> <div class="three">The border around this text will use the initial values for color and width.</div> <div class="four">No border will show as the border-style has not been set.</div> </body> </html>
