CSS ā Making Webpages Beautiful
intermediate25 XP
Borders, Padding & Margins ā Spacing Everything Out
Control exactly how much space is around and inside every element ā the secret to professional-looking layouts!
Borders, Padding & Margins ā Spacing Perfected! š
Every web element is like a box. CSS gives you three tools to control the space around that box.
The Three Spacing Properties
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā MARGIN (space outside) ā
ā āāāāāāāāāāāāāāāāāāāāāāāāāāāā ā
ā ā BORDER ā ā
ā ā āāāāāāāāāāāāāāāāāāāā ā ā
ā ā ā PADDING ā ā ā
ā ā ā āāāāāāāāāāāā ā ā ā
ā ā ā ā CONTENT ā ā ā ā
ā ā ā āāāāāāāāāāāā ā ā ā
ā ā āāāāāāāāāāāāāāāāāāāā ā ā
ā āāāāāāāāāāāāāāāāāāāāāāāāāāāā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
š¦ Padding ā Space INSIDE the border
CSS
.box {
padding: 20px; /* same on all sides */
padding: 10px 20px; /* top/bottom left/right */
padding-top: 10px; /* individual sides */
padding-right: 20px;
padding-bottom: 10px;
padding-left: 20px;
}š„ Border ā The line around the element
CSS
.box {
border: 2px solid black; /* width, style, color */
border: 4px dashed red; /* dashed border */
border: 3px dotted blue; /* dotted border */
border-radius: 10px; /* rounded corners! */
border-radius: 50%; /* makes a circle! ā */
}š© Margin ā Space OUTSIDE (pushes other elements away)
CSS
.box {
margin: 20px; /* same on all sides */
margin: 10px auto; /* auto centres horizontally! */
margin-top: 50px; /* push 50px from top */
}Width & Height
CSS
.card {
width: 300px; /* exact pixel width */
width: 50%; /* 50% of parent width */
max-width: 600px; /* never wider than this */
height: 200px;
}š Beautiful Card Example
HTML
<div class="game-card">
<h2>āļø Minecraft</h2>
<p>Build anything you can imagine!</p>
<button class="play-btn">Play Now</button>
</div>CSS
.game-card {
width: 280px;
background-color: #1e1e2e;
color: white;
border: 2px solid #7c3aed;
border-radius: 16px;
padding: 24px;
margin: 20px auto;
}
.play-btn {
background-color: #7c3aed;
color: white;
border: none;
border-radius: 8px;
padding: 10px 24px;
font-size: 16px;
}This creates a gorgeous purple game card! š®
Quick check
01/3
Which property adds space INSIDE an element (between the content and border)?