CSS borders define the outline around an HTML element, providing visual separation and emphasis within a webpage layout.
You can try different types of borders here:
<!DOCTYPE html>
<html>
<head>
<style>
.simple-border {
border: 2px solid black;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<div class="simple-border">
I am a div element with a solid black border!
</div>
</body>
</html>
In this example:
<div> element is created with a black border that is 2px wide.<div> has 20px of padding for spacing between the text and the border.<div> is centered using text-align: center.Syntax:
element {
border: 1px solid black;
}
CSS provides several properties to control and customize borders:
| Property | Description |
|---|---|
border-style |
Determines the type of border (e.g., solid, dashed, dotted). |
border-width |
Sets the width of the border (in pixels, points, or other units). |
border-color |
Specifies the border color. |
border-radius |
Creates rounded corners for elements. |
The CSS border property enables the styling of an element's border by setting its width, style, and color, allowing for customizable visual boundaries in web design.
You can target specific sides using:
border-top-styleborder-right-styleborder-bottom-styleborder-left-styleYou can change the width for each side:
border-top-widthborder-right-widthborder-bottom-widthborder-left-widthColors can also be specified per side:
border-top-colorborder-right-colorborder-bottom-colorborder-left-colorCSS allows you to style each side of a border individually using shorthand properties like border-top or border-bottom, giving extreme flexibility in design.
The border-radius property allows you to round the corners of an element, creating smoother edges.
The border-style property specifies the type of border. None of the other border properties will work without setting the border style.
| Border Style | Description |
|---|---|
| Dotted | Creates a series of dots. |
| Dashed | Forms a dashed line. |
| Solid | Produces a continuous line. |
| Double | Renders two parallel lines. |
| Groove | Creates a 3D grooved effect. |
| Ridge | Creates a 3D ridged effect. |
| Inset | Adds a 3D inset border. |
| Outset | Adds a 3D outset border. |
| None | Removes the border. |
| Hidden | Hides the border. |
The following example demonstrates different common border styles using the border-style property in CSS.
<!DOCTYPE html>
<html>
<head>
<style>
p { padding: 10px; }
p.dotted {
border-style: dotted;
}
p.dashed {
border-style: dashed;
}
p.solid {
border-style: solid;
}
p.double {
border-style: double;
}
</style>
</head>
<body>
<p class="dotted">A dotted border.</p>
<p class="dashed">A dashed border.</p>
<p class="solid">A solid border.</p>
<p class="double">A double border.</p>
</body>
</html>
In this example:
border-style is used to set the type of border around an element.CSS border-width is used to define the thickness of the border around an element. It can be specified in various units like px, pt, cm, or by using predefined values like thin, medium, and thick.
<!DOCTYPE html>
<html>
<head>
<style>
p {
border-style: solid;
border-width: 8px;
padding: 10px;
}
</style>
</head>
<body>
<p>This paragraph has an 8px solid border.</p>
</body>
</html>
In this example:
border-width property is used to set the thickness of the border.1px, 5pt, 2cm) or keywords (thin, medium, thick) to set the border width.border-style and border-color properties must be used in conjunction with border-width to see the effect.CSS border-color is used to define the color of the border. You can set the color using color names, hexadecimal values, or RGB values. If no color is specified, the border will inherit the text color of the element itself.
<!DOCTYPE html>
<html>
<head>
<style>
p {
border-style: solid;
border-width: 3px;
border-color: red;
padding: 10px;
}
</style>
</head>
<body>
<p>This paragraph has a red border.</p>
</body>
</html>
In this example:
border-color property is used to set the color of the element's border.red. You can also use hex codes like #ff0000 or RGB values like rgb(255, 0, 0).border-style property must be defined (e.g., solid, dashed, etc.) for the border color to be visible.The CSS border-radius property is used to round the corners of an element's border, giving it a more visually pleasing and smoother appearance.
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
border-style: solid;
text-align: center;
background: navy;
color: white;
border-radius: 20px;
padding: 10px;
}
</style>
</head>
<body>
<h1>Rounded Corners</h1>
</body>
</html>
In this example:
border-radius is used to apply rounded corners to the element. In this case, a value of 20px is applied to create a soft, rounded edge.border-radius value to control the curvature of the corners, making them more or less rounded.CSS borders are commonly used in the following scenarios:
Which CSS property MUST be defined for the border to appear on the screen, regardless of its color or width?