setting the width of my footer to 100% shows a scrollbar - css

Although I set the width of my footer to 100%, there it extends to more than 100% having a scroll bar in terms of width. Any ideas why? I know the problem is the width because when I remove the 100%, it does not show the scroll bar. The page is broken down to body>wrapper>footer
Here is my code:
#footer {
margin-top: 30px;
color: white !important;
padding-bottom: 15px;
background: black;
text-align: center;
padding: 20px;
height: 40px;
min-width: 1000px;
width:100%;
position:absolute;
bottom:0;
}
And there is the body css:
body {
font: normal 12pt Georgia, serif;
color: #111;
background: #990000;
margin: 0 auto;
text-align: center;
background-position: 50% 50%;
min-height: 100%;
margin:0;
padding:0;
height:100%;
}
And the wrapper css:
#wrapper {
min-height:100%;
position:relative;
}

You have padding set in your footer's css. That adds up to the width and makes it bigger than 100%. That's why you are seeing a scrollbar.
Replace the padding with these following lines.
padding-top:20px;
padding-bottom:20px;
Also by setting footer div's min width to 1000px, you will get the scrollbar in browser screens narrower than 1000px.

Many browsers have a default margin around the BODY element, which adds to the width.

Most likely this is due to how the default Box Model works in html pages: after the width is set to 100% for content, the borders, margins and paddings are added, increasing the final width beyond 100%.
For modern browsers: hail box-sizing!
See this jsfiddle with your original code.
See this newer version with box sizing set to border-box (only works in newer browsers). This version doesn't show a horizontal scrollbar (I made the min-width a lot smaller, or it would throw off the example in jsfiddle).
For older browsers
If you want to fix this for older browsers you'll have to do something about the padding in your CSS. Remove it from the footer, and place a "footer-content" div inside with margins equal to your old paddings. E.g.:
#footer {
/* padding: 20px; removed! */
}
#footer-content {
margin: 20px;
}

This is happening because of the padding. See the illustration of your problem here.
When you use padding, the size gets added to the total height and width respectively.
Removing the padding will fix your problem. Demo
#footer {
margin-top: 30px;
color: white !important;
background: black;
text-align: center;
height: 40px;
min-width: 1000px;
width:100%;
position:absolute;
bottom:0;
}
Another good solution is to make the browser treat your element differently. by using box-sizing property.
#footer {
/* Add box-sizing */
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; /* Opera/IE 8+ */
margin-top: 30px;
color: white !important;
padding-bottom: 15px;
background: black;
text-align: center;
padding: 20px;
height: 40px;
min-width: 1000px;
width:100%;
position:absolute;
bottom:0;
}
Demo

Just add 0 padding, 0 border and 0 margin to all elements at start.
* {
padding: 0;
border: 0;
margin: 0;
}

Related

Sticky header covering vertical scrollbar

My sticky header covers the vertical scrollbar, is there a way to fix this?
URL: http://jlwebdesigns.co.uk/
Header code (using a HTML5 tag)
header {
background: none repeat scroll 0 0 #283744;
border-bottom: 4px solid #4F5B66;
height: 97px;
margin-top: 0;
position: fixed;
top: 0;
width: 100%;
z-index: 999;
}
Yeah make that element have a higher z-index and make sure you set a positioning such as relative or what not to that element.
header {
background: none repeat scroll 0 0 #283744;
border-bottom: 4px solid #4F5B66;
height: 97px;
margin-top: 0;
position: fixed;
top: 0;
width: 100%;
z-index: 999;
}
Underlying element
#underlyingelementwithscrolls{
position:relative;
z-index:1000;/*higher than 999 since header has it*/
}
in your case you have overlayed the bodies scroller
do this and it should get fixed
body{
overflow:hidden;}
html{
overflow-y:scroll;}
solution is not very neat but give margin-top: 97px; to the below div....based on ruddy's fiddle :
here is a demo
After reviewing your page you can change a couple of things...
Your banner is at 100% but it also has a padding which makes your banner to exeed the 100%. You try to avoid this by putting the overflow hidden in the html, body but that makes your header to overlap the scroll bar.
Solution:
html, body {
width:100%;
overflow:hidden; //remove this
}
#homeBanner {
width: 100%;
background: url(../img/bannerHolder.jpg) center no-repeat #558582;
text-align: center;
margin-top: 100px;
padding: 13% 2%;
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; /* Opera/IE 8+ */
}
I looked at the site and a simple fix is to put:
margin-top: 97px; // if header has some padding so count that too.
Put that margin on the section in the CSS. This will fix the scrollbar but will break the logo. You will have to replace some stuff because of the new margin.
Here is a demo using the margin-top. You can see the text under the header. Take the margin away and it will hide behind the header.
DEMO
Note: Just seen there's more then 1 section. You could just wrap them all in a div and give that the margin. Or use :first-of-type.

