Understand margin, padding, border, and outline to master element spacing and layout
The CSS Box Model is a fundamental concept that describes how elements are structured and spaced on a webpage. Every HTML element is essentially a rectangular box with four main areas: content, padding, border, and margin.
Padding creates space inside an element, between the content and the border. It's part of the element's background.
/* Individual sides */
padding-top: 10px;
padding-right: 20px;
padding-bottom: 10px;
padding-left: 20px;
/* Shorthand - all sides */
padding: 15px;
/* Shorthand - vertical horizontal */
padding: 10px 20px; /* top/bottom left/right */
/* Shorthand - top right/left bottom */
padding: 10px 20px 15px;
/* Shorthand - top right bottom left (clockwise) */
padding: 10px 20px 15px 5px;
padding: 10px; - All sidespadding: 10px 20px; - Vertical | Horizontalpadding: 10px 20px 15px; - Top | Right/Left | Bottompadding: 10px 20px 15px 5px; - Top | Right | Bottom | Left (🕐 clockwise).box {
padding: 20px;
background-color: lightblue;
}
.button {
padding: 10px 25px; /* 10px top/bottom, 25px left/right */
}
The border is a line that goes around the padding and content. You can style its width, style, and color.
/* Individual properties */
border-width: 2px;
border-style: solid;
border-color: blue;
/* Shorthand */
border: 2px solid blue;
/* Individual sides */
border-top: 1px solid red;
border-right: 2px dashed green;
border-bottom: 3px dotted blue;
border-left: 4px double orange;
border-style: solid; /* Solid line */
border-style: dashed; /* Dashed line */
border-style: dotted; /* Dotted line */
border-style: double; /* Double line */
border-style: groove; /* 3D grooved */
border-style: ridge; /* 3D ridged */
border-style: inset; /* 3D inset */
border-style: outset; /* 3D outset */
border-style: none; /* No border */
border-style: hidden; /* Hidden border */
/* All corners */
border-radius: 10px;
/* Specific corners (top-left, top-right, bottom-right, bottom-left) */
border-radius: 10px 20px 30px 40px;
/* Circle */
border-radius: 50%;
/* Ellipse */
border-radius: 50px / 25px;
Margin creates space outside an element, between elements. Margins are transparent and don't have a background color.
/* Individual sides */
margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;
/* Shorthand (same as padding) */
margin: 20px; /* All sides */
margin: 10px 20px; /* Vertical | Horizontal */
margin: 10px 20px 15px; /* Top | Right/Left | Bottom */
margin: 10px 20px 15px 5px; /* Top | Right | Bottom | Left */
/* Auto - centering */
margin: 0 auto; /* Center horizontally */
/* Negative margins */
margin-top: -10px; /* Pulls element up */
/* Center block element horizontally */
.container {
width: 800px;
margin: 0 auto; /* Top/bottom: 0, Left/right: auto */
}
When two vertical margins meet, they collapse into a single margin equal to the larger of the two. This doesn't happen with horizontal margins.
The outline is drawn outside the border and doesn't affect the element's dimensions. Unlike borders, outlines don't take up space.
/* Individual properties */
outline-width: 2px;
outline-style: solid;
outline-color: red;
/* Shorthand */
outline: 2px solid red;
/* Outline offset */
outline-offset: 5px; /* Space between outline and border */
/* Remove default outline (accessibility concern!) */
button:focus {
outline: none; /* ⚠️ Provide alternative focus indicator */
}
The box-sizing property controls how the total width and height of an element is calculated.
/* Width/height applies to content only */
.box {
box-sizing: content-box; /* Default */
width: 200px;
padding: 20px;
border: 5px solid black;
}
/* Total width: 200 + 40 (padding) + 10 (border) = 250px */
/* Width/height includes padding and border */
.box {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid black;
}
/* Total width: 200px (padding and border included) */
Use border-box for easier sizing calculations:
* {
box-sizing: border-box;
}
.card {
width: 300px;
padding: 20px;
margin: 20px;
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.button {
padding: 12px 24px;
margin: 10px;
border: 2px solid #667eea;
border-radius: 25px;
background: white;
color: #667eea;
transition: all 0.3s;
}
.button:hover {
background: #667eea;
color: white;
}
.button:focus {
outline: 3px solid rgba(102, 126, 234, 0.3);
outline-offset: 2px;
}
/* Consistent spacing */
.m-1 { margin: 8px; }
.m-2 { margin: 16px; }
.m-3 { margin: 24px; }
.m-4 { margin: 32px; }
.p-1 { padding: 8px; }
.p-2 { padding: 16px; }
.p-3 { padding: 24px; }
.p-4 { padding: 32px; }
Q1: Which property creates space INSIDE an element?
Q2: What is the correct order of the box model from inside to outside?
Q3: Which box-sizing value includes padding and border in the width?
Q4: How do you center a block element horizontally?
Q5: What's the difference between border and outline?
margin: 0 auto for horizontal centeringbox-sizing: border-box to all elements