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?
Related
PART 1:
I am trying to make my parent div increase with height as the contents. it is a slide show contained in a parent div and the slide show is responsive. Everything sits fine but 10px padding is not reflected around the main container. Any help?
Example:
.mainer {
width: 100%;
background-color: red;
padding: 10px
}
.slide_wrapper {
width: 60%;
height: inherit;
top: 10px;
border: 0px solid black;
clear: left;
margin: 0 auto;
position: relative;
text-align: center;
}
<div class="mainer">
<div class="slide_wrapper">
<div class="carousel_slider">
<div class="item" style="width:100%;">
<img src="image.jpg" style="width:100%;height:auto" />
</div>
</div>
</div>
</div>
Part 2:
(Bonus for me.)
Assuming that I am also trying to include a different div class="rightbox" to the right of the container class="carousel_slider". Both of them have to stay inside the main container. How can I achieve this? Part 2 is just a curiosity for me.
Any help?.
Thanks and appreciation in Advance.
Michelle
Your problem might be related to using a clear without a clearfix. I'm not entirely sure why you have clear:left to begin with really, you don't have any other floated elements to clear in your html. Anyhow, when you clear an element, it no longer takes up space normally. The item within the cleared element will still take up space, but you can't apply margins and padding normally to a cleared element without clearfix. Here's one you can use easily.
http://nicolasgallagher.com/micro-clearfix-hack/
That being said, clear + clearfix is fairly depreciated and you may find better results elsewhere. Try making .mainer relatively positioned, and absolutely position .slide_wrapper inside of it. That should allow you to set the width to 60% still and align it relative to the top and left of .mainer. Perhaps look into flexbox if you aren't using old versions of internet explorer. I'm not going to explain all of flexbox here as other's have already done it and better, but it allows you to align items intelligently to their parent container and is particularly helpful if you element has siblings.
padding: 10px on the parent is being applied, but you have top: 10px on the img that is pushing it down 10px and messing up the bottom padding of the parent. To increase the space between the top of the image and the parent without messing up the bottom padding of the parent, use padding-top or margin-top instead of top on the img
And to put .rightbox beside of .carousel_slider, make the parent display: flex and it will put those 2 children in adjacent columns in a row.
.mainer {
width: 100%;
background-color: red;
padding: 10px;
}
.slide_wrapper {
height: inherit;
border: 0px solid black;
clear: left;
margin: 0 auto;
position: relative;
text-align: center;
padding-top: 10px;
}
.slide_wrapper {
display: flex;
align-items: flex-start;
justify-content: center;
}
.rightbox {
width: 100px;
height: 100px;
}
<div class="mainer">
<div class="slide_wrapper">
<div class="carousel_slider">
<div class="item" style="width:100%;">
<img src="http://kenwheeler.github.io/slick/img/fonz1.png" style="width:100%;height:auto"/>
</div>
</div>
<div class="rightbox">rightbox</div>
</div>
</div>
I am having trouble lining the edge of my div with a border to the edge of my picture.
Before I added the border to my second div it lined up perfectly, but now that I added it there is a gap between the edge of the div and the picture.
.headDiv {
position: relative;
width: 100%;
}
#heading {
font-family: fantasy;
font-size: 36;
position: absolute;
top: 25%;
left: 35%;
}
#navBarDiv {
height: 30px;
width: 100%;
background-color: limegreen;
margin-top: -4px;
border: 10px ridge gray;
}
<div class="headDiv">
<img id="imgBackgroud" src="http://vignette1.wikia.nocookie.net/unturned-bunker/images/4/4c/PEILEVEL.png/revision/latest?cb=20150321082112" alt="background behing heading" height="200px" width="100%" />
<h1 id="heading">Etowah Unturned Server</h1>
</div>
<div id="navBarDiv">
</div>
Add box-sizing: border-box to your bordered div. This will include any borders and padding in the width and height calculations.
#navBarDiv {
height: 30px;
width: 100%;
background-color: limegreen;
margin-top: -4px;
border: 10px ridge gray;
box-sizing: border-box; /* NEW */
}
Now the default box-sizing: content-box is overridden, and your borders don't expand the div.
https://css-tricks.com/box-sizing/
If you remove the width CSS rule for #navBarDiv you'll get the results you want. By default a div element will display at block-level. A block-level element always starts on a new line and takes up the full width available (stretches out to the left and right as far as it can). You overrode this rule by setting the width of #navBarDiv to 100%. So when you added a border with of 10px on both the left and right sides of #navBarDiv, the div element displayed 20 pixels longer than you wanted on the right side of the page.
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.
I am trying to get an Input and a Link element to sit side by side inside a Div element.
I wanted to have the Div and the Link to completely fill the Div element so you cannot see the containing Div around the edges of the Input and Link elements.
However if I set the width of my Link element any wider than 27px is falls to the next line even though I should have up to 31px left of the container Div to fit the Link element in. Relevent HTML and CSS below...
HTML
<section>
<img src="images/logo11w.png">
<div name="search">
<input>
</div>
</section>
CSS
div[name="search"]{
display: block;
margin: auto;
height: 27px;
width: 572px;
background-color: green;}
input{
display: inline-block;
width: 541px;
height: 27px;
border-style: none;
background-color: yellow;
padding: 0;
margin: 0;}
div[name="search"]>a{
display: inline-block;
width: 27px;
height: 27px;
background-color: red;
margin: 0;}
Could someone explain why my link element cannot be wider that 27px without falling to the next line? I am OK with having the width be only 27px I just wanted to understand why this is happening from a technical standpoint.
inline-block displays the whitespace in the actual layout in the rendered html... one whitespace is 4px . so if you want your link to have 31px width, you need to give margin-left:-4px or remove the whitespace in the actual layout.
<section>
<img src="images/logo11w.png">
<div name="search">
<input>
</div>
</section>
updated fiddle with no white space:
http://jsfiddle.net/x6Mq5/
updated fiddle with margin-left:-4px;
http://jsfiddle.net/x6Mq5/1/
Two divs are side by side, one is floating left with a width of 25%, the other just has a width of 75%. But when padding is applied on the right hand div, the padding doesn't work properly.
Here is a JSfiddle example:
http://jsfiddle.net/88upt/
<div id="top">
</div>
<div id="middle">
</div>
<div id="bottom">
</div>
CSS
#top {
float: left;
background-color: green;
width: 25%;
height: 100%;
}
#middle {
background-color: blue;
padding: 30px;
min-height: 30%;
}
#bottom {
background-color: red;
min-height: 70%;
}
Can someone explain to me why this is happening?
Thanks
Floating something is kind of like making it's position absolute. It will hover on top of it's neighboring containers. Add a margin-left equal to the width of the floated element to make the container the correct width.
http://jsfiddle.net/88upt/4/
#middle {
background-color: blue;
padding: 30px;
min-height: 30%;
margin-left:25%
}
EDIT Elaborating a bit more.
The floated element pushes the content of the sibling elements over. It will not push the left side of the content's element over. The padding is there it's just hidden by the floating element.
Add overflow = "auto" in the #middle.
#middle {
background-color: blue;
padding: 30px;
min-height: 30%;
overflow: auto;
}
In this way, you don't need to know the width of floating element.
Width doesn't factor in padding.
Source: http://www.w3schools.com/css/css_boxmodel.asp
The width only applies to content, not padding, border, or margin.
You can find more information here.