Place div in golden ratio: Alternative to absolute position? - html

I have build a page like in this sample below. You can also play around with it on codepen.
The idea was, that the dark grey hero box is placed on the bottom of the viewport. So far so easy.
BUT if the screen is too small in height (e.g. for a small laptop), so that the text would fill most (or even all) of the viewport, the background image could hardly be realized. In this case the hero box should be placed in the golden ratio instead of the bottom of the viewport.
I have already build that solution in the sample as well. Just reduce the height of your browser window.
I just have the feeling that I am doing soemthing wrong in general. At the moment I am placing hero, content and footer in absolute positioned divs. It's working ok as long as there is only static content inside the #content div. But if there is for example an accordion or some other collapsing/dynamic stuff, the position of the footer has to be re-calculated.
I can easily do this in re-calling the ratio function. But meanwhile the whole solution does look wrong or maybe too complex to me.
Isn't there another and more easy solution to place the content div with a RELATIVE position after the hero container?
// call function after everything has been loaded
$(window).load(function() {
checkRatio();
});
window.dispatchEvent(new Event('resize'));
$(window).resize(function() {
checkRatio();
});
function checkRatio() {
var ratio = 0;
var contentTopPos = 0;
var footerTopPos = 0;
var windowHeight = $(window).height();
var headerHeight = $("#hero").height();
var contentHeight = $("#content").height();
ratio = (windowHeight / headerHeight);
contentTopPos = (windowHeight / 1.618);
if(ratio < 1.618) {
// Window too small or Content too long
$("#hero").css('bottom', 'initial');
$("#hero").css('top', contentTopPos);
} else {
// Default Position: bottom 0
$("#hero").css('top', 'initial');
$("#hero").css('bottom', 0);
}
var sumHeight = $("#hero").offset().top + $("#hero").height();
$("#content").css('top', sumHeight + "px");
footerTopPos = (sumHeight + contentHeight);
$("#footer").css('top', footerTopPos + "px");
}
body {
padding: 0;
margin: 0;
font-family:Verdana, Geneva, Tahoma, sans-serif;
}
#header_img {
position: fixed;
top: 0;
height: 100vh;
width: 100%;
background-size: cover;
z-index: 1;
}
#hero_container {
position: relative;
height: 100vh;
width: 100%;
}
#hero {
position: absolute;
margin: 0 5%;
bottom: 0;
width: 90%;
height: auto;
background-color: #333;
box-shadow: 0 -2px 6px rgba(0, 0, 0, 0.46);
z-index: 2;
}
#hero_text {
padding: 20px;
color: #fff;
}
#content {
position: absolute;
width: 100%;
background-color: transparent;
z-index: 3;
}
section {
background-color: #dedede;
padding: 20px;
border-bottom: solid 1px #333;
}
#footer {
position: absolute;
width: 100%;
z-index: 3;
background-color: #999;
color: #555;
}
#footer_text {
padding: 10px 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div id="header_img" style="background: #ffffff url('https://cdn4.vectorstock.com/i/1000x1000/80/28/free-sample-rubber-stamp-vector-13448028.jpg') no-repeat center top; background-size: cover;"></div>
<div id="hero_container">
<div id="hero">
<div id="hero_text">
<h1>Lorem ipsum dolor sit</h1>
<h4>Cras felis leo, pellentesque non dui ut, molestie mollis lorem.</h4>
<p>Fusce venenatis metus id est venenatis ornare. Aliquam id ante et nulla rutrum malesuada vel sit amet felis. Maecenas maximus, quam vitae cursus ultrices, est augue consequat magna, vitae bibendum ex urna eget lacus. Donec quis sagittis diam.</p>
</div>
</div>
</div>
<div id="content">
<section>
<h2>Vivamus molestie</h2>
<p>Sem sit amet posuere elementum, ligula dolor laoreet eros, sed ultrices mi lacus quis est. Sed ut venenatis metus, id consectetur mauris.</p>
</section>
<section>
<h2>Mauris sollicitudin ante est</h2>
<p>Ut ultrices neque volutpat quis. Aenean bibendum dui sed pulvinar vestibulum. Vestibulum finibus ornare dui at lobortis.</p>
</section>
<section>
<h2>Sed consequat euismod sem</h2>
<p>In venenatis lacus luctus in. Nam elementum dolor a magna eleifend consectetur. Curabitur efficitur magna erat, et eleifend enim placerat sit amet. Fusce cursus, sem vel porta tempor, diam dolor auctor lorem, eu venenatis velit lorem ac lectus.</p>
</section>
</div>
<div id="footer">
<div id="footer_text">© 2020 SchweizerSchoggi</div>
</div>

