In CSS, margin and padding control spacing externally and internally, respectively. 1. Margin sets space outside an element, affecting spacing between elements without altering its size. 2. Padding sets space inside an element, increasing its size and pushing content away from the edges. 3. Margins can collapse vertically and be negative, while padding cannot. 4. Use margin for spacing between elements and padding for internal spacing within an element.
When you're trying to control spacing in CSS, two properties you'll run into right away are margin
and padding
. They might look similar when you’re first learning, but they serve very different purposes. In short: margin is outside the box, padding is inside.

What margin does in CSS
Margin
controls the space outside of an element — the area between this element and other elements on the page. It doesn’t affect the element's own size, just how much breathing room it has from its neighbors.

- If you want to push another div away from this one, use
margin
. - Margins can collapse vertically (this means two vertical margins next to each other might merge into one), which sometimes surprises beginners.
- You can set all four sides (
margin-top
,margin-right
, etc.), or shorthand likemargin: 10px 20px;
.
For example:
.box { margin: 20px; }
This adds 20px of space around the entire box, pushing other content away.

What padding does in CSS
Padding
is the space inside the element — between the content and the border. When you add padding, you're effectively making the element itself bigger (assuming box-sizing
is set to content-box
, which is default).
- Padding affects background color and borders. If your box has a background, that background will extend into the padding area.
- Padding doesn’t push other elements away directly, but it pushes the content away from the edges.
Here’s a simple case:
.text-box { padding: 15px; }
Now, any text inside .text-box
will have 15px of space from the edge.
Visualizing the difference
Think of a picture frame:
- The margin is like the empty wall space around the frame — how far it sits from other frames.
- The padding is the matting inside the frame — the space between the photo and the glass/frame.
So if you increase the padding, the photo (your content) moves inward. If you increase the margin, the whole frame moves away from nearby objects.
When to use margin vs padding
Sometimes the choice isn't obvious. Here are a few practical tips:
- Use margin when you want space between elements.
- Use padding when you want internal spacing without changing layout flow too much.
- Want to center something? Try
margin: 0 auto;
. - Need more space inside a button so text doesn’t touch the edges? That’s padding.
Also keep in mind:
- Backgrounds and borders include padding.
- Margins can be negative, which lets you pull elements closer together.
- Padding cannot be negative.
Basically, think of margin as external spacing and padding as internal. Once you get used to that distinction, positioning and spacing elements gets a lot easier.
The above is the detailed content of Difference between margin and padding in css. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

CSS blocks page rendering because browsers view inline and external CSS as key resources by default, especially with imported stylesheets, header large amounts of inline CSS, and unoptimized media query styles. 1. Extract critical CSS and embed it into HTML; 2. Delay loading non-critical CSS through JavaScript; 3. Use media attributes to optimize loading such as print styles; 4. Compress and merge CSS to reduce requests. It is recommended to use tools to extract key CSS, combine rel="preload" asynchronous loading, and use media delayed loading reasonably to avoid excessive splitting and complex script control.

In the following tutorial, I will show you how to create Lottie animations in Figma. We'll use two colorful designs to exmplify how you can animate in Figma, and then I'll show you how to go from Figma to Lottie animations. All you need is a free Fig

ThebestapproachforCSSdependsontheproject'sspecificneeds.Forlargerprojects,externalCSSisbetterduetomaintainabilityandreusability;forsmallerprojectsorsingle-pageapplications,internalCSSmightbemoresuitable.It'scrucialtobalanceprojectsize,performanceneed

No,CSSdoesnothavetobeinlowercase.However,usinglowercaseisrecommendedfor:1)Consistencyandreadability,2)Avoidingerrorsinrelatedtechnologies,3)Potentialperformancebenefits,and4)Improvedcollaborationwithinteams.

CSSismostlycase-insensitive,butURLsandfontfamilynamesarecase-sensitive.1)Propertiesandvalueslikecolor:red;arenotcase-sensitive.2)URLsmustmatchtheserver'scase,e.g.,/images/Logo.png.3)Fontfamilynameslike'OpenSans'mustbeexact.

Autoprefixer is a tool that automatically adds vendor prefixes to CSS attributes based on the target browser scope. 1. It solves the problem of manually maintaining prefixes with errors; 2. Work through the PostCSS plug-in form, parse CSS, analyze attributes that need to be prefixed, and generate code according to configuration; 3. The usage steps include installing plug-ins, setting browserslist, and enabling them in the build process; 4. Notes include not manually adding prefixes, keeping configuration updates, prefixes not all attributes, and it is recommended to use them with the preprocessor.

CSScounterscanautomaticallynumbersectionsandlists.1)Usecounter-resettoinitialize,counter-incrementtoincrease,andcounter()orcounters()todisplayvalues.2)CombinewithJavaScriptfordynamiccontenttoensureaccurateupdates.

In CSS, selector and attribute names are case-sensitive, while values, named colors, URLs, and custom attributes are case-sensitive. 1. The selector and attribute names are case-insensitive, such as background-color and background-Color are the same. 2. The hexadecimal color in the value is case-sensitive, but the named color is case-sensitive, such as red and Red is invalid. 3. URLs are case sensitive and may cause file loading problems. 4. Custom properties (variables) are case sensitive, and you need to pay attention to the consistency of case when using them.
