Table of Contents
- Introduction to the Box Model
- Content Area
- Padding
- Border
- Margin
- Box Sizing
- Display Properties
- Width and Height
- Overflow
- Box Shadows
- Outline
- Border Radius
- Practical Examples
- Common Issues and Solutions
- Best Practices
Introduction to the Box Model
Every element in CSS is considered a rectangular box. The CSS Box Model describes how these boxes are structured and how they interact with each other. Understanding the box model is fundamental to creating layouts, spacing elements, and controlling element dimensions.
What is the Box Model?
The box model consists of four distinct areas:
┌─────────────────────────────────┐ │ Margin │ │ ┌───────────────────────────┐ │ │ │ Border │ │ │ │ ┌─────────────────────┐ │ │ │ │ │ Padding │ │ │ │ │ │ ┌───────────────┐ │ │ │ │ │ │ │ Content │ │ │ │ │ │ │ └───────────────┘ │ │ │ │ │ │ │ │ │ │ │ └─────────────────────┘ │ │ │ │ │ │ │ └───────────────────────────┘ │ │ │ └─────────────────────────────────┘
Box Model Components
/* The complete box model */
.box {
/* Content - where text and images appear */
width: 300px;
height: 200px;
/* Padding - space between content and border */
padding: 20px;
/* Border - line around padding */
border: 5px solid black;
/* Margin - space outside the border */
margin: 30px;
/* Background extends under padding but not margin */
background-color: #f0f0f0;
}
Box Model Calculation
/* Total width calculation */
.box {
width: 300px;
padding: 20px;
border: 5px solid black;
margin: 30px;
/* Content width: 300px */
/* Total element width: 300 + 40 (padding left/right) + 10 (border left/right) = 350px */
/* Total space occupied: 350 + 60 (margin left/right) = 410px */
}
Visualizing the Box Model
<div class="box-model-demo">
<style>
.box-model-demo {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 30px;
border-radius: 8px;
}
.demo-box {
width: 200px;
height: 100px;
padding: 20px;
border: 10px solid #ff6b6b;
margin: 30px;
background-color: #fff;
color: #333;
text-align: center;
line-height: 100px;
font-weight: bold;
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
}
.dimensions {
margin-top: 20px;
color: white;
font-family: monospace;
}
</style>
<div class="demo-box">Content</div>
<div class="dimensions">
<p>Content: 200x100px</p>
<p>Padding: 20px (total +40px)</p>
<p>Border: 10px (total +20px)</p>
<p>Margin: 30px (total +60px)</p>
<p>Total width: 200 + 40 + 20 = 260px</p>
<p>Total space: 260 + 60 = 320px</p>
</div>
</div>
Content Area
Setting Content Dimensions
/* Setting width and height */
.content-box {
width: 300px;
height: 200px;
background-color: #e0e0e0;
}
/* Using different units */
.fixed {
width: 500px;
height: 300px;
}
.percentage {
width: 50%;
height: 30%;
}
.viewport {
width: 80vw;
height: 60vh;
}
.auto-width {
width: auto; /* Default - fills available space */
max-width: 800px;
}
.min-max {
min-width: 200px;
max-width: 600px;
min-height: 100px;
max-height: 400px;
}
Content Overflow
/* Handling content that exceeds dimensions */
.content-container {
width: 300px;
height: 200px;
border: 1px solid #ccc;
/* Different overflow behaviors */
overflow: visible; /* Content spills out */
overflow: hidden; /* Content clipped */
overflow: scroll; /* Always show scrollbars */
overflow: auto; /* Scrollbars only when needed */
/* Separate horizontal/vertical control */
overflow-x: hidden;
overflow-y: auto;
}
/* Text overflow handling */
.text-container {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis; /* Show ... for truncated text */
}
Intrinsic Sizing
/* Intrinsic sizing keywords */
.fit-content {
width: fit-content; /* Shrinks to fit content */
height: fit-content;
}
.min-content {
width: min-content; /* Width of largest unbreakable word */
}
.max-content {
width: max-content; /* Width needed for content without wrapping */
}
/* Practical example */
.button {
width: fit-content;
padding: 10px 20px;
margin: 0 auto; /* Centers the button */
}
Padding
Padding is the space between the content and the border. It's inside the element and takes the background color.
Padding Shorthand
/* All four sides */
.equal-padding {
padding: 20px; /* All sides: 20px */
}
/* Vertical and horizontal */
.two-values {
padding: 10px 20px; /* Top/Bottom: 10px, Left/Right: 20px */
}
/* Top, horizontal, bottom */
.three-values {
padding: 10px 20px 15px; /* Top: 10px, Left/Right: 20px, Bottom: 15px */
}
/* Top, Right, Bottom, Left (clockwise) */
.four-values {
padding: 10px 15px 20px 25px; /* Top:10px, Right:15px, Bottom:20px, Left:25px */
}
Individual Padding Properties
/* Individual sides */
.padding-example {
padding-top: 10px;
padding-right: 15px;
padding-bottom: 20px;
padding-left: 25px;
}
/* Different units */
.padding-units {
padding: 1em 2rem 5% 10px;
}
/* Practical examples */
.card {
background: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.card-content {
padding: 20px; /* Comfortable spacing inside card */
}
.card-header {
padding: 15px 20px;
border-bottom: 1px solid #eee;
}
.card-footer {
padding: 15px 20px;
border-top: 1px solid #eee;
background-color: #f9f9f9;
}
Padding Effects
/* Visual examples */
.demo-container {
display: flex;
gap: 20px;
flex-wrap: wrap;
}
.demo-box {
width: 150px;
height: 150px;
background-color: #4ecdc4;
color: white;
display: flex;
align-items: center;
justify-content: center;
transition: padding 0.3s;
}
/* Different padding amounts */
.padding-0 {
padding: 0;
}
.padding-10 {
padding: 10px;
}
.padding-20 {
padding: 20px;
}
.padding-30 {
padding: 30px;
}
/* Padding on specific sides */
.padding-left {
padding-left: 30px;
}
.padding-vertical {
padding: 20px 0; /* Only top and bottom */
}
.padding-horizontal {
padding: 0 20px; /* Only left and right */
}
Border
Borders surround the padding and content. They can be styled in various ways.
Border Shorthand
/* Basic border */
.border-basic {
border: 1px solid black;
}
/* Different styles */
.border-styles {
border: 2px solid #007bff; /* Solid */
border: 2px dashed #dc3545; /* Dashed */
border: 2px dotted #28a745; /* Dotted */
border: 3px double #ffc107; /* Double */
border: 2px groove #6c757d; /* Groove - 3D effect */
border: 2px ridge #6c757d; /* Ridge - 3D effect */
border: 2px inset #6c757d; /* Inset - 3D effect */
border: 2px outset #6c757d; /* Outset - 3D effect */
border: none; /* No border */
}
Individual Border Properties
/* Border width */
.border-widths {
border-width: 2px;
border-width: thin; /* Usually 1px */
border-width: medium; /* Usually 3px */
border-width: thick; /* Usually 5px */
/* Per side */
border-top-width: 2px;
border-right-width: 4px;
border-bottom-width: 6px;
border-left-width: 8px;
}
/* Border style */
.border-styles-complete {
border-style: solid;
border-style: dashed;
border-style: dotted;
border-style: double;
border-style: groove;
border-style: ridge;
border-style: inset;
border-style: outset;
border-style: none;
border-style: hidden; /* Same as none but for table borders */
/* Different sides can have different styles */
border-style: solid dashed dotted double;
}
/* Border color */
.border-colors {
border-color: #007bff;
border-color: transparent;
border-color: currentColor; /* Matches text color */
/* Different sides */
border-color: red blue green yellow;
}
Border per Side
/* Individual sides */
.border-sides {
border-top: 2px solid #ff6b6b;
border-right: 3px dashed #4ecdc4;
border-bottom: 4px dotted #45b7d1;
border-left: 5px double #96ceb4;
}
/* Remove specific border */
.no-top-border {
border-top: none;
}
.no-right-border {
border-right: none;
}
/* Practical examples */
.card {
border: 1px solid #ddd;
border-top: 3px solid #007bff; /* Accent top border */
}
.divider {
border-bottom: 1px solid #eee;
margin: 20px 0;
}
.alert {
border-left: 4px solid #ff6b6b;
padding: 15px;
background-color: #fff5f5;
}
Border vs Outline
/* Border takes space in box model, outline doesn't */
.comparison {
width: 200px;
height: 100px;
margin: 20px;
display: inline-block;
}
.with-border {
border: 5px solid #007bff; /* Affects layout */
}
.with-outline {
outline: 5px solid #ff6b6b; /* Doesn't affect layout */
outline-offset: 5px; /* Space between element and outline */
}
/* Outline different styles */
.outline-styles {
outline: 2px solid #28a745;
outline: 2px dashed #dc3545;
outline: 2px dotted #ffc107;
outline-offset: 2px;
}
/* Focus indicators */
button:focus {
outline: 3px solid #007bff;
outline-offset: 2px;
}
/* Remove outline (use with caution - accessibility) */
.no-outline:focus {
outline: none;
/* Provide alternative focus indicator */
box-shadow: 0 0 0 3px #007bff;
}
Margin
Margin is the space outside the border. It's transparent and separates elements from each other.
Margin Shorthand
/* Similar shorthand to padding */
.margin-examples {
/* All four sides */
margin: 20px;
/* Top/Bottom, Left/Right */
margin: 10px 20px;
/* Top, Left/Right, Bottom */
margin: 10px 20px 15px;
/* Top, Right, Bottom, Left (clockwise) */
margin: 10px 15px 20px 25px;
}
/* Individual sides */
.margin-sides {
margin-top: 10px;
margin-right: 15px;
margin-bottom: 20px;
margin-left: 25px;
}
/* Different units */
.margin-units {
margin: 1em 2rem 5% 10px;
}
Auto Margins
/* Center block elements */
.center-block {
width: 300px;
margin-left: auto;
margin-right: auto;
}
/* Right align */
.right-align {
margin-left: auto;
margin-right: 0;
}
/* Left align (default) */
.left-align {
margin-right: auto;
}
/* Flexbox centering with margin */
.flex-container {
display: flex;
}
.flex-item {
margin: auto; /* Centers in flex container */
}
/* Practical example - navigation */
.nav-container {
display: flex;
align-items: center;
}
.logo {
margin-right: auto; /* Pushes other items to the right */
}
.nav-links {
display: flex;
gap: 20px;
}
Negative Margins
/* Negative margins pull elements in opposite direction */
.negative-margin {
margin-top: -20px; /* Pulls element up */
margin-left: -10px; /* Pulls element left */
}
/* Overlapping elements */
.overlap-example {
position: relative;
}
.box1 {
width: 200px;
height: 200px;
background-color: #ff6b6b;
}
.box2 {
width: 200px;
height: 200px;
background-color: #4ecdc4;
margin-top: -50px; /* Overlaps box1 by 50px */
margin-left: 50px;
}
/* Creating pull quotes */
.pull-quote {
width: 150px;
float: right;
margin-right: -30px; /* Extends into margin */
position: relative;
z-index: 1;
}
Margin Collapsing
/* Vertical margins collapse between block elements */
.collapse-demo {
background-color: #f0f0f0;
padding: 1px; /* Prevents margin collapse with container */
}
.box-a {
margin-bottom: 30px;
background-color: #ff6b6b;
height: 50px;
}
.box-b {
margin-top: 20px;
background-color: #4ecdc4;
height: 50px;
/* The gap between boxes will be 30px (max of 30 and 20), not 50px */
}
/* Margins don't collapse with padding or borders */
.with-border {
border-top: 1px solid transparent; /* Prevents margin collapse */
}
.with-padding {
padding-top: 1px; /* Prevents margin collapse */
}
/* Parent and child margins collapse */
.parent {
margin-top: 20px;
background-color: #f0f0f0;
}
.child {
margin-top: 30px; /* Collapses with parent margin - 30px total from parent's container */
}
/* Prevent margin collapse */
.parent {
padding-top: 1px; /* Creates separation */
/* or */
border-top: 1px solid transparent;
/* or */
overflow: auto;
}
/* Horizontal margins don't collapse */
.horizontal-demo {
display: flex;
gap: 20px; /* Better than margins for flex/grid */
}
.box-c {
margin-right: 20px; /* Doesn't collapse with next box's margin-left */
}
.box-d {
margin-left: 20px; /* Total gap is 40px, not 20px */
}
Box Sizing
The box-sizing property changes how the total width and height of an element are calculated.
Content-box vs Border-box
/* content-box (default) */
.content-box {
box-sizing: content-box;
width: 300px;
padding: 20px;
border: 5px solid black;
/* Total width = 300 + 40 + 10 = 350px */
}
/* border-box (recommended) */
.border-box {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 5px solid black;
/* Total width = 300px (content width shrinks to accommodate padding/border) */
/* Content width = 300 - 40 - 10 = 250px */
}
/* Global reset */
* {
box-sizing: border-box;
}
/* Or for specific components */
.component {
box-sizing: border-box;
}
/* Keep content-box for specific needs */
.media-object {
box-sizing: content-box; /* For elements where content size must stay fixed */
}
Comparison Demo
.demo-container {
display: flex;
gap: 40px;
margin: 30px 0;
}
.demo-box {
width: 300px;
height: 150px;
padding: 20px;
border: 5px solid #333;
background-color: #f0f0f0;
}
.content-box-demo {
box-sizing: content-box;
background-color: #ff6b6b20;
border-color: #ff6b6b;
}
.content-box-demo::after {
content: "Total: 350px";
display: block;
margin-top: 10px;
color: #ff6b6b;
}
.border-box-demo {
box-sizing: border-box;
background-color: #4ecdc420;
border-color: #4ecdc4;
}
.border-box-demo::after {
content: "Total: 300px";
display: block;
margin-top: 10px;
color: #4ecdc4;
}
Practical Implications
/* Responsive layouts with border-box */
.container {
width: 100%;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
box-sizing: border-box; /* Padding included in width */
}
/* Grid system with border-box */
.row {
display: flex;
flex-wrap: wrap;
margin: 0 -15px; /* Offset column padding */
}
.col {
flex: 1;
padding: 0 15px;
box-sizing: border-box;
}
/* Form elements benefit from border-box */
input, select, textarea {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
box-sizing: border-box; /* Keeps inputs inside containers */
}
Display Properties
The display property affects how the box model behaves.
Block vs Inline
/* Block elements */
.block-demo {
display: block;
width: 300px; /* Can set width */
height: 100px; /* Can set height */
margin: 10px; /* Margin works in all directions */
padding: 10px; /* Padding works in all directions */
}
/* Inline elements */
.inline-demo {
display: inline;
width: 300px; /* Ignored */
height: 100px; /* Ignored */
margin: 10px; /* Only horizontal margins work */
padding: 10px; /* Padding works but can overlap other lines */
}
/* Inline-block */
.inline-block-demo {
display: inline-block;
width: 200px; /* Can set width */
height: 100px; /* Can set height */
margin: 10px; /* Margin works in all directions */
padding: 10px; /* Padding works in all directions */
/* Behaves like inline but accepts block properties */
}
Display Comparison
.demo-wrapper {
border: 1px solid #ccc;
padding: 20px;
margin: 20px;
}
.block-item {
display: block;
background-color: #ff6b6b;
color: white;
padding: 10px;
margin: 10px;
width: 200px; /* Works */
}
.inline-item {
display: inline;
background-color: #4ecdc4;
color: white;
padding: 10px;
margin: 10px;
width: 200px; /* Ignored */
}
.inline-block-item {
display: inline-block;
background-color: #45b7d1;
color: white;
padding: 10px;
margin: 10px;
width: 200px; /* Works */
}
Other Display Values
/* None - element not displayed */
.hidden {
display: none; /* Element removed from layout */
}
/* Visibility hidden vs display none */
.invisible {
visibility: hidden; /* Element hidden but occupies space */
}
.gone {
display: none; /* Element removed from flow */
}
/* Table displays */
.table {
display: table;
}
.table-row {
display: table-row;
}
.table-cell {
display: table-cell;
padding: 10px;
border: 1px solid #ddd;
}
/* Flexbox */
.flex-container {
display: flex;
}
/* Grid */
.grid-container {
display: grid;
}
/* Flow root */
.new-formatting-context {
display: flow-root; /* Creates new block formatting context */
/* Alternative: overflow: auto; */
}
Width and Height
Understanding how dimensions are calculated.
Auto Width and Height
/* Auto behavior */
.auto-example {
width: auto; /* Default - fills available space */
height: auto; /* Default - shrinks to fit content */
}
/* Block-level elements with auto width */
.block-auto {
background-color: #f0f0f0;
margin: 10px;
/* Width auto = fills container minus margins */
}
/* Inline elements with auto width */
.inline-auto {
background-color: #ff6b6b;
display: inline;
/* Width auto = shrinks to fit content */
}
Percentage-Based Dimensions
/* Percentages relative to parent */
.parent {
width: 800px;
height: 500px;
}
.child {
width: 50%; /* 400px */
height: 50%; /* 250px */
}
/* Nested percentages */
.grandchild {
width: 50%; /* 50% of child's width = 200px */
height: 50%; /* 50% of child's height = 125px */
}
/* Viewport units */
.viewport-demo {
width: 50vw; /* 50% of viewport width */
height: 30vh; /* 30% of viewport height */
min-height: 100vh; /* Full viewport height at minimum */
}
Min and Max Dimensions
/* Minimum dimensions */
.responsive-box {
min-width: 300px; /* Won't shrink below 300px */
min-height: 200px; /* Won't shrink below 200px */
}
/* Maximum dimensions */
.responsive-box {
max-width: 800px; /* Won't grow above 800px */
max-height: 600px; /* Won't grow above 600px */
}
/* Combined for responsive design */
.content {
width: 100%;
max-width: 1200px;
min-width: 320px;
margin: 0 auto;
}
/* Image responsiveness */
img {
max-width: 100%; /* Never exceed container */
height: auto; /* Maintain aspect ratio */
}
/* Container queries with min/max */
.responsive-container {
width: clamp(300px, 50%, 800px);
/* Minimum 300px, preferred 50%, maximum 800px */
}
Overflow
Handling content that exceeds the element's dimensions.
Overflow Values
/* Overflow options */
.overflow-example {
width: 200px;
height: 100px;
border: 1px solid #ccc;
overflow: visible; /* Content spills out (default) */
overflow: hidden; /* Content clipped */
overflow: scroll; /* Always show scrollbars */
overflow: auto; /* Scrollbars only when needed */
/* Separate axes */
overflow-x: hidden;
overflow-y: auto;
}
Overflow Examples
.demo-overflow {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
}
.overflow-box {
width: 200px;
height: 100px;
border: 2px solid #333;
padding: 10px;
margin: 10px;
}
.visible-box {
overflow: visible;
background-color: #ff6b6b20;
}
.hidden-box {
overflow: hidden;
background-color: #4ecdc420;
}
.scroll-box {
overflow: scroll;
background-color: #45b7d120;
}
.auto-box {
overflow: auto;
background-color: #96ceb420;
}
Text Overflow
/* Text truncation */
.truncate-text {
width: 200px;
white-space: nowrap; /* Prevent wrapping */
overflow: hidden; /* Hide overflow */
text-overflow: ellipsis; /* Show ... */
}
/* Multi-line truncation */
.multiline-truncate {
display: -webkit-box;
-webkit-line-clamp: 3; /* Limit to 3 lines */
-webkit-box-orient: vertical;
overflow: hidden;
}
/* Custom overflow indicators */
.with-more-indicator {
position: relative;
overflow: hidden;
}
.with-more-indicator::after {
content: "...";
position: absolute;
bottom: 0;
right: 0;
background: linear-gradient(to left, white, transparent);
padding-left: 20px;
}
Scroll Behavior
/* Smooth scrolling */
.smooth-scroll {
scroll-behavior: smooth;
overflow-y: auto;
max-height: 400px;
}
/* Custom scrollbar */
.custom-scroll::-webkit-scrollbar {
width: 10px;
}
.custom-scroll::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 5px;
}
.custom-scroll::-webkit-scrollbar-thumb {
background: #888;
border-radius: 5px;
}
.custom-scroll::-webkit-scrollbar-thumb:hover {
background: #555;
}
Box Shadows
Adding depth and visual interest with shadows.
Basic Box Shadow
/* Simple shadow */
.shadow-simple {
box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
/* offset-x | offset-y | blur-radius | color */
}
/* Inset shadow */
.shadow-inset {
box-shadow: inset 0 0 10px rgba(0,0,0,0.3);
}
/* Spread radius */
.shadow-spread {
box-shadow: 5px 5px 10px 5px rgba(0,0,0,0.3);
/* offset-x | offset-y | blur | spread | color */
}
/* No offset - glow effect */
.shadow-glow {
box-shadow: 0 0 20px #007bff;
}
Multiple Shadows
/* Multiple shadows */
.shadow-multiple {
box-shadow:
0 5px 15px rgba(0,0,0,0.3),
0 0 0 2px #007bff inset,
0 0 0 4px rgba(0,123,255,0.3);
}
/* Layered shadows for depth */
.shadow-layered {
box-shadow:
0 1px 1px rgba(0,0,0,0.1),
0 2px 2px rgba(0,0,0,0.1),
0 4px 4px rgba(0,0,0,0.1),
0 8px 8px rgba(0,0,0,0.1),
0 16px 16px rgba(0,0,0,0.1);
}
Shadow Examples
/* Common UI shadows */
.card-shadow {
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.card-shadow-hover:hover {
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
transform: translateY(-2px);
transition: all 0.3s;
}
/* Button shadows */
.button-shadow {
box-shadow: 0 3px 0 #0056b3;
}
.button-shadow:active {
transform: translateY(3px);
box-shadow: none;
}
/* Floating action button */
.fab {
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
}
.fab:hover {
box-shadow: 0 8px 20px rgba(0,0,0,0.4);
}
/* Neumorphism style */
.neumorph {
background-color: #e0e0e0;
box-shadow:
12px 12px 24px rgba(0,0,0,0.1),
-12px -12px 24px rgba(255,255,255,0.7);
}
Outline
Outline is similar to border but doesn't affect layout.
Outline Properties
/* Outline shorthand */
.outline-basic {
outline: 2px solid #007bff;
outline-offset: 5px; /* Space between element and outline */
}
/* Outline styles */
.outline-styles {
outline-style: solid;
outline-style: dashed;
outline-style: dotted;
outline-style: double;
outline-style: groove;
outline-style: ridge;
outline-style: inset;
outline-style: outset;
}
/* Outline width */
.outline-width {
outline-width: 2px;
outline-width: thin;
outline-width: medium;
outline-width: thick;
}
/* Outline color */
.outline-color {
outline-color: #ff6b6b;
outline-color: currentColor;
outline-color: invert; /* Inverts background */
}
Outline vs Border
/* Outline doesn't affect layout */
.comparison-container {
display: flex;
gap: 30px;
}
.with-border {
border: 5px solid #ff6b6b;
padding: 10px;
}
.with-outline {
outline: 5px solid #4ecdc4;
outline-offset: 5px;
padding: 10px;
}
/* Outline for focus states */
:focus {
outline: 3px solid #007bff;
outline-offset: 2px;
}
/* Remove outline for mouse users only */
:focus:not(:focus-visible) {
outline: none;
}
:focus-visible {
outline: 3px solid #007bff;
outline-offset: 2px;
}
Border Radius
Creating rounded corners.
Basic Border Radius
/* Uniform radius */
.radius-uniform {
border-radius: 10px;
}
/* Percentage radius */
.radius-percent {
border-radius: 50%; /* Makes a circle/ellipse */
width: 200px;
height: 200px;
}
/* Individual corners */
.radius-individual {
border-top-left-radius: 10px;
border-top-right-radius: 20px;
border-bottom-right-radius: 30px;
border-bottom-left-radius: 40px;
}
/* Shorthand for all corners */
.radius-shorthand {
border-radius: 10px 20px 30px 40px; /* TL, TR, BR, BL */
}
/* Two values */
.radius-two {
border-radius: 10px 20px; /* TL/BR = 10px, TR/BL = 20px */
}
Elliptical Corners
/* Elliptical border radius */
.radius-ellipse {
border-radius: 50px / 20px; /* horizontal / vertical radius */
}
/* Different ellipses per corner */
.radius-ellipse-complex {
border-radius: 10px 20px 30px 40px / 5px 10px 15px 20px;
/* TL: 10px/5px, TR: 20px/10px, BR: 30px/15px, BL: 40px/20px */
}
/* Practical example - pill shape */
.pill {
border-radius: 9999px; /* Large value creates pill shape */
}
Border Radius Examples
/* Common shapes */
.circle {
width: 200px;
height: 200px;
border-radius: 50%;
}
.pill-button {
padding: 10px 30px;
border-radius: 9999px;
}
.rounded-card {
border-radius: 8px;
overflow: hidden; /* Ensures corners clip children */
}
.semi-circle-top {
width: 200px;
height: 100px;
border-radius: 100px 100px 0 0;
}
/* Only top corners rounded */
.rounded-top {
border-radius: 8px 8px 0 0;
}
/* Notch effect */
.notch {
border-radius: 0 0 50% 50% / 20px;
}
Practical Examples
Example 1: Card Component
<div class="card-demo">
<div class="card">
<img src="https://via.placeholder.com/300x200" alt="Card image" class="card-image">
<div class="card-content">
<h3 class="card-title">Card Title</h3>
<p class="card-text">This is a sample card showing the box model in action with padding, margin, border, and border radius.</p>
<button class="card-button">Read More</button>
</div>
</div>
</div>
<style>
.card-demo {
background-color: #f5f5f5;
padding: 40px;
display: flex;
justify-content: center;
}
.card {
width: 300px;
background-color: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
transition: transform 0.3s, box-shadow 0.3s;
box-sizing: border-box;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 8px 16px rgba(0,0,0,0.2);
}
.card-image {
width: 100%;
height: 200px;
object-fit: cover;
display: block;
}
.card-content {
padding: 20px;
}
.card-title {
margin: 0 0 10px 0;
font-size: 1.25rem;
color: #333;
}
.card-text {
margin: 0 0 20px 0;
color: #666;
line-height: 1.6;
}
.card-button {
background-color: #007bff;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 1rem;
transition: background-color 0.3s;
margin: 0;
}
.card-button:hover {
background-color: #0056b3;
}
</style>
Example 2: Grid Layout
<div class="grid-demo">
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
</div>
</div>
<style>
.grid-demo {
padding: 40px;
background-color: #f5f5f5;
}
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: white;
border-radius: 8px;
box-sizing: border-box;
}
.grid-item {
background-color: #007bff;
color: white;
padding: 30px 20px;
text-align: center;
font-size: 1.5rem;
font-weight: bold;
border-radius: 4px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
transition: all 0.3s;
box-sizing: border-box;
}
.grid-item:hover {
transform: scale(1.05);
box-shadow: 0 5px 15px rgba(0,123,255,0.4);
background-color: #0056b3;
}
</style>
Example 3: Form with Box Model
<div class="form-demo">
<form class="demo-form">
<h2>Contact Us</h2>
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" class="form-input" placeholder="Enter your name">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" class="form-input" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea id="message" class="form-textarea" placeholder="Your message"></textarea>
</div>
<button type="submit" class="form-button">Send Message</button>
</form>
</div>
<style>
.form-demo {
padding: 40px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
}
.demo-form {
width: 100%;
max-width: 500px;
background-color: white;
padding: 40px;
border-radius: 8px;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
box-sizing: border-box;
}
.demo-form h2 {
margin: 0 0 30px 0;
color: #333;
text-align: center;
}
.form-group {
margin-bottom: 25px;
}
.form-group label {
display: block;
margin-bottom: 8px;
color: #555;
font-weight: 500;
}
.form-input,
.form-textarea {
width: 100%;
padding: 12px;
border: 2px solid #e0e0e0;
border-radius: 4px;
font-size: 1rem;
transition: all 0.3s;
box-sizing: border-box;
}
.form-input:focus,
.form-textarea:focus {
outline: none;
border-color: #667eea;
box-shadow: 0 0 0 3px rgba(102,126,234,0.1);
}
.form-textarea {
height: 120px;
resize: vertical;
}
.form-button {
width: 100%;
padding: 14px;
background-color: #667eea;
color: white;
border: none;
border-radius: 4px;
font-size: 1.1rem;
font-weight: 600;
cursor: pointer;
transition: background-color 0.3s;
margin: 10px 0 0 0;
box-sizing: border-box;
}
.form-button:hover {
background-color: #5a67d8;
}
</style>
Example 4: Navigation Bar
<nav class="navbar-demo">
<div class="nav-container">
<div class="logo">Logo</div>
<ul class="nav-links">
<li><a href="#" class="nav-link active">Home</a></li>
<li><a href="#" class="nav-link">About</a></li>
<li><a href="#" class="nav-link">Services</a></li>
<li><a href="#" class="nav-link">Contact</a></li>
</ul>
<button class="nav-button">Sign In</button>
</div>
</nav>
<style>
.navbar-demo {
background-color: #333;
padding: 0 20px;
position: sticky;
top: 0;
z-index: 1000;
}
.nav-container {
max-width: 1200px;
margin: 0 auto;
display: flex;
justify-content: space-between;
align-items: center;
height: 60px;
box-sizing: border-box;
}
.logo {
color: white;
font-size: 1.5rem;
font-weight: bold;
padding: 0 10px;
}
.nav-links {
display: flex;
list-style: none;
margin: 0;
padding: 0;
height: 100%;
}
.nav-links li {
margin: 0;
height: 100%;
}
.nav-link {
color: white;
text-decoration: none;
padding: 0 20px;
height: 100%;
display: flex;
align-items: center;
transition: background-color 0.3s;
box-sizing: border-box;
}
.nav-link:hover,
.nav-link.active {
background-color: #555;
}
.nav-link.active {
border-bottom: 3px solid #007bff;
}
.nav-button {
background-color: #007bff;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
font-size: 1rem;
transition: background-color 0.3s;
margin: 0;
}
.nav-button:hover {
background-color: #0056b3;
}
@media (max-width: 768px) {
.nav-links {
display: none;
}
.nav-container {
justify-content: space-between;
}
}
</style>
Common Issues and Solutions
Issue 1: Unexpected Element Sizing
/* Problem */
.problem-box {
width: 300px;
padding: 20px;
border: 5px solid black;
/* Actual width: 300 + 40 + 10 = 350px (not 300px) */
}
/* Solution 1: Use border-box */
.solution-box {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 5px solid black;
/* Actual width: 300px (content shrinks) */
}
/* Solution 2: Calculate manually */
.calculated-box {
width: 270px; /* 300 - 20 - 10 */
padding: 10px;
border: 5px solid black;
}
Issue 2: Margin Collapse
/* Problem */
.container {
margin-top: 20px;
}
.container:first-child {
margin-top: 0; /* Doesn't work if parent has margin */
}
/* Solution 1: Add padding or border */
.container {
padding-top: 1px;
margin-top: 19px; /* Adjust to compensate */
}
/* Solution 2: Use overflow */
.container {
overflow: auto;
}
/* Solution 3: Use flex/grid */
.container {
display: flex;
flex-direction: column;
}
Issue 3: Overflow Problems
/* Problem - content spills out */
.container {
width: 200px;
height: 100px;
/* Content overflows if larger */
}
/* Solution 1: Handle overflow */
.container {
overflow: auto; /* Add scrollbars */
}
/* Solution 2: Allow height to adjust */
.container {
height: auto;
min-height: 100px;
}
/* Solution 3: Hide overflow */
.container {
overflow: hidden;
}
Issue 4: Inline Block Spacing
<!-- Problem -->
<div class="parent">
<div class="child">Item 1</div>
<div class="child">Item 2</div>
<div class="child">Item 3</div>
</div>
<style>
/* Problem - spaces between elements due to HTML whitespace */
.child {
display: inline-block;
width: 100px;
height: 100px;
background-color: #007bff;
}
/* Solution 1: Remove whitespace in HTML */
/* <div class="child">Item 1</div><div class="child">Item 2</div> */
/* Solution 2: Negative margin */
.parent {
margin-right: -4px; /* Compensate for whitespace */
}
/* Solution 3: Flexbox */
.parent {
display: flex;
}
/* Solution 4: Font size hack */
.parent {
font-size: 0;
}
.child {
font-size: 16px; /* Restore font size */
}
</style>
Issue 5: Percentages in Nested Elements
/* Problem */
.parent {
width: 500px;
}
.child {
width: 50%; /* 250px - works fine */
padding: 10%; /* 50px - based on parent width, not child! */
}
/* Solution - understand percentage behavior */
/* Padding percentages are always based on parent's width */
/* Height percentages can be tricky - need parent height */
Best Practices
1. Use Border-Box Globally
/* Global border-box reset */
*,
*::before,
*::after {
box-sizing: border-box;
}
/* This makes sizing predictable and layouts easier */
2. Understand Margin Collapse
/* Design with margin collapse in mind */
.stack > * + * {
margin-top: 1.5rem; /* Creates consistent spacing between stacked elements */
}
/* This is often better than margin-bottom on all elements */
3. Use Consistent Spacing
/* CSS variables for consistent spacing */
:root {
--space-xs: 0.25rem; /* 4px */
--space-sm: 0.5rem; /* 8px */
--space-md: 1rem; /* 16px */
--space-lg: 1.5rem; /* 24px */
--space-xl: 2rem; /* 32px */
--space-xxl: 3rem; /* 48px */
}
.card {
padding: var(--space-lg);
margin-bottom: var(--space-md);
}
.button {
padding: var(--space-sm) var(--space-lg);
}
4. Be Careful with Percentage Padding
/* Percentage padding is based on parent width */
.aspect-ratio-box {
width: 100%;
padding-bottom: 56.25%; /* 16:9 aspect ratio */
position: relative;
}
.aspect-ratio-content {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
5. Use Logical Properties for RTL Support
/* Instead of physical properties */
.card {
margin-left: 20px;
padding-right: 20px;
}
/* Use logical properties */
.card {
margin-inline-start: 20px; /* left in LTR, right in RTL */
padding-inline-end: 20px; /* right in LTR, left in RTL */
border-inline-start: 2px solid #007bff;
}
6. Debug with Outlines
/* Temporarily add for debugging */
* {
outline: 1px solid red; /* See all boxes */
}
/* Or specific elements */
.suspicious-element {
outline: 2px solid blue;
}
7. Mobile-First Approach
.element {
/* Base styles for mobile */
width: 100%;
padding: 10px;
margin: 5px 0;
/* Tablet */
@media (min-width: 768px) {
width: 50%;
padding: 15px;
margin: 10px;
}
/* Desktop */
@media (min-width: 1024px) {
width: 33.333%;
max-width: 400px;
}
}
Cheat Sheet
Box Model Properties
| Property | Description | Values |
|---|---|---|
width | Content width | auto, length, percentage |
height | Content height | auto, length, percentage |
padding | Space inside border | length, percentage |
border | Line around padding | width style color |
margin | Space outside border | auto, length, percentage |
Padding/Margin Shorthand
| Values | Result |
|---|---|
1 | All sides |
2 | Top/Bottom, Left/Right |
3 | Top, Left/Right, Bottom |
4 | Top, Right, Bottom, Left |
Border Shorthand
border: 1px solid black; border-width: 1px; border-style: solid; border-color: black;
Box Sizing
box-sizing: content-box; /* Default - width excludes padding/border */ box-sizing: border-box; /* Width includes padding/border */
Overflow Values
| Value | Description |
|---|---|
visible | Content spills out (default) |
hidden | Content clipped |
scroll | Always show scrollbars |
auto | Scrollbars only when needed |
Display Values
| Value | Behavior |
|---|---|
block | New line, full width, respects box model |
inline | No line break, content width, limited box model |
inline-block | Inline flow with block box model |
none | Removed from layout |
Box Shadow Syntax
/* offset-x | offset-y | blur-radius | spread-radius | color */ box-shadow: 5px 5px 10px 5px rgba(0,0,0,0.3); /* inset shadow */ box-shadow: inset 0 0 10px rgba(0,0,0,0.3); /* multiple shadows */ box-shadow: 0 5px 15px rgba(0,0,0,0.3), 0 0 0 2px #007bff inset;
Border Radius
/* Uniform */ border-radius: 10px; /* Percentages */ border-radius: 50%; /* Individual corners */ border-radius: 10px 20px 30px 40px; /* TL, TR, BR, BL */ /* Elliptical */ border-radius: 50px / 20px; /* horizontal / vertical */
Conclusion
The CSS Box Model is fundamental to web layout design. Understanding it thoroughly will help you create predictable, maintainable layouts.
Key Takeaways
- Every element is a box with content, padding, border, and margin
- Box-sizing controls how dimensions are calculated
- Margin collapses vertically between block elements
- Padding adds space inside, margin adds space outside
- Border and outline differ in layout impact
- Overflow controls content that exceeds dimensions
- Box shadows add depth without affecting layout
- Border radius creates rounded corners
Common Patterns
- Use
border-boxglobally for predictable sizing - Use
margin: autofor centering block elements - Use
overflow: autofor scrollable content - Use
border-radius: 50%for circles - Use
box-shadowfor depth and elevation
Debugging Tips
- Use browser dev tools to inspect box model
- Add temporary outlines to see element boundaries
- Check computed styles for actual dimensions
- Verify if margin collapse is occurring
- Test with different viewport sizes
Remember: The box model is your friend! Master it, and you'll have complete control over your layouts.
HTML & CSS Core Styling Guides – Cascade, Colors, Box Model, Components, Forms, Media & Navigation (Related to HTML & CSS Development)
HTML & CSS Cascade & Inheritance:
This guide explains how CSS rules are applied when multiple styles target the same HTML element. It covers the cascade process, inheritance of properties from parent elements, and how specificity and rule order determine which styles are finally applied. Understanding cascade and inheritance is essential because they control how styles flow through the HTML structure.
Read more: https://macronepal.com/bash/complete-guide-to-html-and-css-cascade-inheritance/
HTML & CSS Colors & Backgrounds:
This guide focuses on applying colors, gradients, and background styles to HTML elements. It explains how colors enhance visual appearance and how background properties help design attractive and readable web layouts.
Read more: https://macronepal.com/bash/complete-guide-to-html-and-css-colors-backgrounds/
HTML & CSS Box Model:
The box model explains how HTML elements are structured using content, padding, borders, and margins. It helps developers understand spacing and layout design, making it easier to control how elements appear and align on a webpage.
Read more: https://macronepal.com/bash/complete-guide-to-html-and-css-box-model/
HTML & CSS Reusable Components:
This guide explains how to create reusable HTML and CSS components such as buttons, cards, and navigation bars. Reusable components improve consistency and reduce repetitive coding in web development projects.
Read more: https://macronepal.com/bash/complete-guide-to-html-and-css-reusable-components/
HTML & CSS Forms & Validation:
This tutorial covers creating forms and validating user input using HTML and CSS. It explains how to ensure users enter correct information and how validation improves form usability and data accuracy.
Read more: https://macronepal.com/bash/complete-guide-to-html-and-css-forms-validation/
HTML & CSS Images & Media:
This guide explains how to add images, videos, and other media to HTML pages. It also covers responsive media handling to ensure images and videos display correctly on different screen sizes.
Read more: https://macronepal.com/bash/complete-guide-to-html-and-css-images-media/
HTML & CSS Links & Navigation:
This tutorial focuses on creating links and navigation menus that help users move between pages easily. It explains styling navigation bars and improving user experience through structured navigation.
Read more: https://macronepal.com/bash/complete-guide-to-html-and-css-links-navigation/
HTML & CSS Text & Content Styling:
This guide explains how to style text content using fonts, spacing, alignment, and typography techniques. Proper text styling improves readability and enhances the visual structure of web pages.
Read more: https://macronepal.com/bash/complete-guide-to-html-and-css-text-content/