Why does bootstrap container change my form header margins? [duplicate] - html

This question already has answers here:
How to disable margin-collapsing?
(12 answers)
Closed 3 years ago.
I have the following HTML:
<div class="container">
<form class="form-base text-center" method="post" action="">
<h1 class="h3 mt-4 mb-3">E-mail Addresses</h1>
<p>The following e-mail addresses are associated with your account:</p>
</form>
</div>
With the following CSS:
html,
body {
height: 100%;
}
body {
display: -ms-flexbox;
display: flex;
-ms-flex-align: center;
align-items: center;
padding-top: 40px;
padding-bottom: 40px;
background-color: rgba(216, 240, 230, 1);
}
.form-base {
width: 100%;
max-width: 450px;
padding: 0px;
margin: auto;
background: #fff;
-webkit-border-radius: 10px 10px 10px 10px;
border-radius: 10px 10px 10px 10px;
-webkit-box-shadow: 0 30px 60px 0 rgba(0,0,0,0.3);
box-shadow: 0 30px 60px 0 rgba(0,0,0,0.3);
}
When I put the form inside a container div, the top margin for the h1 header inside the form seems to be measured from the top of the container and not the top of the form. Like in this fiddle: https://jsfiddle.net/a3e2cgLj/3/
When I remove the container div and the form exists by itself, the top margin for the first header inside the form is now applied from the top of the form (which is what I want). Like in this fiddle: https://jsfiddle.net/yfd3hxvc/
Notice the difference in margins between the two fiddles.
Can someone tell me why this is happening?

As per MDN Docs, this is a result of margin collapsing.
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. Note that the margins of floating
and absolutely positioned elements never collapse.
Of the three situations where margin collapsing occurs, the OP falls into No content separating parent and descendants which is self-explanatory and the below example depicts the same.
You may use padding to override this behavior, but with margin this is expected.
div {
margin: 2rem 0;
background: lavender;
}
p {
margin: 1rem;
background: yellow;
}
<div>This parent element contains two paragraphs!
<p>This paragraph has a <code>1rem</code> margin between it and the text above.</p>
<p>My bottom margin collapses with my parent, yielding a bottom margin of <code>2rem</code>.</p>
</div>

Related

Why does inline-block does not make div inline? [duplicate]

This question already has answers here:
Two divs side by side - Fluid display [duplicate]
(9 answers)
Closed 2 years ago.
I wanted to make an inline div using inline-block, but it doesn't work and either turns it turns one of the two divs below and the other above
#div-on-the-left {
background-color: #464886;
width: 200px;
height: 600px;
padding: 10px;
border: 10px double #2c2d54;
margin: 5px;
}
#big-div-on-the-right {
background-color: #AAABB8;
width: 600px;
height: 600px;
display: inline-block;
margin: 5px;
}
<div id="div-on-the-left">
<!--Some html-->
</div>
<div id="big-div-on-the-right">
<!--Some html-->
</div>
I also tried giving #div-on-the-left inline-block too, that brought #big-div-on-the-right above, but left a gap where #div-on-the-left was supposed to be and brought #div-on-the-left to the bottom, why is this happening?
YOu would be way better off using a grid or flexboxes. In this case flexboxes would be "easier" and "shorter". You main issue is, that both div-boxes have a different height. The left div-box have a height of 600px + (2x20px) = 640px because of the double border. the right div-box have a height of only 600px causing different line height and therefor will cause a line-break. Next, the minimum-width has to be set large enough to allow both boxes to be displayed next to each other.
In the snippet below, I wrapped both boxes inside a wrapper with a minimum width high enough to let them be displayed next to each other. Then I changed them to display: flex;.
The height for the right box was set to 640px becaue of the border mentioned above.
.wrapper {
min-width: 850px;
display: flex;
}
#div-on-the-left {
background-color: #464886;
width: 200px;
height: 600px;
padding: 10px;
border: 10px double #2c2d54;
margin: 5px;
}
#big-div-on-the-right {
background-color: #AAABB8;
width: 600px;
height: 640px;
margin: 5px;
}
<div class="wrapper">
<div id="div-on-the-left">
<!--Some html-->
</div>
<div id="big-div-on-the-right">
<!--Some html-->
</div>
</div>

Where does the space from the top-margin came from?