.hero-container {
height: 100vh;
display: flex;
}
.hero-content {
width: 100%;
background: grey;
align-self: flex-end;
}
#media only screen and (max-height: 300px) {
.hero-container{
padding-top: 25vh;
}
.hero-content{
align-self: flex-start;
}
}
<div class="hero-container">
<div class="hero-content">
<h1>Lorem ipsum dolor sit</h1>
<h4>Cras felis leo, pellentesque non dui ut, molestie mollis lorem.</h4>
<p>Fusce venenatis metus id est venenatis ornare. Aliquam id ante et nulla rutrum malesuada vel sit amet felis. Maecenas maximus, quam vitae cursus ultrices, est augue consequat magna, vitae bibendum ex urna eget lacus. Donec quis sagittis diam.</p>
</div>
</div>
<div id="content">
<section>
<h2>Vivamus molestie</h2>
<p>Sem sit amet posuere elementum, ligula dolor laoreet eros, sed ultrices mi lacus quis est. Sed ut venenatis metus, id consectetur mauris.</p>
</section>
<section>
<h2>Mauris sollicitudin ante est</h2>
<p>Ut ultrices neque volutpat quis. Aenean bibendum dui sed pulvinar vestibulum. Vestibulum finibus ornare dui at lobortis.</p>
</section>
<section>
<h2>Sed consequat euismod sem</h2>
<p>In venenatis lacus luctus in. Nam elementum dolor a magna eleifend consectetur. Curabitur efficitur magna erat, et eleifend enim placerat sit amet. Fusce cursus, sem vel porta tempor, diam dolor auctor lorem, eu venenatis velit lorem ac lectus.</p>
</section>
</div>
<div id="footer">
<div id="footer_text">© 2020 SchweizerSchoggi</div>
</div>

Related

Don't know how to position elements that have different order in mobile and desktop version

