Bootstrap sticky footer code clash between height and padding - css

I'm attempting to add the sticky-footer.html example into my bootstrap project.
however there is a clash of code in the body class that is screwing with my layout.
In my page code with bootstrap responsive fixed navbar uses:
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
In the sticky footer example it uses:
html,
body {
height: 100%;
/* The html and body elements cannot have any padding or margin. */
}
At the moment I have both body tags included in my css. Everything is working except on the desktop page widths versions, with the current css below I get a sticky footer with extra depth, and the page has a vertical scroll bar even though it does not need one? And it's all to do with this body tag clash and the extra 60px.
/* Sticky footer styles
-------------------------------------------------- */
html,
body {
height: 100%;
/* The html and body elements cannot have any padding or margin. */
}
/* Wrapper for page content to push down footer */
#wrap {
min-height: 100%;
height: auto !important;
height: 100%;
/* Negative indent footer by it's height */
margin: 0 auto -190px;
}
/* Set the fixed height of the footer here */
#push,
#footer {
margin-top:20px;
height: 190px;
}
#footer {
background-image:url(assets/img/herringboneLight.jpg);
}
/* Lastly, apply responsive CSS fixes as necessary */
#media (max-width: 767px) {
#footer {
}
}
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
Cheers
Alex

I don't know which browsers you are trying to support, but a CSS3 solution (actually in IE8+) I believe would be to change the box-sizing on body:
body {
padding-top: 60px;
-moz-box-sizing: padding-box; /* or border-box */
-webkit-box-sizing: padding-box; /* or border-box */
box-sizing: padding-box; /* or border-box */
}
This should cause the height: 100% to include the 60px in the overall amount. It is working in Firefox so, this fiddle has scroll bars, this one does not.

Related

sticky footer bottom margin with conditional fixed position bottom nav bar

I have a sticky footer that uses the flexbox technique which works perfectly fine and expands when the content fills the page.
I'm now trying to add a conditional, based on a particular user, fixed position bottom navbar. i've applied margin-bottom to the footer which is fine when the content fits within the page however when the content grows i cannot scroll the page to the bottom and view the full footer. It seems the bottom-margin is not applied to the footer when the content grows to fill the page. Any help would be appreciated, example code and Codepen below.
<html>
<head>
<style>
html, body {
/* IE 10-11 didn't like using min-height */
height: 100%;
}
body {
display: flex;
flex-direction: column;
}
.content {
flex: 1 0 auto; /* Prevent Chrome, Opera, and Safari from letting these items shrink to smaller than their content's default minimum size. */
padding: 20px;
}
.footer {
flex-shrink: 0; /* Prevent Chrome, Opera, and Safari from letting these items shrink to smaller than their content's default minimum size. */
padding: 20px;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
font: 16px Sans-Serif;
}
h1 {
margin: 0 0 20px 0;
}
p {
margin: 0 0 20px 0;
}
footer {
background: #42A5F5;
color: white;
margin-bottom: 25px;
height: 50px;
}
.conditionalNav {
position: fixed;
bottom:0;
left:0;
width:100%;
background:green;
color: white;
padding:5px;
hight:25px;
}
</style>
<meta charset="UTF-8">
<title>Sticky Footer with Flexbox</title>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
</head>
<body>
<div class="content">
<h1>Sticky Footer with Flexbox</h1>
<p><button id="add">Add Content</button></p>
<p>some content</p></div>
<footer class="footer">
Footer
</footer>
<div class="conditionalNav">
Conditional bottom navbar
</div>
</body>
</html>
https://codepen.io/pinman/pen/eYBLYOR
Setting height: 100% on html and body (as opposed to min-height) prevents the document height from exceeding the viewport height, so your additional content is overflowing scrollable area.
You could remove body from the 100%, leaving it on html, or add overflow: auto to the html/body rule so that the body element can scroll (as opposed to scrolling the window).
Edit: removing 100% height from body allows the footer to move off the bottom of the window. Updated accordingly.
html, body {
/* IE 10-11 didn't like using min-height */
height: 100%;
overflow: auto;
}
You also have a typo in your .conditionalNav rule:
hight:25px;

Why does margin-top on a div element inside body, show scroll bars on the page? [duplicate]

