What is margin and padding in html
- CSS Text Formatting
- CSS text-align-last Property
- CSS text-decoration Property
- CSS text-decoration-color Property
- CSS text-decoration-line Property
- CSS text-decoration-style Property
- CSS text-indent Property
- CSS text-justify Property
- CSS text-overflow Property
- CSS text-transform Property
- CSS text-shadow Property
- CSS letter-spacing Property
- CSS line-height Property
- CSS direction Property
- CSS word-spacing Property
- CSS Grid Layout Module
- CSS column-gap Property
- CSS gap Property
- CSS grid Property
- CSS grid-area Property
- CSS grid-auto-columns Property
- CSS grid-auto-flow Property
- CSS grid-auto-rows Property
- CSS grid-column Property
- CSS grid-column-end Property
- CSS grid-column-gap Property
- CSS grid-column-start Property
- CSS grid-gap Property
- CSS grid-row Property
- CSS grid-row-end Property
- CSS grid-row-gap Property
- CSS grid-row-start Property
- CSS grid-template Property
- CSS grid-template-areas Property
- CSS grid-template-columns Property
- CSS grid-template-rows Property
- Write an Interview Experience
- Share Your Campus Experience
- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Colors
- CSS Borders
- CSS Margins and Padding
- CSS Height and Width
- CSS Outline
- CSS Fonts
- CSS Icons
- CSS Links
- CSS Lists
- CSS Tables
- CSS Display property
- CSS max-width Property
- CSS Positioning Elements
- CSS z-index Property
- CSS Overflow
- CSS Float
- CSS Align
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo Elements
- CSS Opacity / Transparency
- CSS DropDowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selector
- CSS Counters
- CSS Website Layout
- CSS Units
- CSS | Specificity
- How to apply !important in CSS?
- CSS Text Formatting
- CSS text-align-last Property
- CSS text-decoration Property
- CSS text-decoration-color Property
- CSS text-decoration-line Property
- CSS text-decoration-style Property
- CSS text-indent Property
- CSS text-justify Property
- CSS text-overflow Property
- CSS text-transform Property
- CSS text-shadow Property
- CSS letter-spacing Property
- CSS line-height Property
- CSS direction Property
- CSS word-spacing Property
- CSS Grid Layout Module
- CSS column-gap Property
- CSS gap Property
- CSS grid Property
- CSS grid-area Property
- CSS grid-auto-columns Property
- CSS grid-auto-flow Property
- CSS grid-auto-rows Property
- CSS grid-column Property
- CSS grid-column-end Property
- CSS grid-column-gap Property
- CSS grid-column-start Property
- CSS grid-gap Property
- CSS grid-row Property
- CSS grid-row-end Property
- CSS grid-row-gap Property
- CSS grid-row-start Property
- CSS grid-template Property
- CSS grid-template-areas Property
- CSS grid-template-columns Property
- CSS grid-template-rows Property
Чем отличаются padding и margin
У этого элемента есть пока невидимая граница border . Если мы отобразим эту границу, то увидим, что она прилипла к тексту.
А если поставим два таких элемента рядом, они почти прилипнут друг к другу, потому что между ними нет пространства:
Padding
Чтобы отодвинуть рамку нужен padding — внутреннее пространство между содержимым элемента и его границей border .
⭐ Padding — внутренний отступ.
padding можно задать отдельно сверху, справа, снизу и слева (то есть по часовой стрелке, начиная сверху). Или задать отдельно для верха-низа и право-лево, или вообще одинаковый для всех сторон.
Или даже все стороны по отдельности — вот так. Это аналог выражения padding: 10px 20px 30px 40px;
Все варианты padding одной картинкой:
Margin
margin — пространство от border , padding или контента до соседних элементов. Задаётся по такой же логике и тоже по часовой стрелке.
margin тоже можно задавать отдельно для всех сторон или сделать сразу одинаковым.
Внутренние и внешние отступы одной картинкой:
⭐ padding — внутри элемента, margin — снаружи.
Больше статей о выборе
«Доктайп» — журнал о фронтенде. Читайте, слушайте и учитесь с нами.
CSS padding vs margin: how to decide which to use
It can be difficult to know when to use CSS padding vs margin — and vice versa. Here, we explain when to use one over the other.
Margins and padding are essential elements of spacing when it comes to web design.
If you know when and how to leverage each one, you’ll be well on your way to creating stunning, responsive websites that look great on any device. But mastering them can take some practice.
If you’re new to CSS or interested in learning more about margins vs padding, read on to learn precisely what they are and how to decide when to use each one.
Web design and coding basics
Before we dive into the nuances of margins and padding, let’s quickly review the basics of web development: HTML and CSS.
HTML (HyperText Markup Language) tells your web browser how to display a web page. CSS (Cascading Style Sheets) provides a set of rules that are applied to HTML and govern the style of a website. So, we rely on HTML to define the specific text for a header on a page, but we can use CSS to set a consistent font, text size, and color for all headers in the same class across a website.
The invention of CSS was a game changer for web development because it eliminated the need to write or rewrite an individual line of styling code for each element on every page of a website. CSS enables us to not only set consistent parameters for elements in the same class, but make changes to an entire class all at the same time.
What is the CSS box model?
CSS uses what’s called the box model to tell web browsers how to display and space different elements on a web page. All websites are made up of a series of boxes and these boxes have four major components.
- Content — The innermost part of the box. Content could be an image, text, heading, or something else.
- Padding — The inner space between the content and the border of your box.
- Border — The perimeter of the box. Borders can be invisible or they could be a thick colored line like the green one pictured above.
- Margin — The outer space (or lack of space) surrounding the box.
CSS boxes can be aligned vertically or horizontally. They can sit side-by-side, nest inside one another, and they can even overlap.
What’s the difference between margin and padding?
The main difference between padding and margin is that padding controls how much breathing room exists within a box, while margin controls how much breathing room or whitespace exists outside of a box.
Padding can be set to zero pixels and above. It can change its own element’s size and impact how much of a fill color or background image is visible within a box.
Margins, on the other hand, are essentially invisible barriers around a box. They define how an element interacts with adjacent elements or the edge of a screen. Margins can be set to zero and above like padding. But unlike padding, margins can also be set to auto and can even have negative values.
Similarities between margin vs padding
In certain situations you can use either margin or padding to achieve similar results — and this is what makes the box model a bit confusing. Here’s an example.
The image above shows a set of nested boxes and their corresponding spacing in the Webflow Designer. We have a container box with a solid black border and light blue fill or background color. Within that container, there’s a div block containing a logo image (content), zero pixels of padding, a dotted black border, and a 100 pixel margin.
But we could achieve the exact same appearance using the container box’s padding instead of the div block’s margin. In the image below, we’ve increased the container’s padding from zero to 100 pixels and decreased the div block’s margin from 100 pixels to zero.
Sometimes, there isn’t one correct answer as to whether you should use margin or padding. So, don’t be afraid to play around with both options. Just make sure you review the appearance of your design on different screen sizes and adjust as needed to ensure your responsive web design looks great on any device.
As you can see in the image above, while 100 pixels of margin can look great on a large computer screen, your content may become imperceptibly small when viewed on a cell phone.
From the fundamentals to advanced topics — learn how to build sites in Webflow and become the designer you always wanted to be.