I'm trying to build this a certain design. This is the design on the desktop and mobile:
.section {
width: 100%;
position: relative;
}
.section__inner {
width: 100%;
display: flex;
flex-direction: column;
}
.title {
color: white;
}
.img {
width: 100%;
height: auto;
}
.text-container {
display: flex;
flex-direction: column;
background-color: black;
color: white;
}
#media only screen and (min-width: 768px) {
.section__inner {
width: 175px;
margin-left: auto;
}
.img {
position: absolute;
z-index: -1;
width: 60%;
}
}
<div class="section">
<div class="section__inner">
<h1 class="title">Title</h1>
<img class="img" src="https://source.unsplash.com/random/" alt="random" />
<div class="text-container">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur cursus ornare risus. Ut sed gravida magna. Mauris in elit imperdiet, porta turpis a, mollis lorem. Nulla consectetur gravida urna, at condimentum dolor.</p>
<p>Suspendisse potenti. Cras malesuada lacus sed malesuada efficitur. Maecenas eros leo, sollicitudin convallis nunc nec, maximus blandit nisi. Cras eleifend nisi id risus vestibulum aliquet. Donec maximus justo at nulla blandit, vel dictum nisi volutpat.
Morbi placerat augue vel libero feugiat, eu venenatis libero aliquet.
</p>
</div>
<button>Go to the link</button>
</div>
</div>
Here's my codepen with what I've developed so far. I don't understand how I could position these elements and what approach to take.
First, you need to set a correct max width and width for your container element and allow it to center on the screen (according to your desktop design)
Second, position your .text-container div to the left so it's over the image, and since it's already in the correct hierarchy, no need to set the z-index and it's going to stack on top of the image
And finally, move the button into the .text-container so it can be positioned together with the text
Here is the updated part of the CSS:
#media only screen and (min-width: 768px) {
.section {
/* Ensure that enough space is available before 1200px, you can tweak this according to your design */
width: 90%;
/* So the 90% would only apply below this threshold, you can adjust the value as well if needed */
max-width: 1200px;
/* Center this element */
margin: auto;
}
.text-container {
position: absolute;
top: 20%;
left: 50%;
}
.img {
width: 60%;
}
}
<div class="text-container">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur
cursus ornare risus. Ut sed gravida magna. Mauris in elit imperdiet,
porta turpis a, mollis lorem. Nulla consectetur gravida urna, at
condimentum dolor.
</p>
<p>
Suspendisse potenti. Cras malesuada lacus sed malesuada efficitur.
Maecenas eros leo, sollicitudin convallis nunc nec, maximus blandit
nisi. Cras eleifend nisi id risus vestibulum aliquet. Donec maximus
justo at nulla blandit, vel dictum nisi volutpat. Morbi placerat
augue vel libero feugiat, eu venenatis libero aliquet.
</p>
<button>Go to the link</button>
</div>
Please note that when resizing the image, it should maintain the same aspect ratio as the reference design.

How to ignore a middle <div>?

I want to create a sticky background which starts to stick after the header is scrolled out of frame
so far this is my progress
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.header
{
width:100%;
background-color:black;
height:100px;
}
div.sticky {
position: -webkit-sticky;
position:sticky;
width:100%;
top: 0;
z-index: -1;
}
#contain
{
width: 50%;
background-color: yellow;
margin: auto;
padding-left: 100px;
padding-right: 100px;
height: 1000px;
clear:both;
}
</style>
</head>
<body>
<div class="header"></div>
<div class="sticky"><img src="https://images.all-free-download.com/images/graphicthumb/small_mouse_macro_515329.jpg" style="background-size: cover; flex-shrink: 0;min-width: 100%;min-height: 100%; "></div>
<div id="contain">
<h2>Scroll </h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut
</p>
</div>
</body>
</html>
but the yellow div is below the image, whereas I need it to start below the black header
so is there anyway to "ignore" the middle image wrapper <div>?
(I'm a beginner so please provide the simplest possible solution)
Make its height 0:
.header {
background-color: black;
height: 100px;
}
div.sticky {
position: sticky;
top: 0;
z-index: -1;
height:0;
}
div.sticky img {
width: 100%;
}
#contain {
width: 50%;
background-color: yellow;
margin: auto;
padding:10px 100px;
height: 1000px;
}
<div class="header"></div>
<div class="sticky"><img src="https://images.all-free-download.com/images/graphicthumb/small_mouse_macro_515329.jpg"></div>
<div id="contain">
<h2>Scroll </h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus
vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut
</p>
</div>
Or consider a hack using float and shape-outside:
.header {
background-color: black;
height: 100px;
}
div.sticky {
position: sticky;
top: 0;
z-index: -1;
float:left;
width: 100%;
shape-outside:inset(50%);
}
div.sticky img {
width: 100%;
}
#contain {
background-color: yellow;
margin:0 20%;
padding:10px 100px;
box-sizing:border-box;
height: 1000px;
display:inline-block;
}
<div class="header"></div>
<div class="sticky"><img src="https://images.all-free-download.com/images/graphicthumb/small_mouse_macro_515329.jpg"></div>
<div id="contain">
<h2>Scroll </h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus
vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut
</p>
</div>

Show header below fixed nav when clicking hash tag link

