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

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.

Related

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

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>

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)!

How to choose padding for vertical rhythm

I have a very fundamental question to choose padding size for vertical rhythm.
Let's say that:
.body{
font-size: 14px;
line-height: 18px;
}
And we have a div with content (div has a border):
<div class="content">
... a very large text content here ...
</div>
Which one of the following would be the right chose for vertical align, this one:
.content{ padding: 18px; .... }
or should I consider the difference (18-14)/2 = 2px, So:
.content{ padding: 16px 18px 16px 18px; .... }
or if both answers are wrong, would you please explain how to approach this issue.
You should set the same padding on top and the bottom of the container. example:
.content {
padding: 10px 0;
line-height: 14px;
}
<div class="content">
...some text here...
</div>
Also you can set the height of the div and then set the same line-height

Strange "sticky footer" issue (scroll bar and div overlap)

Based on this article I want to make sticky footer on my project. Please take a look at this link.
I can't understand why I'm getting vertical scrollbar, and why #page div goes under #footer
Your footer has a 1px border-top; your container has a 1px border; your nav has a margin-top... all of these affect the vertical height, so the overall height is pushed to more than 100% => vertical scrollbar.
You need to account for that when you set your padding/margin to offset for the extra pixels. Additionally, you're giving the footer element padding/margin. What you should do is use the footer element as a wrapper and then create an element within it with the proper padding/margin.
Here's how you can make it work...
1 change #nav styles to padding: padding: 10px 0 0 0;
2 get rid of the 1px border on your #container
3 change your footer to this...
<div id="footer">
<div id="footer-content">
© 2012 Code Arts
</div>
</div>
4 change your footer css to this:
#footer {
border-top: 1px solid #C9E0ED;
height: 53px; /* 20px padding-top + 20px padding-bottom + 13px line-height */
margin-top: -54px; /* height + 1px border-top */
position: relative;
clear: both;
}
#footer-content {
font-size: 13px;
line-height: 13px;
text-align: center;
}
Try this:
z-index: 2;
...on #footer.
Also, what in the world is margin-top:-32767px; supposed to do!?

div/CSS question regarding width and padding

I'm having some trouble with this code:
CSS:
div#header
{
width: 100%;
background-color: #252525;
padding: 10px 0px 10px 15px;
position: relative;
}
div#login
{
float: right;
position: absolute;
right: 10px;
top: 5px;
}
HTML:
<div id="header">
<img src="./img/logo.jpg" />
<div id="login">
<form id="header-login" action="#">
<input type="text" /> <input type="text" /> <input type="submit" value="LOGIN" />
</form>
</div>
</div>
The div id=header tag has a left padding of 15px. Because of this, the div itself stretches the width of the page, plus an extra 15px to the right, causing me to have a horizontal scrollbar. I've tried putting the header div inside a container div, with relative positioning, but the padding caused the header div to just overflow 15px over the container, still leaving me with the sidebar. Can someone help me with a better understanding? Thanks.
Try removing the 100% width of the header. Since divs are line elements, thats not needed.
Try the div header with this.
div#header { width: 100%; height:15px; background-color: #252525; padding: 10px 0px 10px; position: relative; }
I'm not really sure what you're trying to accomplish: but here's a starter:
block elements (like a div, for instance) always expand to the width of your container, unless you're using quirks mode in IE.
there is no point in using position absolute and float right on the same element. use margin to get the appropriate distances. A floated element do need some sizes, a width for instance.
If you want to floated element to be "at top", it need to be specified first in it's parent element. Meaning, put the div before the img.
div#header {
background-color: #252525;
padding: 10px 0px 10px 15px;
}
div#login {
width: 100px; /* use preferred size here. */
float: right;
margin-right: 10px;
margin-top: 5px;
}
Would 'overflow:hidden' on a properly sized container div work?