A lot of times the HTML structure for a web page is this:
<div id="full">
<div id="header"></div>
<div id="body"></div>
<div id="footer"></div>
</div>
Mine's a little different:
<div id="full">
<div id="header"></div>
<div id="body"></div>
</div>
<footer id="footer"></footer><!-- I assume it doesn't matter whether it's a footer or a div -->
And the CSS:
html, body {
height: 100%;
}
body {
position: relative;
min-height: 100%;
direction: rtl;
}
#full {
position: relative;
min-height: 100%;
height: 100%;
padding-top: 2%;
display: flex;
flex-direction: column;
}
#header {
position: relative;
padding-bottom: 2%;
}
#body {
width: 100%;
flex-grow: 1;
background-color: #F6F6F6;
}
#footer {
position: absolute;
bottom: 0;
left: 0;
right: 0;
text-align: center;
font-size: 14px;
}
Aside from the structure itself, I think it's worth mentioning that:
The #header's content is pre-known and #body's isn't.
The #body is filling the remaining content after #header (I used the flex method).
I found a lot of examples but each of them had the #footer inside the main container.
My question is:
How do I fix the #footer so it'll stay at the bottom?
I don't know if there's a solution to what I wanted, but I solved it by changing the structure.
As I mentioned in my question, my #body is filling the remaining space after the #header, and everything has to be on top of it (visually, at least), including the footer.
I changed my structure so that the #footer is inside the #body, at the end of it.
That doesn't sound like the right solution but it works for me without causing any other problems.
Related
I want to make a fixed header so I follow the steps listed in W3School, but the content becomes shorter... I don't know how to say, please see the pictures.
This is the picture that I follow the code
.header {
overflow: hidden;
width: 100%;
height: 46px;
background-color: #2f4779;
position: fixed;
top: 0;
z-index: 100;
}
<div class="header">
<div class="logo">
123
</div>
<div class="nav">
<ul>
<li>Discover and Explore</li>
</ul>
</div>
</div>
And here is the picture that I didn't write the fixed header.
Without fixed header
My plan is to make everything inside the viewport. How can I solve the problem?
Generally what I do in this situation is to add padding to the top of the body so it works for every page, and also use a css variable to keep the sizing consistant.
:root {
--headerHeight: 46px;
}
header {
height: var(--headerHeight);
background-color: #2f4779;
position: fixed;
top: 0;
width: 100%;
z-index: 100;
}
body {
padding-top: var(--headerHeight);
}
section {
height: 200vh;
}
<header></header>
<section>
<h1>Test</h1>
</section>
Well, when you give an element a fixed (or absolute) position, it is removed from the normal flow, so you need to add a padding top of 46px (eaqual to header's height) to the section that follows the header.
You'd probably benefit from using CSS Grids or CSS Flexbox. grid-template-columns is a great property and it makes your life a lot easier.
#grid {
display: grid;
width: 100%;
grid-template-columns: 50px 1fr;
}
#areaA {
background-color: lime;
}
#areaB {
background-color: yellow;
}
<div id="grid">
<div id="areaA">A</div>
<div id="areaB">B</div>
</div>
Do give them a read :)
Hi,
Its My Fixed Header and Footer When i am scrolling Browser. Browser Header is Hidden that time my Footer is jumping How Can i Fix it.. I have attached jumping ScreenShot Below :
..
#header {
width: 100%;
background: #fff transition: top .5s ease;
display: flex;
position: fixed;
left: 0;
top: 0;
height: 50px z-index: 999;
}
#main {
padding-top: 50px;
transition: padding-top .2s ease;
}
.fullContainer {
min-height: calc(100vh - 50px);
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
#footer {
width: 100%;
height: 50px;
position: fixed;
bottom: 0;
left: 0;
right: 0;
background-color: #fff;
z-index: 999;
}
<div id="root">
<header id="header"></header>
<footer id="footer"></footer>
<main id="main">
<div class="fullContainer">
</div>
</main>
</div>
Thanks
Add this css for your header and footer.
.fixed-header{
position: fixed;
top: 0;
width: 100%;
}
.fixed-footer{
position: fixed;
bottom: 0;
width: 100%;
}
While scrolling the page, if your content overlaps on the header and footer means please add a z-index for both header and footer as required
This will work:
body {
margin: 0;
}
#root {
height: 100vh;
display: flex;
flex-direction: column;
}
#root > * {
width: 100%;
}
#header, #footer {
flex: 0 0 auto;
}
#main {
flex: 1 1 100%;
overflow-y: auto;
}
<div id="root">
<header id="header">fixed header</header>
<main id="main">
<div class="fullContainer">
your content here
...
<hr>
let's test with something tall
....
<div style="height: 400vh"></div>
</div>
</main>
<footer id="footer">fixed footer</footer>
</div>
It also works with short content:
body {
margin: 0;
}
#root {
height: 100vh;
display: flex;
flex-direction: column;
}
#header, #footer {
flex: 0 0 auto;
}
#main {
flex: 1 1 100%;
overflow-y: auto;
}
<div id="root">
<header id="header">fixed header</header>
<main id="main">
<div class="fullContainer">
your content here
...
<hr>
let's test with something small
....
</div>
</main>
<footer id="footer">fixed footer</footer>
</div>
Cross browser, cross device, no jumping guaranteed, as it doesn't rely on positioning.
The only problem it might have, on some designs, is that the #header, #main and #footer don't actually overlap and this can be a problem if you want the header (and/or footer) to be displayed over the background (or contents) of #main.
To tackle this rare edge case, you have at least a couple of solutions:
a) don't place the background on #main, place it on #root or even body. problem solved.
b) if, for some design reason (let's say header and footer are semi-transparent and they make stuff under them blurry - which is a cool effect) you actually want the content of #main to scroll under them, so it gets blurred as you scroll, you will have to use JavaScript.
You want to give #main a top negative margin (and a matching positive top padding) equal to #header's height and a bottom negative margin (matched with a positive bottom padding) equal to the #footer's height. And, obviously, give both #header and #footer a higher z-index value than #main's.
The JS would be used to update the margins/paddings when header/footer change height.
I started writing on how to tackle this (again there are a couple of options) but at some point I realized the answer is getting too long, all because of trying to solve an edge case. If you have this particular edge case, ask it as another question and I'll answer there.
I would like to put footer on the bottom of the page (or bottom of the screen, if page is shorter than a screen). I am using code:
<div id="wrapper">
<div id="header-wrapper">
...
</div> <!--header-wrapper-->
<div class="clear"></div>
<div id="body-wrapper">
<div class="row960">
<div class="menu">...</div>
<div class="content">...</div>
</div> <!--row960-->
</div> <!--body-wrapper-->
<div class="clear"></div>
<div id="footer-wrapper" class="gray">
</div> <!--footer-wrapper-->
</div> <!--wrapper-->
and css:
.clear{
clear:both;
display:block;
overflow:hidden;
visibility:hidden;
width:0;
height:24px;
margin:0px
}
html, body{
margin: 0;
padding: 0;
height: 100%;
width: 100%;
}
body{
background-color: #000000;
color: #FFFFFF;
font-size: 14px;
}
#wrapper{
min-height: 100%;
position: relative;
}
#header-wrapper{
height: 100px;
}
#body-wrapper{
padding-bottom: 50px;
}
#footer-wrapper{
position: absolute;
bottom: 0;
width: 100%;
height: 50px;
}
.row960{
width: 960px;
margin: auto;
overflow: auto;
}
#menu{
width: 200px;
float: left;
}
.content{
width: 740px;
margin-left: 20px;
float: right;
}
The problem is that footer is on the bottom of the screen even if the page is longer than a screen (it covers a text). I've checked it with Firebug and body-wrapper has right height, but row960 has height of screen instead of height of page. I can't figure out how to fix it. Does any one have idea what to do?
You can see my page on http://www.domenblenkus.com/fiap/notice.php
Thanks for your help!
EDIT: I don't know if I emphasized it enough, so I would like to point it out that the main problem is that height of row960 is not right.
Hmmm, I think I have a solution that fits the requirements you stated. There are certainly other ways to do this though, so you can keep looking around if you don't agree with this method. (Also, when I looked on your site it appeared that your #wrappper element was a sibling of #footer-wrapper, and not a parent.)
So, the HTML would look like (structure copied from your site):
<div id="wrappper">
<div id="header-wrapper" class="gray">
<div class="clear"></div>
<div id="body-wrapper"></div>
<div class="clear"></div>
<div class="spacer"></div>
</div>
<div id="footer-wrapper" class="gray"></div>
Note the addition of the .spacer element at the bottom of #wrappper, it's required for this approach of the "sticky footer".
Now, CSS you'll need to add (add to any current definitions if you already have them):
body, html{
height: 100%;
}
#wrappper{
min-height: 100%;
margin-bottom: -50px;
height: auto;
}
.spacer{
height: 50px;
}
If you're wondering why I chose 50px for the height, it's because that's the height of your footer element, #footer-wrapper.
Anyways, I only really tested this in the Firebug console, so I'm not sure how it will behave in a live environment, but I'm fairly certain this will give you what you want. If this isn't what you were looking for, let me know and I'll be happy to help further!
If you want it at the bottom, then you don't need the position:absolute or bottom:0, it will be at the bottom of your div anyway.
You can try doing it using margin. Here is a fiddle of what I'm taking about: http://jsfiddle.net/8WLyP/
Basically for your HTML, place all your content inside a "container" element and then your footer will be a sibling of that element.
Then in your CSS what you will need is to give them html and body elements a min-height: 100%
You "container" element will also have min-height: 100%
You will then need to give your footer a heightof X, in my example it's 50 pixels.
The "container" element will need to have margin-bottom: -50px or whatever value you give the height of the footer.
With all that done, make sure you don't give "container" and "footer" any other margins or paddings than the ones shown, if you need to give them, then you will need to give it to the child elements, in my example p element.
With this technique, as opposed to position: fixed the footer will stick to the bottom of the window if the content is too short, and it will move with the content when the content is bigger than the window/viewport.
HTML:
<div id="container">
<header>
<p>Header</p>
</header>
<section>
<p>Section</p>
</section>
</div>
<footer>
<p>Footer</p>
</footer>
CSS:
html, body, header, footer, section, p, div {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
p {
padding: 5px 10px;
}
header {
width: 100%;
background: #f00;
color: #fff;
}
section {
width: 100%;
height: 200px;
background :#0f0;
color: #fff;
}
#container {
width: 100%;
min-height: 100%;
margin-bottom: -50px;
}
footer {
width: 100%;
background :#00f;
color: #fff;
height: 50px;
}
You want to place the footer at the bottom of the content. BUT: You want to have it at the bottom of the viewport (window) if the content above it is shorter.
So, try this:
the CSS:
#footer-wrapper {
position: relative;
width: 100%;
height: 50px;
}
#body-wrapper {
position: relative;
height: 100%;
}
… and the JavaScript (jQuery):
var bodyWrap = $('#body-wrapper'),
footerWrap = $('#footer-wrapper'),
windowHeight = $(window).height();
var heightRemaining = parseInt(windowHeight - bodyWrap.outherHeight() - footerWrap.outerHeight());
if (heightRemaining > 0) bodyWrap.css('min-height', heightRemaining);
Didn't test it due to little time.
Give it a try.
I am trying to do the following in a CSS template:
Dock the footer to the bottom when there is not enough content to
fill the page
Stretch the header and footer background across the whole width
Position all the content in the middle of the page
This is the code I have, created with help on here:
http://tinkerbin.com/lCNs7Upq
My question is, I have seen a few ways to achieve this. Is this the best? It seems a shame to have to have the empty div as well, is this a bodge?
You can fix and element to the footer using CSS:
position: fixed;
bottom: 0;
However, I'm trying to figure out what exactly your trying to do.
You header and footer should automatically go 100% across the page if it's a div.
Your middle section can be set to height: auto; via css and will fill up the viewport pushing the footer all the way to the bottom, but to do this you also have to set the body to 100% in order to get it to work.
html, body, #content {
height: 100%;
min-height: 100%;
}
#header {
height: 100px;
width: 100%;
background: blue;
position: fixed;
top: 0;
}
#content {
height: auto;
margin: 100px auto;
background: green;
}
#footer {
position: fixed;
bottom: 0;
height: 100px;
width: 100%;
background: red;
}
Your HTML should look somewhat like this:
<div id="header"></div>
<div id="content"></div>
<div id="footer"></div>
Working Example: http://jsfiddle.net/s4rT3/1/
This is the best example I have seen:
http://css-tricks.com/snippets/css/sticky-footer/
* {
margin: 0;
}
html, body {
height: 100%;
}
.page-wrap {
min-height: 100%;
/* equal to footer height */
margin-bottom: -142px;
}
.page-wrap:after {
content: "";
display: block;
}
.site-footer, .page-wrap:after {
height: 142px;
}
.site-footer {
background: orange;
}
<div class="page-wrap">
Content!
</div>
<footer class="site-footer">
I'm the Sticky Footer.
</footer>
Update: In 2019 using flex is a better option.
Ok so I know this topic has many questions, but I still haven't been able to figure exactly how to make this work. This is close to the problem, but its not working for me.
I want my page to have 100% height. Inside this page is a static header of height 40px, and then content that takes the remaining height (100% - 40px).
HTML:
<body>
<div id="page">
<div id="header">
header
</div>
<div id="content">
content
</div>
</div>
</body>
CSS:
html, body
{
height: 100%;
margin: 0px;
}
#page
{
min-height: 100%;
}
#header
{
height: 40px;
}
#content
{
height: 100%;
position: absolute;
top: 0;
padding-top: 40px;
}
This is an explanation of the code:
I added position: absolute to content because otherwise it would not take up 100% of its container #page for some reason
Then the problem was that it exceeds the boundaries of the page, which is why I added top: 0.
Then the contents of #content overlaps with the header so I added padding-top: 40px
Now the #content exceeds the boundaries of the page again
Any suggestions? Thanks.
This should work:
http://jsfiddle.net/94JNZ/1/
#content
{
height: auto;
width: 100%;
position: absolute;
top: 40px;
bottom: 0;
}
You can use box-sizing property for this
Check this:
http://jsfiddle.net/Gn8zN/1/
Another simple & best solution
Check this:
http://jsfiddle.net/B8J2H/
Here is an article about this problem. CSS 100% height problem
You can see the example page has a perfect 100% layout what header and footer.
It uses relative position and not absolute.
Use flex:1;
html, body
{
height: 100%;
margin: 0px;
}
#page
{
min-height: 100%;
display: flex;
flex-direction: column;
}
#header
{
display: flex;
height: 40px;
background-color:red;
}
#content
{
display: flex;
flex: 1;
background-color:blue;
}
<body>
<div id="page">
<div id="header">
header
</div>
<div id="content">
content
</div>
</div>
</body>
Just script it:
<script type="text/javascript">
function contentSize()
{
document.getElementById('content').style.height=(window.availHeight-40)+"px";
}
onload=contentSize;
onresize=contentSize;
<script>