The CSS margin properties define the space around elements.
Questions tagged [margin]
3723 questions
2597
votes
16 answers
What is the difference between `margin` and `padding` in CSS?
What is the difference between margin and padding in CSS?
In what kind of situations:
both work.
only margin is appropriate.
only padding is appropriate.

Alex Angas
- 59,219
- 41
- 137
- 210
584
votes
15 answers
Difference between a View's Padding and Margin
What is the difference between a View's Margin and Padding?

Ragunath Jawahar
- 19,513
- 22
- 110
- 155
511
votes
18 answers
Margin on child element moves parent element
I have a div (parent) that contains another div (child). Parent is the first element in body with no particular CSS style. When I set
.child
{
margin-top: 10px;
}
The end result is that top of my child is still aligned with parent. Instead of…

Robert Koritnik
- 103,639
- 52
- 277
- 404
424
votes
14 answers
Why does this CSS margin-top style not work?
I tried to add margin values on a div inside another div. All works fine except the top value, it seems to be ignored. But why?
What I expected:
What I get:
Code:
#outer {
width: 500px;
height: 200px;
background: #FFCCCC;
margin: 50px…

lejahmie
- 17,938
- 16
- 54
- 77
351
votes
17 answers
How do I center a SVG in a div?
I have a SVG that I am trying to center in a div. The div has a width of 900px. The SVG has a width of 400px. The SVG has its margin-left and margin-right set to auto. Doesn't work, it just acts as if the left margin is 0 (default).
Does Anyone know…

Don P
- 60,113
- 114
- 300
- 432
331
votes
6 answers
Why is there an unexplainable gap between these inline-block div elements?
I have two inline-block div elements, that are the same, positioned next to eachother. However there seems to be a mysterious space of 4 pixels between the two divs despite the margin being set to 0. There are no parent divs effecting them - What is…

Legatro
- 3,692
- 4
- 15
- 21
285
votes
11 answers
Set margins in a LinearLayout programmatically
I'm trying to use Java (not XML) to create a LinearLayout with buttons that fill the screen, and have margins. Here is code that works without margins:
LinearLayout buttonsView = new…

Timmmm
- 88,195
- 71
- 364
- 509
269
votes
12 answers
How to disable margin-collapsing?
Is there a way to disable margin-collapsing altogether? The only solutions I've found (by the name of "uncollapsing") entail using a 1px border or 1px padding. I find this unacceptable: the extraneous pixel complicates calculations for no good…

kjo
- 33,683
- 52
- 148
- 265
265
votes
6 answers
Margin-Top not working for span element?
Can someone tell me what I coded wrong? Everything is working, the only thing is that there is no margin at the top.
HTML:
Contact

user1548544
- 2,875
- 5
- 19
- 17
207
votes
7 answers
CSS margin terror; Margin adds space outside parent element
My css margins doesn't behave the way I want or expect them to. I seems like my header margin-top affect the div-tags surrounding it.
This is what I want and expect:
...but this is what I end up with:
Source:

lejahmie
- 17,938
- 16
- 54
- 77
204
votes
16 answers
How to set margin of ImageView using code, not xml
I want to add an unknown number of ImageView views to my layout with margin. In XML, I can use layout_margin like this:
There is ImageView.setPadding(), but no…

Bruce Lee
- 3,049
- 3
- 20
- 13
187
votes
9 answers
What does auto do in margin: 0 auto?
What does auto do in margin: 0 auto;?
I can't seem to understand what auto does. I know it sometimes has the effect of centring objects.

Jitendra Vyas
- 148,487
- 229
- 573
- 852
167
votes
15 answers
WPF: Grid with column/row margin/padding?
Is it easily possible to specify a margin and/or padding for rows or columns in a WPF Grid?
I could of course add extra columns to space things out, but this seems like a job for padding/margins (it will give much simplier XAML). Has someone derived…

Brad Leach
- 16,857
- 17
- 72
- 88
156
votes
18 answers
Can we define min-margin and max-margin, max-padding and min-padding in css?
Can we define min-margin and max-margin, max-padding and min-padding in CSS ?

omkar
- 1,778
- 3
- 13
- 15
145
votes
2 answers
How to add a simple 8dp header/footer to Android's RecyclerView?
Is there a way to add a simple header/footer to a RecyclerView?
Here you can see what I've got. The first Card touches the Toolbar
And here you can see what I would like to achieve:
8dp Padding between the bottom and the Card.
Methods I tried…

Philipp Schumann
- 1,790
- 2
- 12
- 12