Out of curiosity, considering the example below, why does having the margin on the #container div cause a vertical scrollbar to appear in the browser? The container is much smaller in height than the body height which is set to 100%.
I have set the padding and margins to 0 for all elements except the #container. Note that I have deliberately omitted absolute positioning on the #container div. In this case how is the browser calculating the height of the body and how is the margin affecting it?
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
* { padding:0; margin:0;}
html, body { height:100%; }
#container
{
padding:10px;
margin:50px;
border:1px solid black;
width: 200px;
height: 100px;
}
</style>
</head>
<body>
<div id='container'>
</div>
</body>
</html>
Example also on JSFiddle
If you paint the backgrounds of html and body (giving each its own color), you'll quickly notice that body is being shifted down along with #container, and #container itself isn't offset from the top of body at all. This is a side effect of margin collapse, which I cover in detail here (although that answer describes a slightly different setup).
It's this behavior that's causing the scrollbar to appear, since you've declared body to have 100% the height of html. Note that the actual height of body is unaffected, as margins are never included in height calculations.
Based upon #BoltClock♦'s answer, I fixed it by zeroing the margin...
so
html,body, #st-full-pg {
height: 100%;
margin: 0;
}
works where id "st-full-pg" is assigned to a panel div (which further contained panel-heading and panel-body)
A bit late, but maybe it helps someone.
Adding float: left; to #container removes the scrollbar, as W3C says:
•Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).
html,body {
height: 100%;
margin: 0;
overflow: hidden;
}
This worked for me
adding float:left; is nice, but will interfere with central horizontal positioning using margin:auto;
if you know how big your margin is, you can account for that in your height percentage using calc:
height: calc(100% - 50px);
browser support is good, but only IE11+
https://caniuse.com/#feat=calc
/*removes default margin & padding*/
html, body{
padding: 0px !important;
margin: 0px !important;
}
/*sets body height to max; and allows scrollbar as page content grows*/
body{
min-height: 100vh;
}
I have found a solution: add padding: 1px 0; to body prevents vertical scrollbars to appear
For those who are coming here for an easier to understand answer that even includes code samples, this answer (copied from here) is for you.
No JavaScript or definite pixel values (such as 100px) are required, just, pure CSS and percentages.
If your div is just sitting there on its own, height: 50% will mean 50% the height of the body. Normally, the height of the body is zero without any visible content, so 50% of that is just, well, zero.
This is the solution (based on this) (uncomment the background lines to get a visualisation of the padding):
/* Makes <html> take up the full page without requiring content to stretch it to that height. */
html
{
height: 100%;
/* background: green; */
}
body
{
/*
100% the height of <html> minus 1 multiple of the total extra height from the padding of <html>.
This prevents an unnecessary vertical scrollbar from appearing.
*/
height: calc(100% - 1em);
/* background: blue; */
}
/* In most cases it's better to use stylesheets instead of inline-CSS. */
div
{
width: 50%;
height: 50%;
background: red;
}
<div></div>
The above was written so that there would still be the usual padding. You could set the dimensions of the red div to 100% and still see padding on each side/end. If you don't want this padding, use this (although it doesn't look nice, I recommend you stick with the first example):
/* Makes <html> take up the full page without requiring content to stretch it to that height. */
html, body
{
height: 100%;
}
/* You can uncomment it but you wouldn't be able to see it anyway. */
/*
html
{
background: green;
}
*/
body
{
margin: 0;
/* background: blue; */
}
/* In most cases it's better to use stylesheets instead of inline-CSS */
div
{
width: 50%;
height: 50%;
background: red;
}
<div></div>
I saw this problem fixed before where you put all the contents of body in a div called wrap. Wrap's style should be set to position: relative; min-height: 100%;. To position #container div 50px from the top and left put a div inside wrap with a padding set to 50px. Margins will not work with wrap and the div we just made, but they will work in #container and everything inside it.
here's my fix on jsfiddle.
you can add non-breaking space into the body tag.
<body> <othertags>...</body>
html, body {
height: 100%;
overflow: hidden;
}
If you want to remove the body scrolling add the following style:
body {
height: 100%;
overflow: hidden;
}
Inspired by #BoltClock, I tried this and it worked, even when zoom out and in.
Browser: Chrome 51
html{
height: 100%;
}
body{
height: 100%;
margin: 0px;
position: relative;
top: -20px;
}
I guess body was shifted down 20px.
It works for me:
html,
body {
height: 100%;
height: -webkit-fill-available; // Chrome
}
// Firefox
#-moz-document url-prefix() {
body {
box-sizing: border-box;
margin: 0;
padding: 1px;
}
}
Add overflow: hidden; to html and body.
html, body {
height: 100%;
overflow: hidden;
}
I found a quick solution: try set height to 99.99% instead of 100%

IE8 layout with min-height and border-box

