CSS Margin
h1 {
margin-bottom: 24px;
}
.card {
margin: 0 auto;
}
Let me ask you something.
Have you ever noticed that on a well designed page everything has room to breathe? The heading is not sitting right on top of the paragraph. The button has space above it. The sections are separated cleanly from each other.
That space between elements is margin. And it is one of the most used properties in all of CSS.
We just learned about padding which is the space inside an element. Margin is the space outside an element between it and everything around it.
Padding vs Margin
Before we go further let me make sure this distinction is crystal clear because it confuses almost everyone at the beginning.
Padding is space inside the element. Between the content and the element's own edges. It pushes inward.
Margin is space outside the element. Between the element and everything around it. It pushes outward.
Think of it like a person standing in a room. Padding is the space inside their personal bubble. Margin is the distance they keep from other people around them.
What is Margin
In CSS you add margin using the margin property. It works exactly the same way as padding — one value
for all sides, two values for top-bottom and left-right, or four values for each side individually.
Unlimited movies, TV shows, and more.
Starts at USD 2.99. Cancel anytime.
h1 { color: white; margin: 40px; } p { color: white; } body { background-color: #141414; }The heading now has 40px of space pushing everything away from it on all four sides.
Margin on Specific Sides
Just like padding you can control each side separately.
Unlimited movies, TV shows, and more.
Starts at USD 2.99. Cancel anytime.
h1 { color: white; margin-bottom: 8px; } p { color: white; margin-bottom: 24px; } button { background-color: #E50914; color: white; padding: 12px 32px; margin-top: 16px; } body { background-color: #141414; }The heading sits close to the paragraph below it. The paragraph has more space before the button. The button has extra space above it. Everything is controlled precisely.
In real projects you will find yourself using margin-top and margin-bottom far more than
margin-left and margin-right. Vertical spacing between sections and elements is what you
will be adjusting constantly. Get comfortable with these two first.
Two Values and Four Values
Just like padding, margin supports shorthand with two or four values.
Netflix
Unlimited movies, TV shows, and more.
h1 { color: white; margin: 40px 0; } p { color: white; } body { background-color: #141414; }40px on the top and bottom, 0 on the left and right. This is a very common pattern for headings and sections — you want vertical space but no extra horizontal space.
Writing margin: 40px 0 is something you will type hundreds of times in your career. It adds breathing
room above and below an element without pushing it left or right. Almost every section heading on every website uses
this pattern.
Margin Auto — Centering Elements
Here is one of the most useful tricks in CSS. If you want to center a block element horizontally on the page you can
use margin: auto.
Netflix
Unlimited movies, TV shows, and more.
The card is now centered on the page. The browser automatically splits the available space equally on the left and right sides.
This only works when the element has a defined width. Without a width the element takes up the full available space and there is nothing to center.
margin: 0 auto is one of the most classic CSS patterns. You will use it to center cards, containers,
forms, and page wrappers constantly. It is one of those things you just memorize early and use forever.
Removing Default Margins
Here is something that trips up almost every beginner. Browsers automatically add their own default margins to certain elements. Headings, paragraphs, the body itself — they all come with built in margin that you did not ask for.
This is why when you open a fresh HTML page there is already a gap around the content even though you did not add any CSS yet.
To remove all those defaults in one go you use this:
Netflix
Unlimited movies, TV shows, and more.
* { margin: 0; padding: 0; } h1 { color: white; } p { color: white; } body { background-color: #141414; padding: 24px; }Now everything starts from zero and you are in full control of every space on the page. This is called a CSS reset and it is something most developers add to the very top of their CSS file before writing anything else.
Always add that margin: 0 and padding: 0 reset at the top of your CSS file on every
project. Without it you are constantly fighting against browser defaults and wondering why things are not lining up
the way you expect. Starting from zero gives you a clean slate.
Building the Netflix Clone
Let us use margin to properly space out all the elements in our Netflix clone.
Netflix
Unlimited movies, TV shows, and more.
Starts at USD 2.99. Cancel anytime.
Ready to watch? Enter your email to create or restart your membership.
* { margin: 0; padding: 0; } body { font-family: "Roboto", Arial, sans-serif; font-size: 16px; padding: 40px; background-image: linear-gradient(to bottom, rgba(0,0,0,0.7), rgba(0,0,0,0.4)), url("https://assets.nflxext.com/ffe/siteui/vlv3/9ba9f0e2-b246-47f4-bd1f-3e84c23a5db8/web/NP-en-20251020-TRIFECTA-perspective_774f89dc-b75d-4cfc-b1fe-52086741fae3_large.jpg"); background-size: cover; background-position: center; background-repeat: no-repeat; text-align: center; } #logo { font-family: "Bebas Neue", sans-serif; font-size: 3rem; font-weight: 700; color: #E50914; letter-spacing: 4px; line-height: 1; margin-bottom: 16px; } h1 { font-family: "Bebas Neue", sans-serif; font-size: 3rem; font-weight: 700; color: white; letter-spacing: 2px; line-height: 1.2; margin: 60px 0 16px 0; } p { font-size: 1.1rem; font-weight: 300; color: white; line-height: 1.6; margin-bottom: 12px; } button { background-color: #E50914; color: white; font-size: 1rem; font-weight: 600; text-transform: uppercase; letter-spacing: 2px; padding: 12px 32px; margin-top: 24px; }Everything is reset to zero first. Then we add back exactly the spacing we want. The logo sits at the top with space below it. The main heading has a generous top margin pushing it down toward the center of the page. The paragraphs have breathing room between them. The button sits comfortably below the form with space above it.
Have anything to say about this lesson?
Your feedback helps improve these tutorials. If something was confusing or missing, let us know.