The border is a line (with width) that surrounds the padding area and content sits inside the padding. The inside of the border touches the padding and the outside of the the border touches the margin. The border itself has four-sides: top, right, bottom, and left. We show that in the following picture.
We look at some of the important properties that add style to the border: border-width, border-style, border-color, and border-radius. We also discuss another property, namely the "border" property, that combines border width, style, and color properties. For all of these properties, we can specify the value as "inherit" and in that case, they would inherit their value is from the parent element.
So, let's get started and list the above properties first!
The first property that we would look is the border-width. A border is basically a line and the border-width allows us to set the value of the width.
The second property specifies the style of the border. Like any other lines, we can specify the line style to be solid, dotted, double (double meaning there would be two lines!), etc. This is a rather important border property -- if this is set to "none", then other border properties would not even show up!
The next property allows us to specify the color of the border. We can specify color as text (like "red", "blue", etc) or as hex RGB (Red, Green, Blue) value (like #0000FF, #FF0000, etc).
Our first example shows the usage of two border-properties: border-width, border-style, and border-color. The example (provided below) has two paragraph elements. We keep the border-style to be solid and double for these two paragraph elements.
<!doctype html> <html> <head> <title> Learning CSS: Box Borders </title> <link type="text/css" rel="stylesheet" href="border.css"> </head> <body> <p id="id_jurassic"> Jurassic period was one of the three main periods of Mesozoic Era. Jurassic period is further divided into three sub-periods: early Jurassic, middle Jurassic, and late Jurassic. This period was a significant period for dinosaurs and other reptiles. They became the most dominant and the most diverse vertebrae during this period. </p> <p id="id_reptiles"> Reptiles ruled the earth during this period, Dinosaurs were the most dominant race on the land. Notable among them were the Theropods -- the dinosaurs that hunted on two legs and were mostly carnivores. Reptiles were also the most dominant race in the ocean. Two such reptiles were Plesiosaurs and Ichthyosaurs. And when it came to sky, the supreme flying creature was once again another reptile: Pterosaurs. </p> </body> </html>
And here is the "border.css" that modifies the border-width, border-style and border-color properties. Please note that we need to set the border-style; otherwise, the default value of the border-style is "none" and that would mean we would not be able to see the border, let alone the value set by border-width property!
#id_jurassic { border-width: thick; border-style: solid; border-color: blue; } #id_reptiles { border-width: 8px; border-style: double; border-color: green; }
If we load the above HTML page, here is what we would see:
Now, CSS provides a shorthand to combine the above three properties (width, style, and color); that is the border property. With this, we can specify the value of the width, style, and color (in that order) without any commas in between. It is okay if we do not provide all three -- the one (or the ones) that we do not provide would assume the default value.
If we want, we can use the "border" property to combine together the border-width, border-style, and border-color into one. Here is the updated "border.css" file from our earlier example. The output would be same as before and hence, we omit it.
#id_jurassic { border: thick solid blue; } #id_reptiles { border: 8px double green; }
By default, the boxes have sharp corner (like a lot of the boxes out there!). However, CSS allows us to make these edges more round and it does so by using the property of "border-radius". We can use this property to specify the radius value that would determine the roundness of the edges.
For the above property, if you find specifying 8 values for the border-radius a little confusing, then I don't blame you! Usually, a single value is all you would need since that would keep both horizontal and vertical radii to be same and would keep the value same for all corners. Also, you might find it simpler to specify these values individually using border-top-left-radius, border-top-right-radius, border-bottom-right-radius, and border-bottom-left-radius. We describe these four properties a little later on this page.
Our next example focuses on the border-radius property. For that, we reuse the same HTML file as before and merely update the CSS file ("border.css"). This time, we keep the border-width to be 8px, border-style to be solid, and border-color to be green for both paragraph boxes, provide different values for border-radius. In this example, for #id_jurassic element, we provide values for both horizontal and vertical radii. For the #id_reptiles element, we specify all 4 pairs, one for each corner. The first set is for the top-left corner, the second set is for top-right corner, and so on. If you wanted to have a single value (which would happen in most of the cases!), you could just keep it as "border-radius: 20px" and this would apply to all four corners.
Here is the updated "border.css" CSS file.
#id_jurassic { border-style: solid; border-width: 8px; border-color: green; border-radius: 20px/40px; } #id_reptiles { border-style: solid; border-width: 8px; border-color: green; border-radius: 10px 30px 50px 70px/10px 30px 50px 70px; }
If we reload the above HTML page after changing the CSS file, we would see that now we have corners for the two borders. Note that the second paragraph box has different radii value pertaining to different corners -- the order being top-left, top-right, bottom-right, and bottom-left.
All of the above border-related properties also come in more specific versions. For the sake of completeness, we provide them here. If you are dealing with a case where you have to specify individual values to different borders (top, right, bottom, or left), then you would need to specify them individually. However, if you do not need to bother about individual sides, then you should just stick with the less-specific versions that are described in the earlier section -- that would be a simpler design.
Let us begin with the four variations for the border-width property. Like the case of the border-width property, all of these accept the same kind of values: "thin | medium | thick | <length> | inherit".
Next, we provide the four specific variants of border-style property. Similar to the border-style property, the following four accept the same set of values: "solid | dotted | dashed | double | groove | ridge | none | inset | outset | hidden". Please look at the border-style property above since the same behavior would apply here as well.
Let us next provide the four variants of the border-color. Like the case of the border-color property, all of these accept values as: "<color> | transparent | inherit". The difference here is that we can specify color to individual borders.
Next, we also have specific variants for the border-radius property. Unlike the border-radius property, the following property take only two sets of input: "<length1> <length2>". The two input are the two radii associated with each corner: the horizontal radius and the vertical radius. Thus, "border-top-left-radius: 5px 10px" would assign the horizontal radius of the top left corner as 5px and the vertical radius of the same corner as 10px. If we want simplicity, then we can still specify a single value and that would apply for both horizontal and vertical radii for the selected corner.