Understand how elements are sized and spaced in CSS - the foundation of layout.
Every HTML element is treated as a box in CSS. The CSS box model describes how the content, padding, border, and margin work together to create the visual representation of an element.
Yellow: Margin | Green: Border | Blue: Padding | Orange: Content
Control the size of the content area.
/* Fixed dimensions */
.element {
width: 200px;
height: 100px;
}
/* Percentage dimensions */
.container {
width: 100%;
height: 50vh; /* 50% of viewport height */
}
/* Auto dimensions */
.auto-width {
width: auto; /* Default - fits content */
height: auto;
}
/* Min and Max dimensions */
.responsive {
min-width: 200px;
max-width: 800px;
min-height: 100px;
max-height: 400px;
}
Add space inside the element, between content and border.
/* All sides */
.element {
padding: 20px;
}
/* Individual sides */
.element {
padding-top: 10px;
padding-right: 20px;
padding-bottom: 15px;
padding-left: 25px;
}
/* Shorthand - top/bottom, left/right */
.element {
padding: 10px 20px;
}
/* Shorthand - top, left/right, bottom */
.element {
padding: 10px 20px 15px;
}
/* Shorthand - top, right, bottom, left (clockwise) */
.element {
padding: 10px 20px 15px 25px;
}
Small padding (5px)
Large padding (20px)
Different horizontal/vertical padding
Create borders around elements.
/* Border shorthand */
.element {
border: 2px solid #333;
}
/* Individual border properties */
.element {
border-width: 3px;
border-style: solid;
border-color: #007bff;
}
/* Individual sides */
.element {
border-top: 1px solid red;
border-right: 2px dashed blue;
border-bottom: 3px dotted green;
border-left: 4px double orange;
}
/* Border radius for rounded corners */
.rounded {
border-radius: 10px;
border: 2px solid #333;
}
/* Different radius for each corner */
.custom-radius {
border-radius: 10px 20px 30px 40px;
}
Solid border
Dashed border
Dotted border
Double border
Rounded border
Add space outside the element, between elements.
/* All sides */
.element {
margin: 20px;
}
/* Individual sides */
.element {
margin-top: 10px;
margin-right: 20px;
margin-bottom: 15px;
margin-left: 25px;
}
/* Shorthand - top/bottom, left/right */
.element {
margin: 10px 20px;
}
/* Auto margins for centering */
.centered {
margin: 0 auto; /* Centers block elements */
width: 200px;
}
/* Negative margins */
.overlap {
margin-top: -10px; /* Can overlap other elements */
}
Small margin (5px)
Large margin (20px)
Different horizontal/vertical margin
The box-sizing
property determines how the width and height are calculated.
Width: 200px + 40px padding + 10px border = 250px total
Total width: 250px
Width: 200px (includes padding & border)
Total width: 200px
/* Default behavior */
.content-box {
box-sizing: content-box;
width: 200px;
padding: 20px;
border: 5px solid #333;
/* Total width = 200 + 40 + 10 = 250px */
}
/* Modern approach */
.border-box {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid #333;
/* Total width = 200px */
}
/* Global reset for easier calculations */
* {
box-sizing: border-box;
}
/* Block elements */
.block {
display: block;
/* Takes full width, creates line breaks */
}
/* Inline elements */
.inline {
display: inline;
/* Takes only needed width, no line breaks */
}
/* Inline-block elements */
.inline-block {
display: inline-block;
/* Behaves like inline but respects width/height */
}
/* Flexbox */
.flex {
display: flex;
/* Creates a flex container */
}
/* CSS Grid */
.grid {
display: grid;
/* Creates a grid container */
}
/* Hidden elements */
.hidden {
display: none;
/* Removes element from layout */
}
Notice how each display type affects the layout and spacing
Vertical margins between adjacent elements collapse to the larger value.
/* These margins will collapse */
.box1 {
margin-bottom: 20px;
}
.box2 {
margin-top: 30px;
}
/* Result: 30px space between boxes, not 50px */
Without border-box, padding and borders add to the specified width.
/* This might not work as expected */
.container {
width: 100%;
padding: 20px;
border: 1px solid #333;
}
/* Total width = 100% + 40px + 2px = over 100% */
Inline elements don't respect width, height, or vertical margins.
/* This won't work on inline elements */
span {
width: 100px; /* Ignored */
height: 50px; /* Ignored */
margin-top: 20px; /* Ignored */
margin-bottom: 20px; /* Ignored */
}
box-sizing: border-box
globally for easier calculations/* Modern CSS Reset */
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
/* Ensure consistent box model */
html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}