Issue with divs stacking beside each other

I'm having some issues with aligning 3 divs beside each other.
http://jsfiddle.net/Lpprn/
I have a strong feeling it's in the syntax, but I can't for the life of me figure it out.
#story-container {
width: 700px;
padding: 0px;
margin: 0 auto;
}
#story-left {
width: 300px;
padding: 10px;
padding-right: 0px;
float: left;
text-align: right;
margin: 0;
background-color: #000000;
}
#story-center {
width: 100px;
float: left;
margin: 0;
background-color: #ffffff;
}
#story-right {
width: 300px;
padding: 10px;
padding-left: 0px;
float: left;
text-align: left;
margin: 0;
background-color: #808080;
}
Thanks for your help!
The containing elements don't add up to the width of the parent, 700px.
This is because padding is added to the width of the children elements.
Therefore, 300px + 10px + 100px + 10px + 300px != 700px
You would either have to subtract the padding values from the widths, or use something like box-sizing, which changes the box model of an element, thereby causing its padding/border properties to be calculated into its width/height.
The box-sizing CSS property is used to alter the default CSS box model used to calculate widths and heights of elements. It is possible to use this property to emulate the behavior of browsers that do not correctly support the CSS box model specification.
border-box: The width and height properties include the padding and border, but not the margin.
From MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing
I added the following to each element, though it actually wouldn't be needed on the middle element, #story-center, as it currently doesn't have any padding.
jsFiddle example - it works now - (red background added to display the parent container)
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;

How to add borders to div without messing up the layout?

I have the following elements:
<body>
<div id="container">
<div id="sidebar1"></div>
<div id="content">
<h3>Lorem ipsum</h3>
<p>Whatnot.</p>
</div>
<div id="sidebar2"></div>
</div>
</body>
Following this style:
/* ~~ this fixed width container surrounds all other divs~~ */
#container {
width: 960px;
background-color: #FFF;
margin: 0 auto;
overflow: hidden;
}
#sidebar1 {
float: left;
width: 180px;
/*border: 2px solid black;*/
background-color: #EADCAE;
padding: 0px 0px 100% 0px;
}
#content {
padding: 10px 0;
width: 600px;
float: left;
}
#sidebar2 {
float: left;
width: 180px;
/*border: 2px solid black;*/
background-color: #EADCAE;
padding: 0px 0px 100% 0px;
}
I am trying to achieve this layout: http://jsfiddle.net/QnRe4/
But as soon as I un-comment the borders it turns into this: http://jsfiddle.net/FZxPQ/
** Solved **
The border width was added to each element's total width making them too wide to fit in the container. Removing 2x the border width from each column's width solves the problem: http://jsfiddle.net/FZxPQ/4/
CSS box-sizing to the rescue! This property
alters the default CSS box model used to calculate widths and heights of elements
The border-box value means that
the width and height properties include the padding and border
/* support Firefox, WebKit, Opera and IE8+ */
#container, #sidebar1, #sidebar2 {
box-sizing: border-box;
-moz-box-sizing: border-box;
}
However, browser support is not 100% standardized.
As other answers have already mentioned the extra width which pushes the sidebars out of alignment is because the width calculation includes the border width. box-sizing simply tells the browser that an element with a given width/height should include any border and padding values into the final width/height calculations.
The problem is that when you add in the boarder, the size of the outer divs increased by 4, 2px on each size. So, your container needs to grow in size by 8px.
So change your container to:
#container {
width: 970px;
background-color: #FFF;
margin: 0 auto;
overflow: hidden;
}
See: http://jsfiddle.net/QnRe4/13/
When you apply the borders, that goes outer the divs, so the sidebars will have 184px width which doesn't fits to the container. try addig width: 176px
http://jsfiddle.net/QnRe4/12/
#sidebar1 {
float: left;
width: 176px;
border: 2px solid black;
background-color: #EADCAE;
padding: 0px 0px 100% 0px;
}
Like this? http://jsfiddle.net/QnRe4/3/
What's happening is that your elements are losing their block display properties when you remove the borders.
So, adding display: block to those elements resolves that.
I've also adjusted your element's widths by 4px in width to retain the layout, since removing those borders essentially reduces the space that those elements occupy on-page.

