, , padding, margin, 100vh and calc()
Consider the following code snippet:
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink;"></section>As expected, the body element fills the entire viewport in green and on top there is a section element in pink.
Now, suppose you want to do something else very simple like set a margin in the section element: style="min-height: 50px; background-color: pink; margin-bottom: 10px;". Unexpectedly, a blue strip from the html element appears in the bottom of the viewport.
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink; margin-bottom: 10px;"></section>Question 1) Why this behaviour? It doesn't make sense to me.
One way to correct this behavior is including padding and min-height calc() adjustments in the body element:
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
padding-bottom: 1px;
background-color: green;
min-height: calc(100vh - 1px);
}<section style="min-height: 50px; background-color: pink; margin-bottom: 10px;"></section>However, this solution requires such a gimmick that I'm not comfortable with.
Question 2 Is there a better solution? (ie: more intuitive and more readable)
html css
add a comment |
Consider the following code snippet:
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink;"></section>As expected, the body element fills the entire viewport in green and on top there is a section element in pink.
Now, suppose you want to do something else very simple like set a margin in the section element: style="min-height: 50px; background-color: pink; margin-bottom: 10px;". Unexpectedly, a blue strip from the html element appears in the bottom of the viewport.
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink; margin-bottom: 10px;"></section>Question 1) Why this behaviour? It doesn't make sense to me.
One way to correct this behavior is including padding and min-height calc() adjustments in the body element:
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
padding-bottom: 1px;
background-color: green;
min-height: calc(100vh - 1px);
}<section style="min-height: 50px; background-color: pink; margin-bottom: 10px;"></section>However, this solution requires such a gimmick that I'm not comfortable with.
Question 2 Is there a better solution? (ie: more intuitive and more readable)
html css
add a comment |
Consider the following code snippet:
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink;"></section>As expected, the body element fills the entire viewport in green and on top there is a section element in pink.
Now, suppose you want to do something else very simple like set a margin in the section element: style="min-height: 50px; background-color: pink; margin-bottom: 10px;". Unexpectedly, a blue strip from the html element appears in the bottom of the viewport.
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink; margin-bottom: 10px;"></section>Question 1) Why this behaviour? It doesn't make sense to me.
One way to correct this behavior is including padding and min-height calc() adjustments in the body element:
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
padding-bottom: 1px;
background-color: green;
min-height: calc(100vh - 1px);
}<section style="min-height: 50px; background-color: pink; margin-bottom: 10px;"></section>However, this solution requires such a gimmick that I'm not comfortable with.
Question 2 Is there a better solution? (ie: more intuitive and more readable)
html css
Consider the following code snippet:
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink;"></section>As expected, the body element fills the entire viewport in green and on top there is a section element in pink.
Now, suppose you want to do something else very simple like set a margin in the section element: style="min-height: 50px; background-color: pink; margin-bottom: 10px;". Unexpectedly, a blue strip from the html element appears in the bottom of the viewport.
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink; margin-bottom: 10px;"></section>Question 1) Why this behaviour? It doesn't make sense to me.
One way to correct this behavior is including padding and min-height calc() adjustments in the body element:
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
padding-bottom: 1px;
background-color: green;
min-height: calc(100vh - 1px);
}<section style="min-height: 50px; background-color: pink; margin-bottom: 10px;"></section>However, this solution requires such a gimmick that I'm not comfortable with.
Question 2 Is there a better solution? (ie: more intuitive and more readable)
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink;"></section>html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink;"></section>html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink; margin-bottom: 10px;"></section>html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink; margin-bottom: 10px;"></section>html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
padding-bottom: 1px;
background-color: green;
min-height: calc(100vh - 1px);
}<section style="min-height: 50px; background-color: pink; margin-bottom: 10px;"></section>html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
padding-bottom: 1px;
background-color: green;
min-height: calc(100vh - 1px);
}<section style="min-height: 50px; background-color: pink; margin-bottom: 10px;"></section>html css
html css
edited Nov 11 at 9:41
Temani Afif
64.4k93675
64.4k93675
asked Jan 18 at 3:31
Mark Messa
18812
18812
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You are facing a margin-collapsing issue. The bottom margin you applied to your section is collpasing with the bottom margin of the body and thus its applied to the body instead of the section.
As you can read here:
The top and bottom margins of blocks are sometimes combined
(collapsed) into a single margin whose size is the largest of the
individual margins (or just one of them, if they are equal), a
behavior known as margin collapsing.
Margin collapsing occurs in three basic cases:
Adjacent siblings
Parent and first/last child
Empty blocks
That's why in your case you have 10px of margin bottom that adds the scroll to your page since body has a min-height:100vh
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>To avoid such behavior you need to simply avoid margin to collpase. So you can include a small padding like you did or a border then don't forget to also add box-sizing:border-box to avoid changing the height and use calc.
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
border-bottom:1px solid transparent;
/* OR padding-bottom:1px */
box-sizing:border-box;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>You can also use flex as there is no margin collpasing with flex (check links below for more method):
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
display:flex;
flex-direction:column;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>Some links that may help you to get more information:
https://css-tricks.com/what-you-should-know-about-collapsing-margins/
How do I uncollapse a margin?
CSS margin terror; Margin adds space outside parent element
Margin on child element moves parent element
Margin collapsing in flexbox
See jsbin.com/qekonaruka/1/edit?html,output. If the bottom margins are collapsing, why is the floated yellow box pushed down by the size of the bottom margin of the section element?
– Alohci
Jan 18 at 8:36
@Alohci well i don't know if i can explain this well :) am not also able to find the similar question to this wher float is involved ... but it's somehow related to this Note that the positions of elements that have been collapsed through have no effect on the positions of the other elements with whose margins they are being collapsed; the top border edge position is only required for laying out descendants of these elements. that you can read here : w3.org/TR/CSS2/box.html#margin-properties
– Temani Afif
Jan 18 at 9:00
@Alohci well i see you have good experience too and you wrote some post about this :) so maybe you also have some clue ... i previously saw a question where there is margin-collpasing, clearance and floating involved. not able to find it :s
– Temani Afif
Jan 18 at 9:03
@Alohci So for me = inside the body the first one has margin pushing the second element and the first element is also that last in-flow element so the margin of this one will collapse with its parent and if we consider the spec [and if i understand well] the behavior of the child element doesn't change after this collpase
– Temani Afif
Jan 18 at 9:08
The choice of using float was that because it's not in the page flow, it doesn't (shouldn't) affect the margin collapse of the section and the body. The quote in your first comment relates to "other elements with whose margins they are being collapsed", but floats won't collapse their margins with anything, so I don't think that's it. Also there's no clearance anywhere, so that's not a factor. So I'm not convinced, but thanks for your response anyway.
– Alohci
Jan 18 at 10:04
|
show 5 more comments
it is because margin is used to set a spacing outside an element thus your element is getting bottom space of 10px on setting margin bottom , so if you want to avoid outer spacing than use padding

Mind to explain better? When padding was zero, margin sets outer spacing. When padding was different than zero, margin sets inner spacing. This is the expected behaviour? Sounds so bizarre ...
– Mark Messa
Jan 18 at 3:50
your section is a box ( just for an imagination) when you apply margin it gives space outside the box and when you give padding it gives space inside the box thus when you fix the height by 100 vh its makes a box of 100vh having a space from bottom 10px as you apply margin bottom
– cyrus
Jan 18 at 3:57
"when you apply margin it gives space outside the box" Ok, but how this space will be achieved seems to vary according whether padding of the outer container is zero or nonzero. Why?
– Mark Messa
Jan 18 at 4:10
you can see in image above black solid line is box outer dotted line is margin as i said and inner dotted line is padding from box i.e. the spacing between content and box
– cyrus
Jan 18 at 4:21
Ok, I'm aware of that image. However, how the browser is achieving this space for section element is changing whether its container (body element) is set to zero or nonzero padding ...
– Mark Messa
Jan 18 at 4:26
|
show 4 more comments
I also don't know why such behavior but you can try with following CSS to overcome this situation:
body {margin: 0; padding: 0; background-color: green; position:absolute; left:0; top:0; right:0; bottom:0; overflow:auto;}
The problem with this code is that you loose some flexibility (although I did not mentioned this constraint in the original question). For example: if the section element increases in height, the body element will not increase with it and the background will turn to blue after 100vh.
– Mark Messa
Jan 18 at 5:38
@MarkMessa Actually when content increase body scroll will appear accordingly. Or you can try html {margin: 0; padding: 0; background-color: blue; height:100vh;} body {margin: 0; padding: 0; background-color: green; min-height: 100%;}
– Hanif
Jan 18 at 5:44
"Actually when content increase body scroll will appear accordingly." Strange, in my browser the body element remains fixed with 100vh and html increases together with section content increase (blue background).
– Mark Messa
Jan 18 at 5:50
"Or you can try html" The problem withhtml {height: 100vt} body {min-height: 100%}is that it doesn't solve the blue strip issue forsection {margin-top: 10px}or when content increasessection {min-height: 1500px}.
– Mark Messa
Jan 18 at 5:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48313396%2fhtml-body-padding-margin-100vh-and-calc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are facing a margin-collapsing issue. The bottom margin you applied to your section is collpasing with the bottom margin of the body and thus its applied to the body instead of the section.
As you can read here:
The top and bottom margins of blocks are sometimes combined
(collapsed) into a single margin whose size is the largest of the
individual margins (or just one of them, if they are equal), a
behavior known as margin collapsing.
Margin collapsing occurs in three basic cases:
Adjacent siblings
Parent and first/last child
Empty blocks
That's why in your case you have 10px of margin bottom that adds the scroll to your page since body has a min-height:100vh
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>To avoid such behavior you need to simply avoid margin to collpase. So you can include a small padding like you did or a border then don't forget to also add box-sizing:border-box to avoid changing the height and use calc.
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
border-bottom:1px solid transparent;
/* OR padding-bottom:1px */
box-sizing:border-box;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>You can also use flex as there is no margin collpasing with flex (check links below for more method):
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
display:flex;
flex-direction:column;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>Some links that may help you to get more information:
https://css-tricks.com/what-you-should-know-about-collapsing-margins/
How do I uncollapse a margin?
CSS margin terror; Margin adds space outside parent element
Margin on child element moves parent element
Margin collapsing in flexbox
See jsbin.com/qekonaruka/1/edit?html,output. If the bottom margins are collapsing, why is the floated yellow box pushed down by the size of the bottom margin of the section element?
– Alohci
Jan 18 at 8:36
@Alohci well i don't know if i can explain this well :) am not also able to find the similar question to this wher float is involved ... but it's somehow related to this Note that the positions of elements that have been collapsed through have no effect on the positions of the other elements with whose margins they are being collapsed; the top border edge position is only required for laying out descendants of these elements. that you can read here : w3.org/TR/CSS2/box.html#margin-properties
– Temani Afif
Jan 18 at 9:00
@Alohci well i see you have good experience too and you wrote some post about this :) so maybe you also have some clue ... i previously saw a question where there is margin-collpasing, clearance and floating involved. not able to find it :s
– Temani Afif
Jan 18 at 9:03
@Alohci So for me = inside the body the first one has margin pushing the second element and the first element is also that last in-flow element so the margin of this one will collapse with its parent and if we consider the spec [and if i understand well] the behavior of the child element doesn't change after this collpase
– Temani Afif
Jan 18 at 9:08
The choice of using float was that because it's not in the page flow, it doesn't (shouldn't) affect the margin collapse of the section and the body. The quote in your first comment relates to "other elements with whose margins they are being collapsed", but floats won't collapse their margins with anything, so I don't think that's it. Also there's no clearance anywhere, so that's not a factor. So I'm not convinced, but thanks for your response anyway.
– Alohci
Jan 18 at 10:04
|
show 5 more comments
You are facing a margin-collapsing issue. The bottom margin you applied to your section is collpasing with the bottom margin of the body and thus its applied to the body instead of the section.
As you can read here:
The top and bottom margins of blocks are sometimes combined
(collapsed) into a single margin whose size is the largest of the
individual margins (or just one of them, if they are equal), a
behavior known as margin collapsing.
Margin collapsing occurs in three basic cases:
Adjacent siblings
Parent and first/last child
Empty blocks
That's why in your case you have 10px of margin bottom that adds the scroll to your page since body has a min-height:100vh
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>To avoid such behavior you need to simply avoid margin to collpase. So you can include a small padding like you did or a border then don't forget to also add box-sizing:border-box to avoid changing the height and use calc.
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
border-bottom:1px solid transparent;
/* OR padding-bottom:1px */
box-sizing:border-box;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>You can also use flex as there is no margin collpasing with flex (check links below for more method):
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
display:flex;
flex-direction:column;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>Some links that may help you to get more information:
https://css-tricks.com/what-you-should-know-about-collapsing-margins/
How do I uncollapse a margin?
CSS margin terror; Margin adds space outside parent element
Margin on child element moves parent element
Margin collapsing in flexbox
See jsbin.com/qekonaruka/1/edit?html,output. If the bottom margins are collapsing, why is the floated yellow box pushed down by the size of the bottom margin of the section element?
– Alohci
Jan 18 at 8:36
@Alohci well i don't know if i can explain this well :) am not also able to find the similar question to this wher float is involved ... but it's somehow related to this Note that the positions of elements that have been collapsed through have no effect on the positions of the other elements with whose margins they are being collapsed; the top border edge position is only required for laying out descendants of these elements. that you can read here : w3.org/TR/CSS2/box.html#margin-properties
– Temani Afif
Jan 18 at 9:00
@Alohci well i see you have good experience too and you wrote some post about this :) so maybe you also have some clue ... i previously saw a question where there is margin-collpasing, clearance and floating involved. not able to find it :s
– Temani Afif
Jan 18 at 9:03
@Alohci So for me = inside the body the first one has margin pushing the second element and the first element is also that last in-flow element so the margin of this one will collapse with its parent and if we consider the spec [and if i understand well] the behavior of the child element doesn't change after this collpase
– Temani Afif
Jan 18 at 9:08
The choice of using float was that because it's not in the page flow, it doesn't (shouldn't) affect the margin collapse of the section and the body. The quote in your first comment relates to "other elements with whose margins they are being collapsed", but floats won't collapse their margins with anything, so I don't think that's it. Also there's no clearance anywhere, so that's not a factor. So I'm not convinced, but thanks for your response anyway.
– Alohci
Jan 18 at 10:04
|
show 5 more comments
You are facing a margin-collapsing issue. The bottom margin you applied to your section is collpasing with the bottom margin of the body and thus its applied to the body instead of the section.
As you can read here:
The top and bottom margins of blocks are sometimes combined
(collapsed) into a single margin whose size is the largest of the
individual margins (or just one of them, if they are equal), a
behavior known as margin collapsing.
Margin collapsing occurs in three basic cases:
Adjacent siblings
Parent and first/last child
Empty blocks
That's why in your case you have 10px of margin bottom that adds the scroll to your page since body has a min-height:100vh
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>To avoid such behavior you need to simply avoid margin to collpase. So you can include a small padding like you did or a border then don't forget to also add box-sizing:border-box to avoid changing the height and use calc.
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
border-bottom:1px solid transparent;
/* OR padding-bottom:1px */
box-sizing:border-box;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>You can also use flex as there is no margin collpasing with flex (check links below for more method):
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
display:flex;
flex-direction:column;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>Some links that may help you to get more information:
https://css-tricks.com/what-you-should-know-about-collapsing-margins/
How do I uncollapse a margin?
CSS margin terror; Margin adds space outside parent element
Margin on child element moves parent element
Margin collapsing in flexbox
You are facing a margin-collapsing issue. The bottom margin you applied to your section is collpasing with the bottom margin of the body and thus its applied to the body instead of the section.
As you can read here:
The top and bottom margins of blocks are sometimes combined
(collapsed) into a single margin whose size is the largest of the
individual margins (or just one of them, if they are equal), a
behavior known as margin collapsing.
Margin collapsing occurs in three basic cases:
Adjacent siblings
Parent and first/last child
Empty blocks
That's why in your case you have 10px of margin bottom that adds the scroll to your page since body has a min-height:100vh
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>To avoid such behavior you need to simply avoid margin to collpase. So you can include a small padding like you did or a border then don't forget to also add box-sizing:border-box to avoid changing the height and use calc.
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
border-bottom:1px solid transparent;
/* OR padding-bottom:1px */
box-sizing:border-box;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>You can also use flex as there is no margin collpasing with flex (check links below for more method):
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
display:flex;
flex-direction:column;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>Some links that may help you to get more information:
https://css-tricks.com/what-you-should-know-about-collapsing-margins/
How do I uncollapse a margin?
CSS margin terror; Margin adds space outside parent element
Margin on child element moves parent element
Margin collapsing in flexbox
html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
border-bottom:1px solid transparent;
/* OR padding-bottom:1px */
box-sizing:border-box;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
border-bottom:1px solid transparent;
/* OR padding-bottom:1px */
box-sizing:border-box;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
display:flex;
flex-direction:column;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>html {
margin: 0;
padding: 0;
background-color: blue;
}
body {
margin: 0;
padding: 0;
background-color: green;
min-height: 100vh;
display:flex;
flex-direction:column;
}<section style="min-height: 50px; background-color: pink;margin-bottom: 10px;"></section>edited Jan 18 at 8:27
answered Jan 18 at 8:19
Temani Afif
64.4k93675
64.4k93675
See jsbin.com/qekonaruka/1/edit?html,output. If the bottom margins are collapsing, why is the floated yellow box pushed down by the size of the bottom margin of the section element?
– Alohci
Jan 18 at 8:36
@Alohci well i don't know if i can explain this well :) am not also able to find the similar question to this wher float is involved ... but it's somehow related to this Note that the positions of elements that have been collapsed through have no effect on the positions of the other elements with whose margins they are being collapsed; the top border edge position is only required for laying out descendants of these elements. that you can read here : w3.org/TR/CSS2/box.html#margin-properties
– Temani Afif
Jan 18 at 9:00
@Alohci well i see you have good experience too and you wrote some post about this :) so maybe you also have some clue ... i previously saw a question where there is margin-collpasing, clearance and floating involved. not able to find it :s
– Temani Afif
Jan 18 at 9:03
@Alohci So for me = inside the body the first one has margin pushing the second element and the first element is also that last in-flow element so the margin of this one will collapse with its parent and if we consider the spec [and if i understand well] the behavior of the child element doesn't change after this collpase
– Temani Afif
Jan 18 at 9:08
The choice of using float was that because it's not in the page flow, it doesn't (shouldn't) affect the margin collapse of the section and the body. The quote in your first comment relates to "other elements with whose margins they are being collapsed", but floats won't collapse their margins with anything, so I don't think that's it. Also there's no clearance anywhere, so that's not a factor. So I'm not convinced, but thanks for your response anyway.
– Alohci
Jan 18 at 10:04
|
show 5 more comments
See jsbin.com/qekonaruka/1/edit?html,output. If the bottom margins are collapsing, why is the floated yellow box pushed down by the size of the bottom margin of the section element?
– Alohci
Jan 18 at 8:36
@Alohci well i don't know if i can explain this well :) am not also able to find the similar question to this wher float is involved ... but it's somehow related to this Note that the positions of elements that have been collapsed through have no effect on the positions of the other elements with whose margins they are being collapsed; the top border edge position is only required for laying out descendants of these elements. that you can read here : w3.org/TR/CSS2/box.html#margin-properties
– Temani Afif
Jan 18 at 9:00
@Alohci well i see you have good experience too and you wrote some post about this :) so maybe you also have some clue ... i previously saw a question where there is margin-collpasing, clearance and floating involved. not able to find it :s
– Temani Afif
Jan 18 at 9:03
@Alohci So for me = inside the body the first one has margin pushing the second element and the first element is also that last in-flow element so the margin of this one will collapse with its parent and if we consider the spec [and if i understand well] the behavior of the child element doesn't change after this collpase
– Temani Afif
Jan 18 at 9:08
The choice of using float was that because it's not in the page flow, it doesn't (shouldn't) affect the margin collapse of the section and the body. The quote in your first comment relates to "other elements with whose margins they are being collapsed", but floats won't collapse their margins with anything, so I don't think that's it. Also there's no clearance anywhere, so that's not a factor. So I'm not convinced, but thanks for your response anyway.
– Alohci
Jan 18 at 10:04
See jsbin.com/qekonaruka/1/edit?html,output. If the bottom margins are collapsing, why is the floated yellow box pushed down by the size of the bottom margin of the section element?
– Alohci
Jan 18 at 8:36
See jsbin.com/qekonaruka/1/edit?html,output. If the bottom margins are collapsing, why is the floated yellow box pushed down by the size of the bottom margin of the section element?
– Alohci
Jan 18 at 8:36
@Alohci well i don't know if i can explain this well :) am not also able to find the similar question to this wher float is involved ... but it's somehow related to this Note that the positions of elements that have been collapsed through have no effect on the positions of the other elements with whose margins they are being collapsed; the top border edge position is only required for laying out descendants of these elements. that you can read here : w3.org/TR/CSS2/box.html#margin-properties
– Temani Afif
Jan 18 at 9:00
@Alohci well i don't know if i can explain this well :) am not also able to find the similar question to this wher float is involved ... but it's somehow related to this Note that the positions of elements that have been collapsed through have no effect on the positions of the other elements with whose margins they are being collapsed; the top border edge position is only required for laying out descendants of these elements. that you can read here : w3.org/TR/CSS2/box.html#margin-properties
– Temani Afif
Jan 18 at 9:00
@Alohci well i see you have good experience too and you wrote some post about this :) so maybe you also have some clue ... i previously saw a question where there is margin-collpasing, clearance and floating involved. not able to find it :s
– Temani Afif
Jan 18 at 9:03
@Alohci well i see you have good experience too and you wrote some post about this :) so maybe you also have some clue ... i previously saw a question where there is margin-collpasing, clearance and floating involved. not able to find it :s
– Temani Afif
Jan 18 at 9:03
@Alohci So for me = inside the body the first one has margin pushing the second element and the first element is also that last in-flow element so the margin of this one will collapse with its parent and if we consider the spec [and if i understand well] the behavior of the child element doesn't change after this collpase
– Temani Afif
Jan 18 at 9:08
@Alohci So for me = inside the body the first one has margin pushing the second element and the first element is also that last in-flow element so the margin of this one will collapse with its parent and if we consider the spec [and if i understand well] the behavior of the child element doesn't change after this collpase
– Temani Afif
Jan 18 at 9:08
The choice of using float was that because it's not in the page flow, it doesn't (shouldn't) affect the margin collapse of the section and the body. The quote in your first comment relates to "other elements with whose margins they are being collapsed", but floats won't collapse their margins with anything, so I don't think that's it. Also there's no clearance anywhere, so that's not a factor. So I'm not convinced, but thanks for your response anyway.
– Alohci
Jan 18 at 10:04
The choice of using float was that because it's not in the page flow, it doesn't (shouldn't) affect the margin collapse of the section and the body. The quote in your first comment relates to "other elements with whose margins they are being collapsed", but floats won't collapse their margins with anything, so I don't think that's it. Also there's no clearance anywhere, so that's not a factor. So I'm not convinced, but thanks for your response anyway.
– Alohci
Jan 18 at 10:04
|
show 5 more comments
it is because margin is used to set a spacing outside an element thus your element is getting bottom space of 10px on setting margin bottom , so if you want to avoid outer spacing than use padding

Mind to explain better? When padding was zero, margin sets outer spacing. When padding was different than zero, margin sets inner spacing. This is the expected behaviour? Sounds so bizarre ...
– Mark Messa
Jan 18 at 3:50
your section is a box ( just for an imagination) when you apply margin it gives space outside the box and when you give padding it gives space inside the box thus when you fix the height by 100 vh its makes a box of 100vh having a space from bottom 10px as you apply margin bottom
– cyrus
Jan 18 at 3:57
"when you apply margin it gives space outside the box" Ok, but how this space will be achieved seems to vary according whether padding of the outer container is zero or nonzero. Why?
– Mark Messa
Jan 18 at 4:10
you can see in image above black solid line is box outer dotted line is margin as i said and inner dotted line is padding from box i.e. the spacing between content and box
– cyrus
Jan 18 at 4:21
Ok, I'm aware of that image. However, how the browser is achieving this space for section element is changing whether its container (body element) is set to zero or nonzero padding ...
– Mark Messa
Jan 18 at 4:26
|
show 4 more comments
it is because margin is used to set a spacing outside an element thus your element is getting bottom space of 10px on setting margin bottom , so if you want to avoid outer spacing than use padding

Mind to explain better? When padding was zero, margin sets outer spacing. When padding was different than zero, margin sets inner spacing. This is the expected behaviour? Sounds so bizarre ...
– Mark Messa
Jan 18 at 3:50
your section is a box ( just for an imagination) when you apply margin it gives space outside the box and when you give padding it gives space inside the box thus when you fix the height by 100 vh its makes a box of 100vh having a space from bottom 10px as you apply margin bottom
– cyrus
Jan 18 at 3:57
"when you apply margin it gives space outside the box" Ok, but how this space will be achieved seems to vary according whether padding of the outer container is zero or nonzero. Why?
– Mark Messa
Jan 18 at 4:10
you can see in image above black solid line is box outer dotted line is margin as i said and inner dotted line is padding from box i.e. the spacing between content and box
– cyrus
Jan 18 at 4:21
Ok, I'm aware of that image. However, how the browser is achieving this space for section element is changing whether its container (body element) is set to zero or nonzero padding ...
– Mark Messa
Jan 18 at 4:26
|
show 4 more comments
it is because margin is used to set a spacing outside an element thus your element is getting bottom space of 10px on setting margin bottom , so if you want to avoid outer spacing than use padding

it is because margin is used to set a spacing outside an element thus your element is getting bottom space of 10px on setting margin bottom , so if you want to avoid outer spacing than use padding

edited Jan 18 at 4:18
answered Jan 18 at 3:40
cyrus
15110
15110
Mind to explain better? When padding was zero, margin sets outer spacing. When padding was different than zero, margin sets inner spacing. This is the expected behaviour? Sounds so bizarre ...
– Mark Messa
Jan 18 at 3:50
your section is a box ( just for an imagination) when you apply margin it gives space outside the box and when you give padding it gives space inside the box thus when you fix the height by 100 vh its makes a box of 100vh having a space from bottom 10px as you apply margin bottom
– cyrus
Jan 18 at 3:57
"when you apply margin it gives space outside the box" Ok, but how this space will be achieved seems to vary according whether padding of the outer container is zero or nonzero. Why?
– Mark Messa
Jan 18 at 4:10
you can see in image above black solid line is box outer dotted line is margin as i said and inner dotted line is padding from box i.e. the spacing between content and box
– cyrus
Jan 18 at 4:21
Ok, I'm aware of that image. However, how the browser is achieving this space for section element is changing whether its container (body element) is set to zero or nonzero padding ...
– Mark Messa
Jan 18 at 4:26
|
show 4 more comments
Mind to explain better? When padding was zero, margin sets outer spacing. When padding was different than zero, margin sets inner spacing. This is the expected behaviour? Sounds so bizarre ...
– Mark Messa
Jan 18 at 3:50
your section is a box ( just for an imagination) when you apply margin it gives space outside the box and when you give padding it gives space inside the box thus when you fix the height by 100 vh its makes a box of 100vh having a space from bottom 10px as you apply margin bottom
– cyrus
Jan 18 at 3:57
"when you apply margin it gives space outside the box" Ok, but how this space will be achieved seems to vary according whether padding of the outer container is zero or nonzero. Why?
– Mark Messa
Jan 18 at 4:10
you can see in image above black solid line is box outer dotted line is margin as i said and inner dotted line is padding from box i.e. the spacing between content and box
– cyrus
Jan 18 at 4:21
Ok, I'm aware of that image. However, how the browser is achieving this space for section element is changing whether its container (body element) is set to zero or nonzero padding ...
– Mark Messa
Jan 18 at 4:26
Mind to explain better? When padding was zero, margin sets outer spacing. When padding was different than zero, margin sets inner spacing. This is the expected behaviour? Sounds so bizarre ...
– Mark Messa
Jan 18 at 3:50
Mind to explain better? When padding was zero, margin sets outer spacing. When padding was different than zero, margin sets inner spacing. This is the expected behaviour? Sounds so bizarre ...
– Mark Messa
Jan 18 at 3:50
your section is a box ( just for an imagination) when you apply margin it gives space outside the box and when you give padding it gives space inside the box thus when you fix the height by 100 vh its makes a box of 100vh having a space from bottom 10px as you apply margin bottom
– cyrus
Jan 18 at 3:57
your section is a box ( just for an imagination) when you apply margin it gives space outside the box and when you give padding it gives space inside the box thus when you fix the height by 100 vh its makes a box of 100vh having a space from bottom 10px as you apply margin bottom
– cyrus
Jan 18 at 3:57
"when you apply margin it gives space outside the box" Ok, but how this space will be achieved seems to vary according whether padding of the outer container is zero or nonzero. Why?
– Mark Messa
Jan 18 at 4:10
"when you apply margin it gives space outside the box" Ok, but how this space will be achieved seems to vary according whether padding of the outer container is zero or nonzero. Why?
– Mark Messa
Jan 18 at 4:10
you can see in image above black solid line is box outer dotted line is margin as i said and inner dotted line is padding from box i.e. the spacing between content and box
– cyrus
Jan 18 at 4:21
you can see in image above black solid line is box outer dotted line is margin as i said and inner dotted line is padding from box i.e. the spacing between content and box
– cyrus
Jan 18 at 4:21
Ok, I'm aware of that image. However, how the browser is achieving this space for section element is changing whether its container (body element) is set to zero or nonzero padding ...
– Mark Messa
Jan 18 at 4:26
Ok, I'm aware of that image. However, how the browser is achieving this space for section element is changing whether its container (body element) is set to zero or nonzero padding ...
– Mark Messa
Jan 18 at 4:26
|
show 4 more comments
I also don't know why such behavior but you can try with following CSS to overcome this situation:
body {margin: 0; padding: 0; background-color: green; position:absolute; left:0; top:0; right:0; bottom:0; overflow:auto;}
The problem with this code is that you loose some flexibility (although I did not mentioned this constraint in the original question). For example: if the section element increases in height, the body element will not increase with it and the background will turn to blue after 100vh.
– Mark Messa
Jan 18 at 5:38
@MarkMessa Actually when content increase body scroll will appear accordingly. Or you can try html {margin: 0; padding: 0; background-color: blue; height:100vh;} body {margin: 0; padding: 0; background-color: green; min-height: 100%;}
– Hanif
Jan 18 at 5:44
"Actually when content increase body scroll will appear accordingly." Strange, in my browser the body element remains fixed with 100vh and html increases together with section content increase (blue background).
– Mark Messa
Jan 18 at 5:50
"Or you can try html" The problem withhtml {height: 100vt} body {min-height: 100%}is that it doesn't solve the blue strip issue forsection {margin-top: 10px}or when content increasessection {min-height: 1500px}.
– Mark Messa
Jan 18 at 5:59
add a comment |
I also don't know why such behavior but you can try with following CSS to overcome this situation:
body {margin: 0; padding: 0; background-color: green; position:absolute; left:0; top:0; right:0; bottom:0; overflow:auto;}
The problem with this code is that you loose some flexibility (although I did not mentioned this constraint in the original question). For example: if the section element increases in height, the body element will not increase with it and the background will turn to blue after 100vh.
– Mark Messa
Jan 18 at 5:38
@MarkMessa Actually when content increase body scroll will appear accordingly. Or you can try html {margin: 0; padding: 0; background-color: blue; height:100vh;} body {margin: 0; padding: 0; background-color: green; min-height: 100%;}
– Hanif
Jan 18 at 5:44
"Actually when content increase body scroll will appear accordingly." Strange, in my browser the body element remains fixed with 100vh and html increases together with section content increase (blue background).
– Mark Messa
Jan 18 at 5:50
"Or you can try html" The problem withhtml {height: 100vt} body {min-height: 100%}is that it doesn't solve the blue strip issue forsection {margin-top: 10px}or when content increasessection {min-height: 1500px}.
– Mark Messa
Jan 18 at 5:59
add a comment |
I also don't know why such behavior but you can try with following CSS to overcome this situation:
body {margin: 0; padding: 0; background-color: green; position:absolute; left:0; top:0; right:0; bottom:0; overflow:auto;}
I also don't know why such behavior but you can try with following CSS to overcome this situation:
body {margin: 0; padding: 0; background-color: green; position:absolute; left:0; top:0; right:0; bottom:0; overflow:auto;}
answered Jan 18 at 5:29
Hanif
2,5961611
2,5961611
The problem with this code is that you loose some flexibility (although I did not mentioned this constraint in the original question). For example: if the section element increases in height, the body element will not increase with it and the background will turn to blue after 100vh.
– Mark Messa
Jan 18 at 5:38
@MarkMessa Actually when content increase body scroll will appear accordingly. Or you can try html {margin: 0; padding: 0; background-color: blue; height:100vh;} body {margin: 0; padding: 0; background-color: green; min-height: 100%;}
– Hanif
Jan 18 at 5:44
"Actually when content increase body scroll will appear accordingly." Strange, in my browser the body element remains fixed with 100vh and html increases together with section content increase (blue background).
– Mark Messa
Jan 18 at 5:50
"Or you can try html" The problem withhtml {height: 100vt} body {min-height: 100%}is that it doesn't solve the blue strip issue forsection {margin-top: 10px}or when content increasessection {min-height: 1500px}.
– Mark Messa
Jan 18 at 5:59
add a comment |
The problem with this code is that you loose some flexibility (although I did not mentioned this constraint in the original question). For example: if the section element increases in height, the body element will not increase with it and the background will turn to blue after 100vh.
– Mark Messa
Jan 18 at 5:38
@MarkMessa Actually when content increase body scroll will appear accordingly. Or you can try html {margin: 0; padding: 0; background-color: blue; height:100vh;} body {margin: 0; padding: 0; background-color: green; min-height: 100%;}
– Hanif
Jan 18 at 5:44
"Actually when content increase body scroll will appear accordingly." Strange, in my browser the body element remains fixed with 100vh and html increases together with section content increase (blue background).
– Mark Messa
Jan 18 at 5:50
"Or you can try html" The problem withhtml {height: 100vt} body {min-height: 100%}is that it doesn't solve the blue strip issue forsection {margin-top: 10px}or when content increasessection {min-height: 1500px}.
– Mark Messa
Jan 18 at 5:59
The problem with this code is that you loose some flexibility (although I did not mentioned this constraint in the original question). For example: if the section element increases in height, the body element will not increase with it and the background will turn to blue after 100vh.
– Mark Messa
Jan 18 at 5:38
The problem with this code is that you loose some flexibility (although I did not mentioned this constraint in the original question). For example: if the section element increases in height, the body element will not increase with it and the background will turn to blue after 100vh.
– Mark Messa
Jan 18 at 5:38
@MarkMessa Actually when content increase body scroll will appear accordingly. Or you can try html {margin: 0; padding: 0; background-color: blue; height:100vh;} body {margin: 0; padding: 0; background-color: green; min-height: 100%;}
– Hanif
Jan 18 at 5:44
@MarkMessa Actually when content increase body scroll will appear accordingly. Or you can try html {margin: 0; padding: 0; background-color: blue; height:100vh;} body {margin: 0; padding: 0; background-color: green; min-height: 100%;}
– Hanif
Jan 18 at 5:44
"Actually when content increase body scroll will appear accordingly." Strange, in my browser the body element remains fixed with 100vh and html increases together with section content increase (blue background).
– Mark Messa
Jan 18 at 5:50
"Actually when content increase body scroll will appear accordingly." Strange, in my browser the body element remains fixed with 100vh and html increases together with section content increase (blue background).
– Mark Messa
Jan 18 at 5:50
"Or you can try html" The problem with
html {height: 100vt} body {min-height: 100%} is that it doesn't solve the blue strip issue for section {margin-top: 10px} or when content increases section {min-height: 1500px}.– Mark Messa
Jan 18 at 5:59
"Or you can try html" The problem with
html {height: 100vt} body {min-height: 100%} is that it doesn't solve the blue strip issue for section {margin-top: 10px} or when content increases section {min-height: 1500px}.– Mark Messa
Jan 18 at 5:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48313396%2fhtml-body-padding-margin-100vh-and-calc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown