CSS border
Understanding DimensionsIn this lesson, we will learn how to apply border to an HTML element using CSS border properties.
CSS border Property
The CSS border property is used to apply border on an HTML element. It is a short hand property that can be used to set the style, width and color of the border in a single line.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CSS border Example</title>
<!-- Internal CSS -->
<style>
div {
width: 300px;
height: 150px;
border: 1px solid red;
}
</style>
</head>
<body>
<div>
<p>CSS border property</p>
</div>
</body>
</html>
In the above example, the width of the border is 1px, the style of the border is solid and the color of the border is red.
We can also set border's width, style and color separately using the CSS property border-width, border-style and border-color.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CSS border Example</title>
<!-- Internal CSS -->
<style>
div {
width: 300px;
height: 150px;
border-width: 5px;
border-style: solid;
border-color: green;
}
</style>
</head>
<body>
<div>
<p>CSS border-width, border-style and border-color property</p>
</div>
</body>
</html>
CSS Border Styles
There are eight types of border styles available in CSS and they are:
- dotted
- dashed
- solid
- double
- groove
- ridge
- inset
- outset
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CSS border style Example</title>
<!-- Internal CSS -->
<style>
div {
width: 200px;
height: 60px;
border: 10px dotted green;
}
</style>
</head>
<body>
<div>
<p>dotted style border</p>
</div>
</body>
</html>
CSS Border Sides
The CSS border property apply border to all the four sides of an HTML element. You can also apply border to the individual side of an element using the border-top, border-bottom, border-left and border-right CSS property.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CSS border side Example</title>
<!-- Internal CSS -->
<style>
div {
width: 200px;
height: 60px;
background-color: yellow;
border-top: 5px solid red;
}
</style>
</head>
<body>
<div>
<p>border top</p>
</div>
</body>
</html>
CSS Border Radius
The CSS border-radius property allows you to round the corners of an element's border box. It applies to all four border corners by default. You can also apply the border-radius property to individual corners of an element's border box.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CSS border radius Example</title>
<!-- Internal CSS -->
<style>
div {
width: 200px;
height: 60px;
background-color: yellow;
border: 2px solid red;
border-radius: 20px;
}
</style>
</head>
<body>
<div>
<p>border radius</p>
</div>
</body>
</html>