Page Shows Scroll bar no matter what

Here's my page: http://bad-sf.com/stemtest/about.html
Notice that scrolling is still an option even though the content is small enough to not require scrolling. Could it have to do with my css? (below):
body:before {
content:"";
height:100%;
float:left;
width:0;
margin-top:-32767px;
}
* {
margin:0;
padding:0;
outline: none;
}
html, body {
height:100%;
font-family: '_.regular';
font-size: 13px;
outline: none;
}
#wrap {
min-height:100%;
width:800px;
margin: 2% auto;
}
#main {
overflow:auto;
padding-bottom: 30px;
}
#smm {
width: 400px;
height: 200px;
float:left;
}
#footer {
position: relative;
margin-top: 0px;
height: 35px;
clear:both;
font-family: '_.regular';
}
THANKS! I'm still learning html and css so any input you have would be really appreciated - Danny
This is caused by #wrap, being 104% height. Note these rules:
body {
height: 100%;
}
#wrap {
min-height: 100%;
margin: 2% auto;
}
So your #wrap is actually 100% height plus 2% margin on top plus 2% margin on bottom.
There are several ways of countering this.
You can remove the height from body and optionally min-height from #wrap, as it's no use anymore in this case.
You can change margin on #wrap to margin: 0 auto; (this will inevitably raise the content though).
There are probably a few other possibilities, but seeing as the unsatisfactory answers are voted down, I don't really feel compelled towards thinking about more sublime solutions.
Take away height:100%; from html, body { }. Why did you need it there even anyway?
The reason why the scrollbar is always appearing is because the #wrap DIV is also set to 100% height and on top of that, a margin of 2%. This forces your body to be 2% more than 100%.
You can remove the 2% margin from #wrap but if you don't want do, removing the height: 100%; from html, body { } should do the trick.

Width: 100% Without Scrollbars

I'm trying to make a part of my webpage that fit the width of the browser, for this I'm using width: 100%, the problem is that it shows scrollbars and I can't use overflow-x: hidden; because it will make some of the content hidden, so how I can fix this?
#news {
list-style-type: none;
position: absolute;
bottom: 0;
width: 100%;
text-align: center;
margin-right: 10px;
margin-left: 10px;
padding: 0;
-webkit-user-select: text;
}
Because you're using position: absolute, instead of using:
width: 100%; margin-right: 10px; margin-left: 10px
you should use:
left: 10px; right: 10px
That will make your element take the full width available, with 10px space on the left and right.
You have to remove the margins on the #news item
#news {
list-style-type: none;
position: absolute;
bottom: 0;
width: 100%;
text-align: center;
margin-right: 10px; /*REMOVE THIS*/
margin-left: 10px; /*REMOVE THIS*/
padding: 0;
-webkit-user-select: text;
}
If this doesn't work, you might have margin and padding set on the element itself. Your div - if that is what you are using - might have styles applied to it, either in your stylesheet or base browser styles. To remove those, set the margins specifically to 0 and add !important as well.
#news {
list-style-type: none;
position: absolute;
bottom: 0;
width: 100%;
text-align: center;
margin: 0 !important;
padding: 0 !important;
-webkit-user-select: text;
}
It seems that you have set the width to 100%, but there are also margins that force the width to expand beyond that.
Try googling for "css flexible ( two/three-collumn) layouts".
Here's an example,
<div id="cont">
<div id="menu"></div>
<div id="main"></div>
<div class="clear"></div>
</div>
and the css
#menu{
float:left;
height:100%;
width:200px;
}
#main{
padding-left:200px;
}
.clear{clear:both;}
The #menu div, will be aligned to the left and have a static width of 200px.
The #main div, will "begin" below #main, but because of it's 200px padding (can also be margin) its content and child elements will start - where #menu ends.
We must not set #main to a percent width, (for example 100%) because the 200 pixels of left padding will be added to that, and break the layout by adding scrollbars to the X axis.
I had a similar issue with a absolute positioned element, and I wanted to use width 100%. This is the approach I used and it solved my problem:
box-sizing=border-box
Otherwise I always had a little content and padding pushing past the scroll bar.
The answer is that you have margins set that will make the div wider than the 100%; hence the scrollbars.
If you can rid yourself of margins do it! However, often you'll want the margins. In this case, wrap the whole thing in a container div and set margins to 0 with width at 100%.

Resources