It’s Day 4 of our CSS journey! Today we are going to explore two core concepts that determine how elements are sized, spaced, and rendered in web pages: the CSS Box Model and Display Properties.
Every HTML element can be thought of as a box – and learning how boxes behave is like learning the rules of arranging furniture in a room. You will master controlling spacing around an element, adding borders, sizing, and showing/hiding the element. These concepts are vital because they are the foundation for every web layout you will develop. Understanding these concepts will not only help you get around the hassle of spacing issues, but it will also allow you to design great layering solutions with ease!
Let’s begin with our HTML structure, which we will continue to style during this tutorial:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Day-4 css box model practice</title>
<link rel="stylesheet" href="Box_Model.css">
</head>
<body>
<h1>Day-4 css box model practice</h1>
<div class="card box-model">
<h2>Box Model</h2>
<div class="box">Content Area</div>
<p>Includes: Margin → Border → Padding → Content</p>
</div>
<!-- More card sections will go here -->
</body>
</html>
What this code does:
Why we do this:
Pro Tip: Use class names that describe the class like “box-model” to describe what the element is, not just what it looks like.
Create a new file called Box_Model.css and we will start applying some basic styles:
/* Reset default margins and set up base styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
background-color: #f8f9fa;
padding: 20px;
color: #333;
}
h1 {
text-align: center;
margin-bottom: 30px;
color: #2c3e50;
padding: 15px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border-radius: 8px;
}
What this code does:
Why we do this:
Common Mistake to Avoid: Forgetting to apply the css reset leading to inconsistent spacing in different browsers.
Now let’s style our card containers which will each hold the demo section:
/* Card container styling */
.card {
background: white;
border-radius: 10px;
padding: 25px;
margin: 20px 0;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
border-left: 5px solid #667eea;
}
.card h2 {
color: #2c3e50;
margin-bottom: 15px;
border-bottom: 2px solid #ecf0f1;
padding-bottom: 10px;
}
What this code does:
Why we do this:
Let’s create our entry-level demonstration of the box model:
/* Box Model Demonstration */
.box-model .box {
width: 200px;
height: 100px;
background-color: #3498db;
color: white;
text-align: center;
line-height: 100px;
/* Padding - space inside the border */
padding: 20px;
/* Border - the visible edge */
border: 5px solid #2980b9;
/* Margin - space outside the border */
margin: 30px auto;
}
.box-model p {
text-align: center;
font-weight: bold;
color: #7f8c8d;
margin-top: 10px;
}
What this code does:
Why we do this:
Pro Tip: For a block element that has a defined width, it is easy to horizontally center the block in a larger container using margin: 0 auto.
Now let’s look at sizing an element. the primary size property, and more importantly, the box-sizing property:
/* Width & Height Examples */
.fixed-box {
width: 150px;
height: 80px;
background-color: #e74c3c;
color: white;
text-align: center;
line-height: 80px;
border: 3px solid #c0392b;
margin: 15px 0;
}
/* Box-Sizing Demonstration */
.box-sizing-demo {
width: 200px;
height: 100px;
background-color: #27ae60;
color: white;
text-align: center;
line-height: 100px;
padding: 20px;
border: 5px solid #219653;
/* The magic property! */
box-sizing: border-box;
margin: 15px 0;
}
What this code does:
Why we do this:
Common mistake to avoid: Failing to apply box-sizing: border-box will result in unexpected element dimensions when padding or borders are assigned.
Let’s look at how each display type affects how an element behaves:
/* Display Types Demonstration */
.display-types span {
display: block;
margin: 8px 0;
padding: 10px;
background-color: #f39c12;
color: white;
text-align: center;
}
.display-types .inline {
display: inline;
background-color: #8e44ad;
}
.display-types .inline-block {
display: inline-block;
background-color: #16a085;
width: 120px;
}
.display-types .none {
display: none;
background-color: #95a5a6;
}
What this code does:
Why we do this:
Lastly, let’s take a look at visibility and content overflow management:
/* Visibility Examples */
.visible {
visibility: visible;
background-color: #d4edda;
padding: 10px;
margin: 10px 0;
border: 1px solid #c3e6cb;
}
.hidden {
visibility: hidden;
background-color: #f8d7da;
padding: 10px;
margin: 10px 0;
border: 1px solid #f1b0b7;
}
/* Overflow Demonstration */
.overflow-box {
width: 300px;
height: 150px;
border: 2px solid #bdc3c7;
padding: 15px;
margin: 15px 0;
/* Overflow handling */
overflow: auto;
background-color: #ecf0f1;
}
.sticky {
position: sticky;
top: 0;
background-color: #34495e;
color: white;
padding: 10px;
margin: -15px -15px 15px -15px;
text-align: center;
}
What this code does:
Why we do this:
The Complete Box Model Breakdown
Next, we want to explore the entire concept of the box model with a thorough example:
.box-model-detail {
/* Content dimensions */
width: 300px;
height: 150px;
/* Padding - inside space */
padding: 25px;
/* Border - visible boundary */
border: 10px solid #3498db;
/* Margin - outside space */
margin: 40px;
/* Background and text */
background-color: #ecf0f1;
text-align: center;
line-height: 150px;
}
How it works:
Total element width calculation:
Why this matters: Knowing how to calculate these dimensions does prevent layouts from doing unexpected things and, more importantly, will open the door to more precise designs.
Display Types Deep Dive
Next, we will look at the most common display values and when you typically want to use them:
.display-comparison {
margin: 20px 0;
}
.block-element {
display: block;
width: 100%;
background: #e74c3c;
padding: 10px;
margin: 5px 0;
}
.inline-element {
display: inline;
background: #3498db;
padding: 5px;
/* width and height don't work on inline elements! */
}
.inline-block-element {
display: inline-block;
background: #27ae60;
padding: 10px;
width: 120px; /* This works! */
margin: 5px;
}
How they work:
Why choose each:
1. Components / Keywords Used
Name | Description |
box-sizing | Controls how element width and height are calculated |
display | Defines how an element is displayed in the layout |
visibility | Controls whether an element is visible or hidden |
overflow | Manages content that overflows its container |
margin | Space outside the element’s border |
padding | Space inside the element’s border, around content |
border | Visible boundary around padding and content |
width/height | Sets dimensions of the content area |
block | Display type: element takes full width, starts on new line |
inline | Display type: flows with text, only takes needed space |
inline-block | Display type: inline flow but accepts block properties |
2. Key Properties / Parameters / Features
Name | Description |
content-box | Default box-sizing: width/height only apply to content |
border-box | Box-sizing: width/height include content, padding, border |
visible | Default visibility: element is visible |
hidden | Visibility: element is hidden but still takes space |
auto | Overflow: adds scrollbars only when needed |
scroll | Overflow: always shows scrollbars |
hidden | Overflow: clips overflowing content |
sticky | Position: sticks to viewport when scrolling |
auto margin | Centers block elements horizontally when width is set |
You have learned how to:
Mastering these basics helps you build neat, consistent, and professional web layouts. Keep practicing different spacing and display combinations to improve your designs!
The CSS Box Model defines how every element on a webpage is structured—content, padding, border, and margin. It is crucial for controlling spacing and layout accurately.
Block elements take full width and start on a new line, inline elements stay within text flow, and inline-block elements allow width/height control while staying inline.
box-sizing: border-box; includes padding and border inside the set width/height, making layouts easier and preventing unexpected element expansion.
Margins of two vertical block elements can merge into a single margin. This happens when no padding, border, or content separates them.
Use flex when you need dynamic alignment, spacing, and responsive layouts. Use block for simple, full-width elements without complex alignment needs.
Copyright © 2025 NioTechOne Software Solution Pvt. Ltd. All Rights Reserved.