CSS - hidden div at bottom of the page but visible header - css

So I have a div that i want to position at the bottom of my page, with the body of the div completely hidden below the user's viewport, except the entirety of a header which is fixed to the bottom of the page.
Contraints:
The height of the entire div is dynamic, as there can be any number of items in there
The header of the div is contained entirely within the div, syntax-wise
Ideally I'd like to do this completely with CSS, but if a little bit of JavaScript is needed, that's fine too.
Any ideas on how this can be done? position: fixed and bottom: (height of header - height of div) doesn't work because the height of the div changes when you resize the viewport.

Figured it out. Here's my solution. The key is to have a wrapper that is the same height as the header that you want sticking out, and apply position fixed and bottom 0 to that parent wrapper.
html
<html>
<body>
<div class="wrapper">
<div class="wrapper-top">
</div>
<div class="wrapper-bottom">
This can be variable height
<div>
</div>
</body>
</html>
css
.wrapper {
height: 50px;
position: fixed;
bottom: 0;
width: 100%;
}
.wrapper-top {
height: 50px;
width: 100%;
}

Related

CSS Keep Footer at Bottom with Expanding Content

I am building a page HERE and I'm having trouble with the footer. I've done a lot of research looking at sticky footers and wrapping everything in containers... and my head is spinning.
The goal of the site is to display the song lyrics on the right as the title is clicked on the left, and it works miraculously well. The problem is that the footer doesn't move with the lyrics...
Your help is much appreciated!
When you used position:absolute for any element then you must add to position:relative their parent element otherwise it not work.
body {
position: relative;
padding-bottom: 50px;
}
Or If you don't want add this in body then just wrap all the divs on one parent div like .wrapper and this css in that.
<div class="wrapper">
<div class="header"></div>
<div class="banner"></div>
<div class="container clearfix"></div>
<footer></footer>
<div>
Also add clearfix class in container div because its have float element
You can fix or make a sticky footer by using CSS or you can just put this CSS for you footer.
.footer-class{
position: fixed;
bottom: 0;
left: 0;
right: 0;
width: 100%;
z-index: 999;
}
Position is fixed for footer will never move in any page.
bottom 0 will fixed the footer at the bottom.
left and right 0 will placed the footer in the screen.
Width 100% will show the full width.
z-index will show at the front. Placed everything will behind the footer.

body background extends into margins or is cut-off when scrolling