I have a fixed navigation bar. Its menu items link to a bookmark with a hash tag: example.html#bookmark
When the hash tag link is clicked, the <h1 id="bookmark">Bookmark</h1> should show, just below the nav, like this:
But currently, the heading element is being overlapped by the fixed header navigation bar:
How can this be prevented?
The HTML and CSS:
* {
margin: 0;
padding: 0;
}
.header {
height: 80px;
background: #EEE;
position: fixed;
top: 0;
left: 0;
width: 100%;
line-height: 80px;
text-align: center;
}
.header a {
color: blue;
text-decoration: none;
}
/*Ignore below this. This just creates padding for the example to scroll*/
body:before,
body:after {
content: '';
display: block;
height: 200vh;
}
<div class="header">
Click this link to take you to the anchor
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sit amet auctor tellus. Integer imperdiet urna vulputate pellentesque consectetur. Donec bibendum mi ac augue maximus, a porttitor risus faucibus. Aenean dui nisi, ornare et auctor vel, condimentum
vel lorem. Aliquam et mollis nisi, nec auctor diam. Ut sollicitudin vel nisl vel condimentum. Quisque ut nisl lobortis, blandit ante vitae, pellentesque lectus.</p>
<h1 id="bookmark">Bookmark</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sit amet auctor tellus. Integer imperdiet urna vulputate pellentesque consectetur. Donec bibendum mi ac augue maximus, a porttitor risus faucibus. Aenean dui nisi, ornare et auctor vel, condimentum
vel lorem. Aliquam et mollis nisi, nec auctor diam. Ut sollicitudin vel nisl vel condimentum. Quisque ut nisl lobortis, blandit ante vitae, pellentesque lectus.</p>
Here is a simple workaround. Give the linked element a suitable large top padding, and cancel it out with an equal negative margin:
h1 {
margin-top: -80px;
padding-top: 80px;
}
The linked element moves the viewport to the top of the elements padding and the negative margin removes the extra whitespace.
Example
* {
margin: 0;
padding: 0;
}
.header {
height: 80px;
background: #EEE;
position: fixed;
top: 0;
left: 0;
width: 100%;
line-height: 80px;
text-align: center;
}
.header a {
color: blue;
text-decoration: none;
}
h1 {
margin-top: -80px;
padding-top: 80px;
}
/*Ignore below this. This just creates padding for the example to scroll*/
body:before,
body:after {
content: '';
display: block;
height: 100vh;
}
<div class="header">
Click this link to take you to the anchor
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sit amet auctor tellus. Integer imperdiet urna vulputate pellentesque consectetur. Donec bibendum mi ac augue maximus, a porttitor risus faucibus. Aenean dui nisi, ornare et auctor vel, condimentum</p>
<h1 id="bookmark">Bookmark</h1>
<p>vel lorem. Aliquam et mollis nisi, nec auctor diam. Ut sollicitudin vel nisl vel condimentum. Quisque ut nisl lobortis, blandit ante vitae, pellentesque lectus.</p>
Limitation
A limitation of this workaround is the padding that is now underneath the text above the linked element. Any background colour of the linked element would show underneath and this would require its own workaround :)

I want only content being scrollable

