About Lesson
box-sizing: border-box;
Definition:
The box-sizing
property defines how the total width and height of an element is calculated.
- Default Behavior (
content-box
):- By default,
width
andheight
apply only to the content area, and padding and border are added outside it.
- By default,
- With
border-box
:- When you use
box-sizing: border-box;
, the padding and border are included inside the total width and height. - This makes layout easier and more predictable
- When you use
Syntax:
selector {
box-sizing: border-box;
}
Example:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.default-box {
width: 300px;
padding: 20px;
border: 5px solid red;
background-color: lightcoral;
margin-bottom: 20px;
}
.border-box {
width: 300px;
padding: 20px;
border: 5px solid green;
box-sizing: border-box;
background-color: lightgreen;
}
</style>
<title>Box-Sizing Example</title>
</head>
<body>
<div class="default-box">
box-sizing: content-box (default)
</div>
<div class="border-box">
box-sizing: border-box
</div>
</body>
</html>
Output:
