I am using display: table property to achieve sticky footer with the following features:
Header and footer heights are not fixed
Content is always 100% of the remaining height
Here is a fiddle of what I currently have: JSFiddle
Everything works perfectly, but what I am trying to achieve is a footer that is not visible on the screen, unless the user scrolls. The footer should be right below the window if there is not enough content, otherwise the content should push it. Here is a picture that explains what I actually mean. In the left is what I currently have, in the right what I am trying to achieve.
You could use flexboxes, along with view height for that !
.container {
display: flex;
flex-direction: column;
justify-content: start;
align-items: stretch;
height: 100vh;
}
.header {
flex: 0 0 50px;
background: lightblue;
}
.content {
flex: 1 1 auto;
background: lightgrey;
}
.footer {
height: 100px;
background: darkcyan;
}
<div class="container">
<div class="header"></div>
<div class="content"></div>
</div>
<div class="footer"></div>
#trichetriche's answer is better than mine (I really need to get to grips with flexbox!), but I updated your fiddle with a little bit of jQuery. This could be distilled down more but you can see what's going on as it is.
All it does is get the header height and content height, and if they're smaller than the window height then it sets the content height accordingly.
Fiddle
$(document).ready(function() {
var h = $('#header').height();
var c = $('#content').height();
var w = $(window).height();
if((h + c) < w) {
$('#content').height(w - h);
}
});
I ended up using just the same code, but instead of having all elements in the container I left the footer outside. Seems to work just right. Here is an updated fiddle: JSFiddle
* {
margin: 0;
padding: 0;
}
body, html{
width: 100%;
height: 100%;
}
.container{
width: inherit;
height: inherit;
display: table;
background: red;
}
.container div {
width: inherit;
}
.header {
background: darkgray;
}
.content {
height: 100%;
background: green;
}
.footer {
background: yellow;
}
<div class="container">
<div class="header">
<h1>HEADER</h1>
</div>
<div class="content">
<h1>CONTENT</h1><h1>CONTENT</h1><h1>CONTENT</h1>
</div>
</div>
<div class="footer">
<h1>FOOTER</h1>
</div>
Related
I want to center .donut-graphs inside .dashboard horizontally, so the space between the right edge of the sidebar and the left edge of .donut-graphs is the same as the space from the right edge of .donut-graphs and the right edge of the screen. I have managed to do so, but I had to remove position: fixed from .navbar. The problem is, I can't do that because my sidebar has to stay on top of the screen when you scroll up/down, and with position: fixed on .navbar, the graphs aren't centered properly.
HTML:
<div class="container">
<div class="navbar">
</div>
<div class="content">
<div class="dashboard">
<div class="donut-graphs">
<div class="dashboard-income">
Div 1
</div>
<div class="dashboard-overall">
Div 2
</div>
<div class="dashboard-spent">
Div 3
</div>
</div>
</div>
</div>
</div>
CSS:
body {
margin: 0;
}
.container {
display: flex;
align-items: stretch;
max-width: 100%;
min-height: 100vh;
}
.navbar {
background-color: #ddd;
flex: 0 0 230px;
position: fixed;
height: 100vh;
width: 230px;
}
.content {
flex: 1;
overflow-x: auto;
text-align: center;
}
.donut-graphs {
display: inline-flex;
border: 1px solid;
margin: 50px auto 0;
position: relative;
text-align: left;
}
.dashboard-income,
.dashboard-overall,
.dashboard-spent {
height: 256px;
width: 357px;
display: inline-block;
}
.dashboard-income {
background-color: green;
}
.dashboard-overall {
background-color: blue;
}
.dashboard-spent {
background-color: red;
}
How can I overcome the issue?
Demo
position: fixed puts element above everything. That element won't attach to any element in body because it is the way that works. It only becomes dependent of viewport
What you want to achive could be done with position: absolute but parent (whose child you want to center) has to be position: relative for this to work.
Read more about positioning elements in css here
.content { padding-left:230px; }
Should do the trick.
Assigning your navbar a fixed position takes it out of the document flow, so when centering your donut graphs the browser doesn't take the navbar into account.
Giving the .content element a padding equivalent to the width of the navbar makes up for this.
The only problem with this approach is that if .navbar changes dimensions, you'll need to change the padding on .content to match.
as I stated in the title I'm trying to make a website were you can not scroll down or something, it should cover the whole webpage.
Here is an example,
I tried doing this but whenever I set the height and width to 100% it doesn't seem to work, it always make content inside the wrapper that exceeds 100% overflow.
Edit: also making everything equal to 100% height/width doesn't work since I'm using borders and px.
use
body{
width:100vw;
height:100vh;
overflow:hidden;
}
(don't forget to use a reset sheet first)
If you use px(usage that I don't recommend), you may use the CSS calc() function for sizing things just like this (for instance) width: calc((100vw - 900px) / 2)
EDIT
For everything else :
CSS
div#header/*or simply the HTML header*/{
width:100vw;
height:/*something here that I'll call Hh for calculuses (less than 100vh)*/;
float:left;
}
div.sidebar{
width:/*something i'll call SBw for calculuses (less than 50vw)*/;
height:calc(100vh - Hh - Fh);
}
div#main{
width:calc(100vw - SBw - SBw);
height:calc(100vh - Hh - Fh);
}
div#footer/*or simply the HTML footer*/{
width:100vw;
height:/*something I'll call Fh*/;
}
and HTML
<body>
<div id="header"></div>
<div class="sidebar" id="Sidebar1"></div>
<div id="main"></div>
<div class="sidebar" id="Sidebar2"></div>
<div id="footer"></div>
</body>
I would use the flexbox sticky footer technique. The 100vh makes it always the height of the screen and then flexbox magic takes care of the rest making it always fit no matter what screensize. Check it out:
HTML
<body>
<header></header>
<main class="content">
<section class="left-side"></section>
<section class="right-side"></section>
</main>
<footer></footer>
</body>
CSS
body {
display: flex;
min-height: 100vh;
padding: 0;
margin: 0;
flex-direction: column;
background-color: red;
}
header {
height: 50px;
background-color: green;
}
main.content {
display: flex;
justify-content: space-between;
flex: 1 0 auto;
background-color: yellow;
}
.left-side, .right-side {
display: block;
height: auto;
width: 20%;
background-color: orange;
}
footer {
height: 50px;
background-color: blue;
}
Full codepen example: http://codepen.io/StefanBobrowski/pen/zZXXWy
As an example, I've made a fiddle:
https://jsfiddle.net/L7mwpzux/3/
How do I make the div .container minimally fill the screen?
So when there is almost no content, it still fills the screen.
It's for a page that is shown when the checkout cart is empty. The content is too thin, so the screen is not fully filled with content.
P.s. I am not looking for an answer that assumes that the header or footer has a static height. I want to be able to use it also in situations where the height of the header or footer is variable.
Also, I would love a CSS solution, so no JavaScript or jQuery
You can use calc() and set 100vh - height of header, also add box-sizing: border-box to keep padding inside.
* {
box-sizing: border-box;
}
body,
html {
margin: 0;
padding: 0;
}
header {
height: 200px;
background-color: blue;
width: 100%;
}
.container {
padding: 50px;
min-height: calc(100vh - 200px);
}
footer {
width: 100%;
height: 200px;
background-color: #333;
}
<header>
</header>
<div class="container">
small text
</div>
<footer>
</footer>
Other approach is to use Flexbox and set display: flex on body which is parent element in this case with min-height: 100vh and then just set flex: 1 on .container so it takes rest of free height.
* {
box-sizing: border-box;
}
body,
html {
margin: 0;
padding: 0;
}
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
header {
height: 100px;
background-color: blue;
}
.container {
padding: 50px;
flex: 1;
}
footer {
height: 100px;
background-color: #333;
}
<header>
</header>
<div class="container">
small text
</div>
<footer>
</footer>
try this
min-height: calc(100vh - 400px);
here is the fiddle https://jsfiddle.net/L7mwpzux/1/
Is there a way for the footer to be positioned at the bottom no matter how much content is on the page and the content does not overlap with the footer?
Thank You for all your help
Flexbox might be a solution which is pretty flexible. The footer will always be at the bottom of the page unless there is too much content, then it will just be at the end of the page (thus no "overlapping").
document.getElementById("expand").addEventListener("click", function() { document.getElementById("long").style.display = "block"; });
* {
box-sizing: border-box;
}
#container {
display: flex;
flex-direction: column;
align-content: stretch;
height: 100vh;
}
header, footer {
flex-basis: auto;
flex-grow: 0;
padding: 20px;
background: #ccc;
}
div#content {
flex-grow: 1;
padding: 20px;
}
div#long {
display: none;
}
<div id="container">
<header>This is the header with auto-height.</header>
<div id="content">Short Content. <span id="expand"><strong><u>Click here more content!</u></strong></span>
<div id="long"><img src="http://placehold.it/350x1000"></div></div>
<footer>This is the footer, and always at the bottom of the window unless there's too much content.</footer>
</div>
A better idea can be utilizing a css framework like twitter-bootstrap. but if you want to achieve that with css only, something like this in your css file will give you what you need:
footer {
width: 100%;
bottom: 0;
position: fixed;
}
In order to make sure, any content and footer will not overlap you can either set a padding-bottom in your body:
body {
padding-bottom: 60px;
}
or have a content div which you set its 'margin-bottom':
.content {
margin-bottom: 60px;
}
jsfiddle
I found several questions about but none of their solutions was working for me so here we go again.
Let's say I have this template of HTML
<html>
<div id="header">...</div>
<div id="contentA">...</div>
<div id="contentB">...</div>
<div id="footer">...</div>
</html>
The footer div should be at least 80px height, but if those 80px plus the height of all other 3 divs is not enough to fullfill the screen I want the footer to increase as much as the screen is filled with it below header, contentA and contentB.
BG-Color Solution
If you just want to let the remaining space have the same background-color as the footer (but not the body), you could add the footer bg-color to the html-tag:
html {
background-color: #footer_color;
}
body {
background-color: #body_color;
}
#footer {
min-height: 80px;
}
.
JS-Solution
If you have something more complex within your footer, you could use javascript/jquery to calculate the remaining space and set the footer to that height.
There is a similar question with a code example here: https://stackoverflow.com/a/14329340/3589841
.
Flexbox-Solution
If you only care about the latest browsers you can use the flexbox-box-model:
HTML:
<html>
<body>
<div id="flex_container">
<div id="header">...</div>
<div id="contentA">...</div>
<div id="contentB">...</div>
<div id="footer">...</div>
</div>
</body>
</html>
CSS:
html, body {
min-height: 100%;
}
#flex_container {
display: flex;
flex-direction: column;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: flex-start;
}
#header {
flex: 0 1 auto;
}
#contentA {
flex: 0 1 auto;
}
#contentB {
flex: 0 1 auto;
}
#footer {
flex: 0 1 100%;
min-height: 80px;
}
I believe you're going for something like this, have a look http://jsfiddle.net/dusUK/
Using CSS, we create a class, which in this case is fullheight, and we apply the following:
.fullheight {
display: block;
position: relative;
background: red;
height: 100%;
}
We also then apply the following to html, body
html, body {
height: 100%;
min-height: 100%;
}