This code works in Chrome as I want but not in IE or Firefox. To be more clear and satisfy SO constrains about the comment-code ratio in a post I'd like that only the content area being scrollable when the viewport goes bellow 300px or the content simply does not fit. Actually IE is my only concern. How could I achieve the same behavior under IE >= v10.
* {
font-family: Helvetica, Sans;
border: 0px;
margin: 0px;
padding: 0px;
}
html,
body {
height: 100%;
overflow: hidden;
}
#table {
display: table;
height: 100%;
width: 100%;
}
.navBar {
width: auto;
height: 72px;
overflow: auto;
border-bottom: 1px solid #bbb;
display: table-row;
}
.results {
background: gray;
width: 100%;
height: 100%;
overflow: auto;
display: table-row;
}
.results > div {
height: 100%;
overflow: auto;
}
#media screen and (max-height: 300px) {
footer {
display: none;
}
}
<body>
<div id="table">
<div class='navBar'>header</div>
<div class='results'>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi faucibus sem quam, quis finibus leo pretium sit amet. Sed imperdiet venenatis enim at sagittis. Praesent porta purus nec aliquet pellentesque. Nunc bibendum urna non risus lacinia, at
venenatis nisl interdum. Duis porta tristique augue vel dictum. Curabitur feugiat tincidunt risus eget semper. Aliquam quis cursus nibh, feugiat commodo arcu. Aliquam non dolor vel ex dapibus interdum vitae nec lorem. Phasellus fermentum neque
ut nibh hendrerit tempus. Pellentesque sit amet ligula dui. Donec laoreet est erat. Etiam aliquet sem sit amet quam tempus aliquam. Vivamus eleifend nunc ipsum, a viverra neque efficitur at. Duis mi nisl, accumsan quis ex et, aliquam lobortis
lectus. Vestibulum luctus diam eu mattis gravida. Quisque nisi felis, posuere vitae purus sit amet, pellentesque fermentum enim. Proin eu dui ex. Nunc nec erat sed augue rhoncus gravida. Suspendisse potenti. Pellentesque mattis lorem felis, a
venenatis odio gravida eget. Nam dictum dui efficitur pellentesque feugiat. Aliquam quis velit sit amet nibh rhoncus lacinia. Ut sed aliquet odio. Phasellus ut eros a nulla viverra convallis aliquet vel risus. Integer eu tellus congue, sodales
leo et, placerat nisi. Quisque semper bibendum tortor. Maecenas sed est sit amet neque convallis lacinia. Praesent vitae dapibus nibh, accumsan lobortis velit. Mauris sed imperdiet lectus. Nunc est turpis, lobortis sit amet hendrerit eu, eleifend
sed dui. Vivamus vulputate semper elit, vitae finibus metus mollis sed.</div>
</div>
<footer>footer</footer>
</div>
</body>
What do you think?
Is it a solution? I got the idea from here
<div class="table">
<!-- Header -->
<div class="row header">Header</div>
<div class="row content">
<!-- Use inner div's with position relative and absolute, to fix cell height, making it overflow correctly. -->
<div class="wrapper">
<div class="inner-content">
<input type="text" />
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi faucibus sem quam, quis finibus leo pretium sit amet. Sed imperdiet venenatis enim at sagittis. Praesent porta purus nec aliquet pellentesque. Nunc bibendum urna non risus lacinia, at venenatis nisl interdum. Duis porta tristique augue vel dictum. Curabitur feugiat tincidunt risus eget semper. Aliquam quis cursus nibh, feugiat commodo arcu. Aliquam non dolor vel ex dapibus interdum vitae nec lorem. Phasellus fermentum neque ut nibh hendrerit tempus. Pellentesque sit amet ligula dui. Donec laoreet est erat. Etiam aliquet sem sit amet quam tempus aliquam. Vivamus eleifend nunc ipsum, a viverra neque efficitur at. Duis mi nisl, accumsan quis ex et, aliquam lobortis lectus. Vestibulum luctus diam eu mattis gravida. Quisque nisi felis, posuere vitae purus sit amet, pellentesque fermentum enim. Proin eu dui ex. Nunc nec erat sed augue rhoncus gravida. Suspendisse potenti. Pellentesque mattis lorem felis, a venenatis odio gravida eget. Nam dictum dui efficitur pellentesque feugiat. Aliquam quis velit sit amet nibh rhoncus lacinia. Ut sed aliquet odio. Phasellus ut eros a nulla viverra convallis aliquet vel risus. Integer eu tellus congue, sodales leo et, placerat nisi. Quisque semper bibendum tortor. Maecenas sed est sit amet neque convallis lacinia. Praesent vitae dapibus nibh, accumsan lobortis velit. Mauris sed imperdiet lectus. Nunc est turpis, lobortis sit amet hendrerit eu, eleifend sed dui. Vivamus vulputate semper elit, vitae finibus metus mollis sed.</div>
<div>Some text.</div>
</div>
</div>
</div>
<!-- footer -->
<div class="row footer">Footer</div>
html, body {
height: 100%;
max-height: 100%;
padding:0px;
margin:0px;
}
.table, .row {
outline: none;
border: none;
outline-style: none;
vertical-align: top;
text-align: left;
}
.table {
border-collapse: collapse;
display: table;
table-layout: fixed;
/* This will ensure the cells within the table will keep there width. */
width: 100%;
height: 100%;
}
.row {
display: table-row;
width: 100%;
}
.header {
background-color: red;
}
.content {
height: 100%;
}
.footer {
background-color: green;
}
.wrapper {
position:relative;
height: 100%
}
.inner-content {
overflow: auto;
position: absolute;
top: 0;
right:0;
bottom: 0;
left: 0;
}
#media screen and (max-height: 300px) {
.footer {
display: none !important;
}
}
I'm not entirely sure what you mean by "only content area being scrollable". This was my interpretation of it:
http://jsfiddle.net/5q1Lgsy6/11/
By using a position: fixed width:100% top bar you can make it so that only the content below it will be scrollable.
I ditched all display: table tags, you don't really need them to organize your content unless that content is supposed to be displayed on an actual table.
Here's the CSS:
* {
font-family: Helvetica, Sans;
border: 0px;
margin: 0px;
padding: 0px;
}
html, body {
height: 100%;
}
#table {
height: 100%;
width: 100%;
}
.navBar {
background-color: white;
top: 0;
width: 100%;
height: 72px;
border-bottom: 1px solid #bbb;
position: fixed;
}
.results {
margin-top: 72px;
background: gray;
width: 100%;
height: 100%;
overflow: auto;
}
.results > div {
height: 100%;
overflow: auto;
}
#media screen and (max-height: 300px) {
footer {
display: none;
}
}
EDIT: if you also want the footer to be permanently fixed add this to your CSS:
footer {
position: fixed;
bottom: 0;
width: 100%;
height: 20px;
background-color: white;
}
there is a way to write css for IE
IE-6 ONLY
* html #div {
height: 300px;
}
IE-7 ONLY
*+html #div {
height: 300px;
}
IE-8 ONLY
#div {
height: 300px\0/;
}
IE-7 & IE-8
#div {
height: 300px\9;
}
NON IE-7 ONLY:
#div {
_height: 300px;
}
Hide from IE 6 and LOWER:
#div {
height/**/: 300px;
}
html > body #div {
height: 300px;
}