First let me say that it seems Internet Explorer 8 completely ignores box-sizing: border-box; declaration on a box, when there is a min-height set on it as well (this post confirms that: https://stackoverflow.com/a/11714178/3355252).
Now let me describe what I need to accomplish. Here's my site (much simplified): http://jsfiddle.net/ttKP3/. Doctype is HTML 4.01 Strict.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head></head>
<body>
<div id="header">header</div>
<div id="main">main</div>
<div id="footer">footer</div>
</body>
html,
body {
height: 100%;
margin: 0;
}
#header,
#footer {
height: 100px;
}
#main {
box-sizing: border-box;
min-height: 100%;
width: 300px;
margin: -100px auto;
padding: 100px 0;
}
What I need is footer at the very bottom of the screen and the main box filling the whole screen from the top to the bottom. Also, when the content gets bigger (click Add content in JSFiddle few times) I need the main box to enlarge and footer to move accordingly.
It renders just fine in Chrome and Firefox. You probably won't be able to run it in IE8 (it doesn't render JSFiddle), but border-box property on main is completely ignored and so that main box is 200px higher than it should be - the footer is below the visible area of screen.
As it looks like it couldn't be handled by using border-box and min-height, I'm looking for any other solution for IE8. The only one that came to my mind was using calc, that is height: calc(100% - 200px); but IE8 doesn't support that as well. By the way I have a conditional adding ie8 class on html element when ran in IE8, so I don't need a cross browser solution - just CSS to get the desired layout in that particular browser.
After reading once more this sticky footer post on CSS-tricks and tinkering a bit, I found a clean solution that doesn't require any markup changes (you can do with only 3 containers: header, main and a footer) nor using box-sizing at all. It works in IE8 just as well.
html,
body {
height: 100%;
margin: 0;
}
#header,
#footer {
height: 100px;
}
#header {
margin-bottom: -100px;
}
#footer {
margin-top: -100px;
}
#main {
min-height: 100%;
width: 300px;
margin: 0 auto;
}
/* Space for header and footer. */
#main:before,
#main:after {
display: block;
content: "";
height: 100px;
}
You may like to apply the margins to main instead of header and footer.
Here's my fiddle with this solution applied: http://jsfiddle.net/ttKP3/1/

.container not covering full height

Best to explain with the screenshots, this is my application layout (rails):
http://i.stack.imgur.com/gzPgy.png
And here is the issue:
http://i.stack.imgur.com/LtgNG.jpg
The colored bg just below the header is the .main class, which I want to be covering the entirety of the site from the header down. I'm not really sure why it gets pushed to a fixed height there. CSS:
/* Global Styles */
html,
body {
height: 100%;
}
/* Structure styles (footer css) */
.container, .main {
height: 100%;
}
.main {
background-color: #f4f4ec;
padding-top: 20px;
}
Thanks for any help

Fix iframe height to be exactly same as containing div

I have a simple 4-panel webpage set up mostly using divs and css. The panel divs are header, footer, menu, and content. The menu and content are supposed to be parallel columns with the same height.
Everything works fine until I put an iframe inside the content div. The content div then becomes taller than the menu div, even when I set them to the same height. I know that the iframe is the reason because this doesn't happen when I take out the iframe, but it's the content div - not the iframe - that actually is too tall.
How can I fix this? Some similar questions have been asked, but the proposed solutions didn't work for me, unless I was doing something wrong. Here's my complete code:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<style>
body {
margin: 0;
padding: 0;
}
#header {
background-color: #7D110C;
height:100px;
}
#menu {
float:left;
width:300px;
background-color: lightGrey;
min-height:500px; /* for modern browsers */
height:auto !important; /* for modern browsers */
height:500px; /* for IE5.x and IE6 */
}
#content {
margin-left:300px;
background-color: white;
min-height:500px; /* for modern browsers */
height:auto !important; /* for modern browsers */
height:500px; /* for IE5.x and IE6 */
}
#content iframe{
width:100%;
border:none;
margin: 0;
padding: 0;
background-color: pink;
min-height:500px; /* for modern browsers */
height:auto !important; /* for modern browsers */
height:500px; /* for IE5.x and IE6 */
}
#footer {
clear:both;
background-color: #7D110C;
height:100px;
}
</style>
</head>
<body>
<div id="header"></div>
<div id="menu"></div>
<div id="content"><iframe id="content_iframe" name="content_iframe"></iframe></div>
<div id="footer"></div>
</body>
<script type="text/javascript">
console.log( $('#content').css('height') );
console.log( $('#content_iframe').css('height') );
</script>
</html>
height:auto !important; overrides height:500px; in #content and in #content iframe. If you get rid of the height:auto !important; in both CSS classes, it works fine. jsFiddle
Ok here's the real fix, just leave everything as is and add display: block to #content iframe. That fixes it. An iframe is an inline frame, hence the extra white space. Updated jsFiddle
For modern browsers you can try this:
add position:relative to #content
remove width, height, min-heigth from #content iframe and add this instead:
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
No idea what to do for IE 5 and 6, though.
If you set a fixed height:500px; and the iframe is taller than this, you will get a scrollbar on the side.
If you want a fixed height at all times, remove both height: auto !important and min-height: 500px and leave only height:500px.
height-auto: The browser calculates the height. This is default.
min-height: Defines the minimum height
The following will make menu and content have the same height at all times.
HTML
<div id="wrapper">
<div id="menu"></div>
<div id="content"><iframe id="content_iframe" name="content_iframe"></iframe></div>
</div>
CSS (Just add this to the already existent)
#wrapper { display: table; }
#menu { display: table-cell; } /* Remove the float */
#content { display: table-cell; } /* Remove the float */
Note, this won't work on IE7 and below though. Either you'll have to use fixed height for both menu and content or javascript.

Resources