The three key properties for basic borders are border-width
,
border-style
, and
border-color
.
The following code defines a Basic Border.
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
p {<!-- w ww .j av a 2 s . c o m-->
border-width: 5px;
border-style: solid;
border-color: black;
}
</style>
</head>
<body>
<p>This is a test.</p>
</body>
</html>
The code above is rendered as follows:
You may express the border-width
property as
The default border-width value is medium.
The following code defines a Basic Border.
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
p {<!--from w ww.ja v a 2 s. co m-->
border-width: 5px;
border-style: solid;
border-color: black;
}
</style>
</head>
<body>
<p>This is a test.</p>
</body>
</html>
The code above is rendered as follows:
The border-style
property can be one of the values shown in the following list.
The default value is none, meaning that no border is drawn.
The following code defines a Basic Border.
<!DOCTYPE HTML>
<html>
<head>
<style type='text/css'>
div {<!-- w w w . j a v a2 s . c o m-->
padding: 10px;
margin: 10px;
border: thick black;
}
</style>
</head>
<body>
<div style='border-style: dashed;'> border-style: dashed;</div>
<div style='border-style: solid;'>border-style: solid;</div>
<div style='border-style: ridge;'>border-style: ridge;</div>
<div style='border-style: outset;'>border-style: outset;</div>
<div style='border-style: none;'>border-style: none;</div>
<div style='border-style: inset;'>border-style: inset;</div>
<div style='border-style: hidden;'>border-style: hidden;</div>
<div style='border-style: groove;'>border-style: groove;</div>
<div style='border-style: dotted;'>border-style: dotted;</div>
</body>
</html>
The code above is rendered as follows:
border-color
controls the border color.
The following code shows how to set border-color to blue.
<html>
<head>
<style type="text/css">
div {<!--from ww w .java2 s. co m-->
margin-bottom: 1em;
padding: .5em;
border-width: thick;
border-color: blue;
border-style: groove;
}
.dotted {
border-color: red;
border-width: medium;
border-style: dotted;
text-align: center;
}
#two {
border-style: double solid;
border-color: red green purple blue;
border-width: thin medium thick .25cm;
}
</style>
</head>
<body>
<div>this is a test.</div>
<p class="dotted">Dotted</p>
<p id="two">This is another paragraph
that has its borders set in a very bizarre way!</p>
</body>
</html>
The code above is rendered as follows:
You can apply different borders to each side of an element using properties that are more specific.
The following code uses the Side-Specific Border Properties.
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
h2 {<!--from w ww . j a v a 2 s. co m-->
border-bottom-style: dashed;
}
p {
border-width: 5px;
border-style: solid;
border-color: black;
border-left-width: 10px;
border-left-style: dotted;
border-top-width: 10px;
border-top-style: dotted;
}
.myStyle {
border-bottom: 1px dotted black;
}
</style>
</head>
<body>
<h2>This is the title</h2>
<p>This is a test.</p>
<span class="myStyle">Under 5</span>
</body>
</html>
The code above is rendered as follows:
You can use shorthand properties that set all three values in one step.
You set the values for these properties by specifying the width, style, and color values in a single line, separated by spaces.
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
p {<!--from ww w .ja v a 2 s . c om-->
border: medium solid black;
}
div#shorthand {
border: 1px solid crimson;
}
</style>
</head>
<body>
<p>This is a test.</p>
<div id='shorthand'>border</div>
</body>
</html>
The code above is rendered as follows:
The color for the border-top property is not set. If you omit one or more of the values, the browser will use whatever value has been previously defined.
In this case, the color specified by the border shorthand property.
Property | Description | CSS |
---|---|---|
border-bottom-color | Sets the color of the bottom border | 1 |
border-bottom-left-radius | Set the border radius for the bottom-left corner | 3 |
border-bottom-right-radius | Set the border radius for the bottom-right corner | 3 |
border-bottom-style | Set the border bottom style | 1 |
border-bottom-width | Set the width for bottom border | 1 |
border-bottom | Shorthand property for bottom border | 1 |
border-collapse | collapse table borders | 2 |
border-color | Sets the color for the four borders | 1 |
border-image-outset | Set distance the border image area extends beyond the border box | 3 |
border-image-repeat | repeat, round or stretch image-border | 3 |
border-image-slice | Set the inward offsets of the image-border | 3 |
border-image-source | Set border image location | 3 |
border-image-width | Set the widths of the image-border | 3 |
border-image | Shorthand property for all the border-image-* properties | 3 |
border-left-color | Set color for left border | 1 |
border-left-style | Set style for left border | 1 |
border-left-width | Set width for left border | 1 |
border-left | Shorthand property for left border | 1 |
border-radius | shorthand property for setting all the four border-*-radius properties | 3 |
border-right-color | Set color for right border | 1 |
border-right-style | Set style for right border | 1 |
border-right-width | Set width for right border | 1 |
border-right | Shorthand property for right border | 1 |
border-spacing | Set the distance between the borders of adjacent cells | 2 |
border-style | Set style for all four borders | 1 |
border-top-color | Set color for top border | 1 |
border-top-left-radius | Set border radius for top left | 3 |
border-top-right-radius | Set border radius for top right | 3 |
border-top-style | Set style for top border | 1 |
border-top-width | Set width for top border | 1 |
border-top | Shorthand property for top border | 1 |
border-width | Set border width for all four directions | 1 |
border | Shorthand border setting | 1 |