I have a layout where I need to use height: 100% on html and body (and any wrapper divs I resort to using) to achieve an effect similar to pages, so that the content on my first "page" is centred, scrolling down the content on the second "page" is centred etc.
The html looks like this:
<section class="page" id="p01">
<div class="spacer">
</div>
<div class="outer">
<div class="inner">
Some content
</div>
<div class="inner">
Some content
</div>
</div>
</section>
<section class="page" id="p02">
<div class="spacer">
</div>
<div class="outer">
<div class="inner">
Some content
</div>
<div class="inner">
Some content
</div>
</div>
</section>
and the vertical centring etc. achieved with this styling:
body, .page {height: 100%; margin: 0 auto;}
.spacer {
float: left;
height: 50%;
margin-bottom: -150px;
}
.outer {
height: 300px;
width: 100%;
background-color: #fca;
clear: both;
position: relative;
display: block;
white-space: nowrap;
}
.inner {
width: 41%;
margin: 0 6%;
height: 300px;
background-color: green;
display: inline-block;
vertical-align: middle;
white-space: normal;
}
.inner:first-child {
margin-right: 0;
}
You can see it at work in this fiddle:
http://jsfiddle.net/terraling/3V5rV/
The problem is the body background (here I'm just using color, but on my site it will be an image) leaks out into the body margins, i.e. the body content has a max-width and should be centred with white margins.
I can fix that either by... setting html background-color to white, as per
http://jsfiddle.net/terraling/yM53t/
...but body background becomes cutoff when scrolling into the second page (that wasn't a problem in the first fiddle).
Alternatively I could set the background image on a wrapper div and not on the body. That solves the problem of it leaking into the body margins, but it still has the same problem that it is cut off on scrolling.
(see: http://jsfiddle.net/terraling/3V5rV/1/ )
Any solution that involves removing the height: 100% declaration from any of html, body or wrapper collapses the layout (including replacing with max-height: 100%).
There's a whole lot of problems with this construct and not all of them can be solved, unfortunately.
The background issue
As you have seen yourself the background of body extends to the viewport if html does not have a background. That's solvable.
The float issue
When an element floats it does not contribute to the height of its parent element. So they don't grow (e.g. body does not expand). That can be solved if you can use alternatives. For vertically centering an element you could use display: table-cell e.g., which allows you to vertically center the content.
The height issue
This is where all hope is gone. height: 100% refers to the height of the parent, of course. The parent of body is html which in turn is the child of the viewport. You gave html the size of 100% (= the size of the viewport) and body the size of 100% (= size of html = size of viewport).
So now body has a fixed height and it can't expand meaning the background doesn't expand as well. Now one might have the idea to give body no size so that it can expand. But .page has 100% too. If a parent (in this case body) has no fixed size 100% has no meaning and will be treated as auto, which means as big as the content. And the content has a height of 300px. So the .page elements wouild no longer have the height of the viewport but 300px.
As for the collapse of the CSS, you should either specify the height specifically height:200px; or add padding to the bottom/top of the page so that the content wraps. You can also use min-height:200px; then add the margin-bottom:20px; to separate the pages. I would approach this at a specific height with the wrapper having the specific background-image and bottom-margin.
In order to center your background-image to the <html> you can specify the position as 50%.
This can be done by doing background:url('yourimage.jpg') repeat 0 50%;This will ensure the background is centered.

Fixed sidebar anchored between header and footer

I have a sidebar that is position:fixed and height:100%
However, the effect is not what I desire. The height 100% is 100% of window where as I need to to be 100% of it's container, which is confined between the header and footer. The header is also fixed so the main content scrolls "underneath" it meanwhile the footer is anchored to the bottom of the main content which can be of variable height depending on how much content is loaded.
In this jsfiddle I have set the height of the ul#toolBarList to 200px to demonstrate. If you enlarge the window and scroll down, the sidebar should take the full white-space height. I do not mean to simple set a background behind the list, but rather that the bottom of the sidebar should anchor to the top of the footer.
To see why 100% doesn't work, set 200px to 100% and witness how the list overflows the footer.
Answer can be some concise javascript or perferably all CSS with compatibility with IE8+, latest chrome/firefox.
Much thanks
You need to set the height of all of its parent elements to 100% or just use this CSS instead of the height:
top: 0;
bottom: 0;
right: 0; //whichever side you want it on
100% height only works when the parent element has an explicit height set. You can achieve 100% height by setting 100% height on the html element, body element, and any other container elements in between. For example,
<html style="height: 100%;">
<body style="height: 100%;">
<div id="page" style="height: 100%;">
<div id="sidebar" style="height: 100%; float: left;">content</div>
<div id="main" style="height: 100%; float: left;">content</div>
</div>
</body>
</html>

CSS How to set div height to 100% minus some pixels

I'm trying to design a web page these days that is a bit hard.
I have three main divs. First one for header, another for footer, and third one for main content. Header and footer must be fixed in top and bottom of the page. Their place mustn't change with resizing of browser window. Third div must be in the blank space between those divs. It can resize to fit the page with window resize.
Height of main div must exactly change, because I want to place a Google Maps in that div, so the height of this div is important.
I tried so many things, but they were not successful. Setting height of the div to 100%(while height of body and html is 100%, too) was not the answer. Using a table (with three rows, two rows with fixed height, one row with variable height, with height="100%") had some problems, too(in IE8, when I declared a doctype, the div in second row (with height:100%) didn't fit the cell anymore!).
Now I have no idea to do this work. What can I do?
Note: I prefer not to use CSS3, because compatibility with old browsers is important for me.
You could try something like this.
HTML
<div id="header"></div>
<div id="body"></div>
<div id="footer"></div>
CSS
#header {
height:50px;
width: 100%;
background: black;
position: fixed;
z-index: 1;
top: 0;
}
#body {
height:100%;
width: 100%;
background: #CCC;
position: absolute;
z-index: 0;
}
#footer {
height: 50px;
width: 100%;
background: #0CF;
position: fixed;
bottom: 0;
}
Here is a fiddle - http://jsfiddle.net/6M59T/
Use a set height for your header, and use sticky footer to keep your footer a set height and aligned to the bottom as well. The space in between should then always be the right height.
You should try the well known Clearfix hack to handle height issues, because you need to "clear" parents elements to get that full 100% height you need.
This is one of the shortcomings of css. You cannot accomplish what you want using just those three divs. You need to use additional divs to offset the height of your header and footer. Here is how to solve this:
<body style="height:100%; margin:0; padding:0;">
<div id="header" style="height:50px; position: relative; z-index: inherit; background-color:lightblue;"></div>
<div id="content" style="height:100%; margin:-50px 0 -70px 0; background-color:wheat">
<div id="header-offset" style="height:50px;"></div>
<div id="footer-offset" style="height:70px;"></div>
</div>
<div id="footer" style="height:70px; background-color:lightblue;"></div>
</body>

How to position a div at the bottom of the page correctly?

I have divs like this:
<div class="container">
<div class="header"></div>
<div class="body"></div>
<div class="footer"></div>
</div>
now I use this style for my container and footer:
html, body {
height:100%;
}
div.container {
min-height: 100%;
position: relative;
}
div.footer {
width:100%;
height: 40px;
positioin: absolute;
bottom: 0px;
}
So, the footer stays at the bottom relative to the page, it is good, but I found out two problems:
if the body div's content is too long, it will overlap the footer!
I want the background color of the footer to span over the whole browser view port, but currently it is just as wide as its the container div.
Any idea of how to fix this?
My best tip is to use A CSS Sticky Footer, works like a charm.
place the footer div out of the container and give marin-top:-(height:px)px;.
<div class="footer"></div>
html, body {
height:100%;
}
div.container {
min-height: 100%;
}
div.footer {
width:100%;
height: 40px;
margin-top:-40px;
}
You might want to try bottom: -40px;.
The bottom property positions your element so that the bottom of your element is offset by the bottom of the containing element by this amount. So if you had bottom: 0; as in your example, the bottom of your element is aligned with the bottom of its containing element, hence it will overlap it.
I want the background color of the footer to span over the whole browser view port, but currently it is just as wide as its the container div.
This is because the width: 100%; is defined relative to the containing block of the element, which is the div.container (which is set to position: relative). You would have to take it out of this container, or not define the container as position: relative; to fix this.

Resources