I started to learn CSS, and I got to that example in the internet:
https://www.w3schools.com/css/tryit.asp?filename=trycss_margin_shorthand
But I tried to play with it, and changed some things. Now the code looks like:
div#my {
border: 8px solid black;
margin-left: 0%;
margin-right: 0%;
height: 500px;
background-color: red;
}
div {
border: 5px solid black;
margin: inherit;
background-color: lightblue;
}
<div id="my">
<div>This div element has a top margin of 100px, a right margin of 150px, a bottom margin of 100px, and a left margin of 80px.</div>
</div>
What I see is a big blue block inside a big red block. The blue block collapse with his left and right sides to the red block's border, but from the top I have margin for some reason.
I think its because I chose inherit in the css element. But that says that I have a default margin to the page. Please, help here to understand.
As noone seems to have explained this properly I will try for you.
The inherit keyword in css means that you inherit the styles directly from it's parent. As you have set all divs to inherit their margins, they will inherit that property from their direct parent.
The final div (the div that is nested inside #my) will inherit from the #my div. - As you have not set any margin for top and bottom, this will also stay as inherit.
This means that the #my div will inherit the top and bottom margin (left and right have been set and so will not be inherited) from it's parent, which is the body tag.
The body tag has 8px of margin (depending on which browser you use - I use chrome) so this is inherited by all the divs, which is why you have 8px margin at the top of the inner div. If you want no margin on that final div, you can either remove the margin property altogether, or just set it to 0 instead of inherit.
So to summarise inner div inherits from parent div called my, which in turn inherits from parent body tag, which is set to 8px (or browser default) so your margin comes from your body tag.
More information on css inherit
There's default margin to body tag of 8px which gets attached to your child divs because of explicitly written rule margin: inherit. You have to change the margin form inherit to 0 or remove it, in order to child elements don't have weird (default) margins.
div#my {
border: 8px solid black;
margin-left: 0%;
margin-right: 0%;
height: 500px;
background-color: red;
}
div {
border: 5px solid black;
margin: 0;
background-color: lightblue;
}
<div id="my">
<div>This div element has a top margin of 100px, a right margin of 150px, a bottom margin of 100px, and a left margin of 80px.</div>
</div>
There is a default margin on the body which is also inherited in your case (with margin: inherit), you can reset it like this:
html,
body {
margin: 0;
}
div#my {
border: 8px solid black;
margin-left: 0%;
margin-right: 0%;
height: 500px;
background-color: red;
}
div {
border: 5px solid black;
margin: inherit;
background-color: lightblue;
}
<div id="my">
<div>This div element has a top margin of 100px, a right margin of 150px, a bottom margin of 100px, and a left margin of 80px.</div>
</div>
W3C schools explanation of inherit
The inherit keyword specifies that a property should inherit its value
from its parent element.
In your code you have inherited the margin property of ALL DIV.
The div with id "my": inherit margin top and bottom from body (8px of margin), left and right are set.
The div without id: inherit the margin top and bottom from div#id (always 8px) and the left and right from div#id (0% = 0px).
To better understand I added a different margin to body, and set a different margin left/right on div#my.
body {
margin:50px;
background-color:yellow;
}
div#my {
border: 8px solid black;
margin-left: 5px;
margin-right: 0%;
height: 500px;
background-color: red;
}
div {
border: 5px solid black;
margin: inherit;
background-color: lightblue;
}
<div id="my">
<div>This div element has a top margin of 100px, a right margin of 150px, a bottom margin of 100px, and a left margin of 80px.</div>
</div>

top margin does not work

Sorry, i am novice in CSS. I have the following html:
<div class="box-A" >Box A is here</div>
<div class="box-B" >Box B is here</div>
and i have tried to apply the following css to it:
.box-A{
background-color: red;
padding: 30px;
margin: auto;
border: 13px solid green;
margin-bottom: 40px;
}
.box-B{
background-color: blue;
padding: 40px;
margin-top: 140 px;
}
It works correctly for box-A meanwhile when i apply margin-top: 140 px; to box-B I expect to see a margin around 180px between 2 boxes.
But nothing happens. Can you please tell me why margin-top does not effect?
Do not use space between px and number.
.box-B{
background-color: blue;
padding: 40px;
margin-top: 140px;
}
regarding to w3 schools css margin-top property it can happen, that only the biggest of both values (top and bottom) is accepted.
Margin Collapse. Top and bottom margins of elements are sometimes collapsed into a single margin that is equal to the largest of the two margins.
This does not happen on horizontal margins (left and right)! Only vertical margins (top and bottom)!

Picture inside centered div shifted a bit

I need to display a logo in the middle of a header, so I put it inside a div with following properties:
header {
background:#840CFF;
width:100%;
padding: 30px;
text-align: center;
}
div.logo-main{
display: inline-block;
padding: 0px;
}
body {
margin:0;
padding: 0;
border: 0;
}
But log is actually shifted a bit to the left. Why?
UPD: HTML code:
<body>
<header>
<div class = "logo-main">
<img src="logo_forest.png">
</div>
</header>
</body>
Remove the padding on the left and right. Use padding: 30px 0 instead of padding: 30px
header {
background: #840CFF;
width: 100%;
padding: 30px 0;
text-align: center;
}
div.logo-main {
display: inline-block;
padding: 0px;
}
body {
margin: 0;
padding: 0;
border: 0;
}
<header>
<div class="logo-main">
<img src="http://placehold.it/300x300">
</div>
</header>
There's a few different methods in which to achieve this.
The method you're trying to do seems to be the simple text-align method.
Which makes an element 'flow' with the text and center itself in the middle.
The other is a little complicated (well I did when I first started using this method). You want to make the element have 'automatic' margins so that it positions itself in the center. Here's the simple method first
header{width:100%;padding:0;text-align:center;/*remove your 30px padding. It's what's causing the positioning error*/}
.logo-main{display:inline-block;}
Now for the slightly more complicated method :
header{position:relative;width:/*not sure you need to set a block element 100%?*/}
.logo-main{position:absolute;top:30px;/*what you set with padding*/left:0;right:0;margin:0px;auto;}
But back to the reason why you have a position error is because you're setting header at 100%. Then adding 30px to all sides. so that's 100% + (30px + 30px) meaning header would probably actually be larger than it's parent container by 60px. Making your .logo-main off center by 30px.