Make footer stick to bottom of page correctly [duplicate]

This question already has answers here:
How do you get the footer to stay at the bottom of a Web page?
(32 answers)
Closed 8 years ago.
I am trying to have my footer (just a div with a line of text in it) be at the bottom of the screen if the content doesn't go all the way to the bottom, or be at the bottom of the content if the content requires scroll bars. If the content doesn't require scroll bars, it works perfectly, but when the content is too long, the footer is still in the same spot, sitting right on top of the content.
My basic div structure is:
<div id="container">
<div id="body"></div>
<div id="footer"></div>
</div>
My corresponding CSS (stripped down somewhat):
body {
margin: 0;
padding: 0;
height: 100%;
}
html {
margin: 0;
padding: 0;
height: 100%;
}
#container {
width: 674px;
min-height: 100%;
height: 100%;
position: relative;
margin: 0 auto;
}
#body {
width: 616px;
padding: 5px 14px 5px 14px;
margin: 0 auto;
padding-bottom: 20px;
}
#footer {
position: absolute;
bottom: 0;
width: 644px;
height: 20px;
margin: 0 auto;
}
The simplest solution is to use min-height on the <html> tag and position the <footer> with position:absolute;
Demo: jsfiddle and SO snippet:
html {
position: relative;
min-height: 100%;
}
body {
margin: 0 0 100px;
/* bottom = footer height */
padding: 25px;
}
footer {
background-color: orange;
position: absolute;
left: 0;
bottom: 0;
height: 100px;
width: 100%;
overflow: hidden;
}
<article>
<!-- or <div class="container">, etc. -->
<h1>James Dean CSS Sticky Footer</h1>
<p>Blah blah blah blah</p>
<p>More blah blah blah</p>
</article>
<footer>
<h1>Footer Content</h1>
</footer>
Why not using: { position: fixed; bottom: 0 } ?
A simple solution that i use, works from IE8+
Give min-height:100% on html so that if content is less then still page takes full view-port height and footer sticks at bottom of page. When content increases the footer shifts down with content and keep sticking to bottom.
JS fiddle working Demo: http://jsfiddle.net/3L3h64qo/2/
Css
html{
position:relative;
min-height: 100%;
}
/*Normalize html and body elements,this style is just good to have*/
html,body{
margin:0;
padding:0;
}
.pageContentWrapper{
margin-bottom:100px;/* Height of footer*/
}
.footer{
position: absolute;
bottom: 0;
left: 0;
right: 0;
height:100px;
background:#ccc;
}
Html
<html>
<body>
<div class="pageContentWrapper">
<!-- All the page content goes here-->
</div>
<div class="footer">
</div>
</body>
</html>
Use this one. It will fix it.
#ibox_footer {
padding-top: 3px;
position: absolute;
height: 20px;
margin-bottom: 0;
bottom: 0;
width: 100%;
}
Use min-height as some pixel value, instead of %.
Like:
min-height:620px;
height:auto;
and footer as:
.footer {
height:70px;
clear:both;
position:relative;
bottom:0;
width: 100%;
}
do it using jQuery put inside code on the <head></head> tag
<script type="text/javascript">
$(document).ready(function() {
var docHeight = $(window).height();
var footerHeight = $('#footer').height();
var footerTop = $('#footer').position().top + footerHeight;
if (footerTop < docHeight) {
$('#footer').css('margin-top', 10 + (docHeight - footerTop) + 'px');
}
});
</script>
This should help you.
* {
margin: 0;
}
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -155px; /* the bottom margin is the negative value of the footer's height */
}
.footer {
height: 155px;
}
the easiest hack is to set a min-height to your page container at 400px assuming your footer come at the end. you dont even have to put css for the footer or just a width:100% assuming your footer is direct child of your <body>
The model being shared here is very similar to Ryan Fait's StickyFooter
http://ryanfait.com/sticky-footer
Just one div is missing so far in this discussion (the model proposed here by Kenneth Palanganas worked fine for local Win81 design for about 48 hours and then in ie/chrome collapsed for unknown reason). Ryan's "push" div will satisfy some reluctant browsers. Note that px is usual, however, for liquid layout consistency, em may be preferred.
* { border: 0; margin: 0; padding: 0; }
html, body { height: 100%; }
.wrapper { height: auto !important; height: 100%; margin: 0 auto -1em; min-height: 100%; }
.footer, .push { height: 1em; }
<div class="wrapper"><p>Your website content here.</p>
<div class="push"></div>
</div>
<div class="footer"><p>This is a footer</p>
</div>
I would like to share how I solved mine using Javascript function that is called on page load. This solution positions the footer at the bottom of the screen when the height of the page content is less than the height of the screen.
function fix_layout(){
//increase content div length by uncommenting below line
//expandContent();
var wraph = document.getElementById('wrapper').offsetHeight;
if(wraph<window.innerHeight){ //if content is less than screenheight
var headh = document.getElementById('header').offsetHeight;
var conth = document.getElementById('content').offsetHeight;
var footh = document.getElementById('footer').offsetHeight;
//var foottop = window.innerHeight - (headh + conth + footh);
var foottop = window.innerHeight - (footh);
$("#footer").css({top:foottop+'px'});
}
}
function expandContent(){
$('#content').append('<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at ante. Mauris eleifend, quam a vulputate dictum, massa quam dapibus leo, eget vulputate orci purus ut lorem. In fringilla mi in ligula. Pellentesque aliquam quam vel dolor. Nunc adipiscing. Sed quam odio, tempus ac, aliquam molestie, varius ac, tellus. Vestibulum ut nulla aliquam risus rutrum interdum. Pellentesque lorem. Curabitur sit amet erat quis risus feugiat viverra. Pellentesque augue justo, sagittis et, lacinia at, venenatis non, arcu. Nunc nec libero. In cursus dictum risus. Etiam tristique nisl a nulla. Ut a orci. Curabitur dolor nunc, egestas at, accumsan at, malesuada nec, magna.</p>'+
'<p>Nulla facilisi. Nunc volutpat. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Ut sit amet orci vel mauris blandit vehicula. Nullam quis enim. Integer dignissim viverra velit. Curabitur in odio. In hac habitasse platea dictumst. Ut consequat, tellus eu volutpat varius, justo orci elementum dolor, sed imperdiet nulla tellus ut diam. Vestibulum ipsum ante, malesuada quis, tempus ac, placerat sit amet, elit.</p>'+
'<p>Sed eget turpis a pede tempor malesuada. Vivamus quis mi at leo pulvinar hendrerit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Pellentesque aliquet lacus vitae pede. Nullam mollis dolor ac nisi. Phasellus sit amet urna. Praesent pellentesque sapien sed lacus. Donec lacinia odio in odio. In sit amet elit. Maecenas gravida interdum urna. Integer pretium, arcu vitae imperdiet facilisis, elit tellus tempor nisi, vel feugiat ante velit sit amet mauris. Vivamus arcu. Integer pharetra magna ac lacus. Aliquam vitae sapien in nibh vehicula auctor. Suspendisse leo mauris, pulvinar sed, tempor et, consequat ac, lacus. Proin velit. Nulla semper lobortis mauris. Duis urna erat, ornare et, imperdiet eu, suscipit sit amet, massa. Nulla nulla nisi, pellentesque at, egestas quis, fringilla eu, diam.</p>'+
'<p>Donec semper, sem nec tristique tempus, justo neque commodo nisl, ut gravida sem tellus suscipit nunc. Aliquam erat volutpat. Ut tincidunt pretium elit. Aliquam pulvinar. Nulla cursus. Suspendisse potenti. Etiam condimentum hendrerit felis. Duis iaculis aliquam enim. Donec dignissim augue vitae orci. Curabitur luctus felis a metus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In varius neque at enim. Suspendisse massa nulla, viverra in, bibendum vitae, tempor quis, lorem.</p>'+
'<p>Donec dapibus orci sit amet elit. Maecenas rutrum ultrices lectus. Aliquam suscipit, lacus a iaculis adipiscing, eros orci pellentesque nisl, non pharetra dolor urna nec dolor. Integer cursus dolor vel magna. Integer ultrices feugiat sem. Proin nec nibh. Duis eu dui quis nunc sagittis lobortis. Fusce pharetra, enim ut sodales luctus, lectus arcu rhoncus purus, in fringilla augue elit vel lacus. In hac habitasse platea dictumst. Aliquam erat volutpat. Fusce iaculis elit id tellus. Ut accumsan malesuada turpis. Suspendisse potenti. Vestibulum lacus augue, lobortis mattis, laoreet in, varius at, nisi. Nunc gravida. Phasellus faucibus. In hac habitasse platea dictumst. Integer tempor lacus eget lectus. Praesent fringilla augue fringilla dui.</p>');
}
/*sample CSS*/
body{ background: black; margin: 0; }
#header{ background: grey; }
#content{background: yellow; }
#footer{ background: red; position: absolute; }
#header, #content, #footer{ display: inline-block; width: 100vw; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body onload="fix_layout()">
<div id="wrapper">
<div id="header" class="navbar navbar-inverse navbar-fixed-top" role="navigation">
[some header elements here]
</div>
<div id="content" class="container">
[some content elements here]
</div>
<div id="footer" class="footer">
[some footer elements here]
</div>
</div>
</body>
Hope that helps.