When to use margin vs padding in CSS
TL;DR: By default I use margin everywhere, except when I have a border or background and want to increase the space inside that visible box.
To me, the biggest difference between padding and margin is that vertical margins auto-collapse, and padding doesn't.
Consider two elements one above the other each with padding of 1em
. This padding is considered to be part of the element and is always preserved.
So you will end up with the content of the first element, followed by the padding of the first element, followed by the padding of the second, followed by the content of the second element.
Thus the content of the two elements will end up being 2em
apart.
Now replace that padding with 1em margin. Margins are considered to be outside of the element, and margins of adjacent items will overlap.
So in this example, you will end up with the content of the first element followed by 1em
of combined margin followed by the content of the second element. So the content of the two elements is only 1em
apart.
This can be really useful when you know that you want to say 1em
of spacing around an element, regardless of what element it is next to.
The other two big differences are that padding is included in the click region and background color/image, but not the margin.
div.box > div { height: 50px; width: 50px; border: 1px solid black; text-align: center; }div.padding > div { padding-top: 20px; }div.margin > div { margin-top: 20px; }
<h3>Default</h3><div class="box"> <div>A</div> <div>B</div> <div>C</div></div>
<h3>padding-top: 20px</h3><div class="box padding"> <div>A</div> <div>B</div> <div>C</div></div>
<h3>margin-top: 20px; </h3><div class="box margin"> <div>A</div> <div>B</div> <div>C</div></div>
What is difference between CSS margin and padding?
In short padding make "space" inside your box, and margin do this outside of your box.
This picture explain it pretty good :)
what the difference between margin and padding in Container widget using Flutter framework?
Padding: is the inner space of the element to the edge
Margin: is the space between widgets together
Difference between a View's Padding and Margin
To help me remember the meaning of padding, I think of a big coat with lots of thick cotton padding. I'm inside my coat, but me and my padded coat are together. We're a unit.
But to remember margin, I think of, "Hey, give me some margin!" It's the empty space between me and you. Don't come inside my comfort zone -- my margin.
To make it more clear, here is a picture of padding and margin in a TextView
:
xml layout for the image above
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#c5e1b0"
android:textColor="#000000"
android:text="TextView margin only"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#f6c0c0"
android:textColor="#000000"
android:text="TextView margin only"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#c5e1b0"
android:padding="10dp"
android:textColor="#000000"
android:text="TextView padding only"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f6c0c0"
android:padding="10dp"
android:textColor="#000000"
android:text="TextView padding only"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#c5e1b0"
android:textColor="#000000"
android:padding="10dp"
android:text="TextView padding and margin"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#f6c0c0"
android:textColor="#000000"
android:padding="10dp"
android:text="TextView padding and margin"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#c5e1b0"
android:textColor="#000000"
android:text="TextView no padding no margin"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f6c0c0"
android:textColor="#000000"
android:text="TextView no padding no margin"
android:textSize="20sp" />
</LinearLayout>
Related
- Gravity vs layout_gravity
- Match_parent vs wrap_content
what's the difference between padding and margin?
Padding is the space INSIDE an element (inside the border of the element).
Margin is the space OUTSIDE(Around) an element.
Android beginner difference between padding and margin
Padding
is for inside/within components. Eg. TextView
, Button
, EditText
etc.
Eg. space between the Text and Border
Margin
is to be applied for the on-outside of the components.
Eg. space between left edge of the screen and border of your component
Visual representation is great in : Difference between a View's Padding and Margin
With Padding
, i have seen a difference in 2.2, 2.3 and say 4.3, 4.4
in such cases:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="ASDFGHJKL" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:text="@string/hello_world" />
</RelativeLayout>
Also, check the use of dimens:
http://developer.android.com/guide/topics/resources/more-resources.html
Using Margin vs Padding?
Margin is the space around elements.
Padding is the space around the elements contents.
So margin is outside the element.
Padding is inside.
You can use margin when you are placing elements near each other
You can use padding when you are changing how a element interacts with other elements.
(not a good explanation)
Also pixel is the pixels on the screen.
Rem is "Relative to font-size of the root element" (w3schools) https://www.w3schools.com/cssref/css_units.asp
Hope this helps.
Margin or padding what is recommended?
One important difference (asides from margin means outside and padding means inside): Margins of adjacent elements overlap, when paddings don't. Take this example:
<p>Lorem ipsum</p>
<p>Dolor sic amet</p>
If you use margin:
p { margin: 10px 0; }
The space between these 2 paragraphs will be 10px.
But if you go:
p { padding: 10px 0; }
The contents will be 20px separated.
Related Topics
How to Make Ng-Select Remove-And-Read-Only
How to Scale an Image Proportionally Within a Table Cell of a Fixed Size
Bootstrap 4 - Change Position of Carousel Controls
Bootstrap 4 Table With One Column Larger Than Others
Is There a CSS Selector for Elements Containing Certain Text
How to Change Angular Material Table Row Height
How to Change the Color of an Svg Element
Fullcalendar: Change the Color for Specific Days
How to Change the Symbol When Click in CSS
How to Disable the Border on Bootstrap 4 Cards
Css Hover Drop Down Menu Disappears When Trying to Select Menu Option
How to Force Browsers to Print Background Images in CSS
Creating Css3 Circles Connected by Lines
Vuejs Vue-Router Linking an External Website
Bootstrap Take Full Width for Select Drop Down