Why is float and padding combo causing element to push thru margin?

I have a footer that has three rows. Row one is two divs floated left. Row two is a 'divider' line that is 100 width of the footer. Row three will be 3 more divs floated left.
The problem is on the first row. I have a margin-top:40px; for the middle line. The first floated element sits on top as it should but the second floated element ( which is going to be a text box and has padding inside ) sits on top fine WITHOUT padding, but when I put the 10px padding in, it sits 40px above as it should, but adds extra margin to the elements around it.
.footer {
background-color: #172135;
padding: 40px;
}
.footer-links {
margin: 0px auto 0px auto;
float: left;
}
.middle-line {
width: 100%;
border: 1px solid #1889b4;
padding: 0;
margin-top: 40px;
}
.newsletter {
padding: 10px;
border: 1px solid #188ab4;
width: 300px;
font-family: 'rBblack';
font-size: 12px;
color: white;
text-transform: uppercase;
float: left;
}
<footer class="footer clear" role="contentinfo">
<div class="footer-row-1 clear">
<div class="footer-links">
stuff
</div>
<div class="newsletter">
Sign Up For Our Newsletter
</div>
</div>
<div class="footer-row-2 clear">
<div class="middle-line"></div>
</div>
<div class="footer-row=3 clear">
more stuff
</div>
</footer>
**** PLEASE NOTE ***** The code snippet is not an accurate representation as css reset and clearfix is missing so not correct. Someone else edited this and put it there....
Unless you tell it to, the browser will make the element the width you specify, and then add on the padding etc
If you set the border-sizing property this will prevent it from happening;
box-sizing: border-box;
Try adding that to your CSS declaration
You can compensate for the shifting by of the padding by adding either margin-top:-10px; or position: relative; top: -10px; to .newsletter.
.footer {
background-color: #172135;
padding: 40px;
}
.footer-links {
margin: 0px auto 0px auto;
float: left;
}
.middle-line {
width: 100%;
border: 1px solid #1889b4;
padding: 0;
margin-top: 40px;
}
.newsletter {
padding: 10px;
margin-top: -10px; /* negative or padding value - readjusts position back up */
border: 1px solid #188ab4;
width: 300px;
font-family: 'rBblack';
font-size: 12px;
color: white;
text-transform: uppercase;
float: left;
}
<footer class="footer clear" role="contentinfo">
<div class="footer-row-1 clear">
<div class="footer-links">
stuff
</div>
<div class="newsletter">
Sign Up For Our Newsletter
</div>
</div>
<div class="footer-row-2 clear">
<div class="middle-line"></div>
</div>
<div class="footer-row=3 clear">
more stuff
</div>
</footer>
After reviewing what you said about my old, now competently irrelevant answer, i think i found what your issue is.
padding:10px;
adds padding to ALL 4 sides. it is functionally equivalent to
padding: 10px 10px 10px 10px;
the newsletter div is now significantly Taller than the other stuff in the same div, and the browser is forced to compensate by making the container div bigger. the container div gains 20 pixels in height when you do this, which would appear to add additional margin to the other elements.
to remove this, you would instead want to use either of these
padding: 0 10px;
padding: 0 10px 0 10px;
as per http://www.w3schools.com/cssref/pr_padding.asp
either will add padding to the LEFT and RIGHT sides equal to 10px, but the top and bottom will remain 0. the newsletter div will no longer be over-sized, making the container div bigger, which will make it appear there is margin for the others.
Edit (additional options):
however, if you want to keep the top and bottom padding, your have 3 main options.
1) add the padding to the other div inside the parent as well as newsletter. they will line up with newsletter, and have the extra space above and below. you would likely want to shrink the middle div's height to compensate for the increase.
2) to completely remove the newsletter from its parent div. set the width of newsletter and its parent div so that they add up to 100% including padding and borders, or use box-sizing:border box, and float both left so that they line up horizontally. now you can make newsletter as big as you want, and it will not affect the others.
3) you fix the height of the parent div,so that newsletter can be bigger than its parent div, however this tends to cause problems with layouts if your not careful, as it may overlap.