Although a lot of questions have been posted I did not find the answer, so hopefully someone can help.
I have a website which consists of a header and an iframe below it. The iframe's height changes from page to page, sometimes very long, sometimes very small. The iframe must fit the whole width.
I want the height of the iframe to fit the rest of the page (means: the whole page minus the height of the header). I don't care if the iframe is a bit smaller, I am searching for a pixel-exact solution.
Here is a code-example I have generated so far:
<!doctype html>
<html lang="de">
<head>
<style>
header { border: 2px solid blue; }
.iframewrapper iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: auto;
border: 2px solid green;
}
.iframewrapper {
padding-top: 40%;
position: relative;
height: 100%;
width: 100%;
border: 2px solid red;
}
</style>
</head>
<body>
<header>
<h1>This is the header.</h1>
<p>Some text.</p>
</header>
<div class="iframewrapper">
<iframe src="iframetest-big.html" frameborder="0" scrolling="yes" />
</div>
</body>
</html>
You can look at it here (or in this css-fiddle, but this is not very figurative because I am not able to embed an iframe-file from an external url).
Now with this code the iframe is either too small (if I have a big window or a small padding-top in the wrapper-class) or to big, causing the appearance of a second scrollbar (if I have a small window or a big padding-top in the wrapper-class). I try to illustrate the situation in this image:
What is the right way to accomplish this?
Flexbox is perfect for that. Just set body to 100vh, set direction to column (so one element under another) and let iframe fill entire flexbox except header space.
body {
display: flex;
flex-direction: column;
height: 100vh;
margin: 0;
}
iframe {
width: 100%;
flex: 1;
border: 0;
min-height: 0; /* just due to very small preview stackoverflow fiddle */
}
<!doctype html>
<html lang="de">
<head>
</head>
<body>
<header>
<h1>This is the header.</h1>
<p>Some text.</p>
</header>
<iframe src="https://en.wikipedia.org/wiki/Stack_Overflow" />
</body>
</html>
So I'm found an article on responsive design (here) and I tried to make something like what it had on part of the tutorial. The site said to divid the size of the element by the size of the container that the element(s) are in. (the reason I divided by 1000 and not 1050 is because the margins on the div#main make it 1000px even though the header is 1050px) If that doesn't make sense than the link can explain it. It looks fine with my browser at full size, but if I shrink the window to much then it doesn't resize the way it should. I'm not exactly sure what part of my code is wrong but if someone could help me that would be great! Here's a link to the page I made. And here is my source code:
<!DOCTYPE html>
<html>
<head>
<style>
body, html {
margin: 0;
height: 100%;
width: 100%;
}
header {
height: 100px;
max-width: 1050px;
margin: 0 auto;
}
#main {
border-radius: 25px;
background-color: #aaa;
height: inherit;
max-width: inherit;
margin: 25px;
}
.box {
width: 47.5%;
height: 75%;
margin: 1.25%;
background-color: #444;
border-radius: 15px;
float: left;
}
</style>
</head>
<body>
<header>
<div id="main">
<span class="box">
</span>
<span class="box">
</span>
</div>
</header>
</body>
</html>
Maybe if I explain what's happening you'll see that there actually is no problem.
The inner boxes have a fixed height based on 75% of the parent container's height. Therefore, the heights of all elements stay the same. However, your margins are fractions of the parent element's width, therefore they change with the page width. As the page gets smaller, the margin gets smaller. Since a div naturally lies as high on the page as it can, it moves toward the border of its parent.
All this is expected with your design. To fix it, set fixed top and bottom margins:
.box {margin: 12px 1.25%;}
I've been in the industry for many years, however for the last 10 years I haven't had to do much of the HTML myself. I've recently become the only developer at work and as such I have to do all of the HTML myself as well.
Normally this wouldn't be an issue, however I'm trying to stick with the same quality standards that I have for my PHP / MySQL / JavaScript / jQuery work that I do. So tables are definitely out of the question (the last time I had to write HTML/CSS myself was when nested tables was acceptable).
I've been toying around with HTML divs and CSS and I'm having some pretty major issues with it, and not finding much of anything online other than the crap posted at W3Schools doesn't help either.
Let's first take a look at some code I'm working on, here's the HTML:
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
body {
background: gray;
}
#page_wrapper {
background: yellow;
height: 100%;
width: 100%;
}
#content {
width: 980px;
background: white;
height: 100%;
margin: 0 auto;
}
header {
height: 160px;
width: 980px;
background: blue;
}
#content-wrapper {
background: green;
width: 100%;
}
footer {
height: 120px;
margin-top: -120px;
width: 980px;
background: orange;
}
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Page Title</title>
<link href="inc/css/style.css" rel="stylesheet">
</head>
<body>
<div id="page_wrapper">
<div id="content">
<header>this is the header...</header>
<div id="content-wrapper">sdasd</div>
<footer>
this is the footer
</footer>
</div>
</div>
</body>
</html>
I've attempted several variations of this, and I haven't been able to get it to look the way that it should. Note that I'm using background colors specifically to tell the positioning of everything because this is more of a learning exercise than a real-world example.
Many of the pages that I will have to create will have a background image in the body just like many websites these days. Then the content will be 980 pixels wide. My big problem with the code above, is that the content-wrapper div, needs to be 100% of the available space if the content isn't long enough to push it down.
When I add height: 100% to that declaration in the CSS it seems to render it just fine however it puts it to 100% of the window which makes it overlap the page_wrapper div that contains it.
I'd like to not use overflow declarations at all, as for some reason every time I do it screws everything else up.
So I guess the real question and/or request here would be:
How do I do what I would like to do in the above code?
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
body {
background: gray;
}
#page_wrapper {
background: yellow;
height: 100%;
width: 100%;
}
#content {
width: 980px;
background: white;
height: 100%;
margin: 0 auto;
}
header {
height: 160px;
width: 980px;
background: blue;
}
#content-wrapper {
background: green;
height: 100%;
position:relative;
}
footer {
height: 120px;
width: 980px;
background: orange;
position:absolute;
bottom:0;
}
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Page Title</title>
</head>
<body>
<div id="page_wrapper">
<div id="content">
<div id="content-wrapper">
<header>this is the header...</header>
test
<footer>this is the footer</footer>
</div>
</div>
</div>
</body>
</html>
The body tag is already "100% of the available space if the content isn't long enough to push it down."
So just use that element for anything you need (background images, etc). Take a look at CSS properties such as background-position to control where something is visible inside the <body> tag.
Otherwise, I would go for min-height: 100%; on the wrapper but am not sure how compatible that is with older versions of IE. And it may or may not do funky things on a touchscreen devices.
In general however, HTML is not very good at vertical layout. Especially in relation to the browser window's height. Most of the issues can be worked around in modern browsers, but it's better to simply accept this constraint and design your website around the assumption that you have no control over the height. It's just not worth the headache.
You don't need the page wrapper or content div. You can just set the body width to 980px. You can also set your html and body to whatever color you want.
While W3Schools has gotten a deservedly bad rap, they've cleaned up quite a bit and aren't a bad, quick reference.
Beginning to wonder if I am missing something obvious but I have been searching for days now and still haven't been able to find a definitive answer.
What I am looking for is a Source ordered Content CSS layout for a two column page (menu to right) with header and sticky footer, and preferably no nasty hacks. Preferable Source order of:
{content}
{rightmenu}
{footer}
{header}
Like I say I'm not getting too far in trying to build this for myself, nor have I been able to find a suitable example anywhere. Any suggestions?
Thanks
content right, with sidebar left is perhaps the easiest floated layout to make, just float the content right with a width, let the left fill the space with overflow to avoid wrapping. footer goes below by clearing.
As for the header put a fake header div first in the source, presuming there may be a logo or something to go in it, even though you might not want hordes of links up there if there is a big dropdown menu to go in there or something like that. Anyway I'd make the "fake" header tall enough to create the space you need then put any actual content in it, then put the content you want to appear top only in a div at the bottom and absolutely position it.
here's a fiddled mockup
This is the best I can come up with at the moment. Bit of a mixture of relative and absolute positioning but seems to work. Can anyone see any problems with this.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<style>
* {
margin: 0;
}
html, body {
height: 100%;
}
.container {
min-height: 100%;
/*height: auto !important;*/
height: 100%;
margin: 0 auto -2em;
}
.content{
float: left;
width: 80%;
}
.menu{
margin-left: 80%;
}
.header{
position: absolute;
top: 0px;
height: 3em;
width: 100%;
}
.clearheader{
height: 3em;
}
.footer, .clearfooter {
height: 2em;
clear: both;
}
.container {
background: grey;
}
.header{
background: cyan;
}
.clearheader{
background: cyan;
}
.footer{
background: blue;
}
.clearfooter {
background: lightblue;
}
</style>
<link rel="stylesheet" href="NJC layout2.css" ... />
</head>
<body>
<div class="container">
<div class="clearheader"></div>
<div class="content">Content</div>
<div class="menu">menu</div>
<div class="clearfooter"></div>
</div>
<div class="header">header</div>
<div class="footer">Footer</div>
</body>
</html>
If I understand your question right, this should be your answer:
http://www.positioniseverything.net/ordered-floats.html
I actually think this article is explaining everything quite nice.
In the markup shown below, I'm trying to get the content div to stretch all the way to the bottom of the page but it's only stretching if there's content to display. The reason I want to do this is so the vertical border still appears down the page even if there isn't any content to display.
Here is my DEMO:
body {
font-family: Trebuchet MS, Verdana, MS Sans Serif;
font-size:0.9em;
margin:0;
padding:0;
}
div#header {
width: 100%;
height: 100px;
}
#header a {
background-position: 100px 30px;
background: transparent url(site-style-images/sitelogo.jpg) no-repeat fixed 100px 30px;
height: 80px;
display: block;
}
#header, #menuwrapper {
background-repeat: repeat;
background-image: url(site-style-images/darkblue_background_color.jpg);
}
#menu #menuwrapper {
height:25px;
}
div#menuwrapper {
width:100%
}
#menu, #content {
width:1024px;
margin: 0 auto;
}
div#menu {
height: 25px;
background-color:#50657a;
}
<form id="form1">
<div id="header">
<a title="Home" href="index.html" />
</div>
<div id="menuwrapper">
<div id="menu">
</div>
</div>
<div id="content">
</div>
</form>
Your problem is not that the div is not at 100% height, but that the container around it is not.This will help in the browser I suspect you are using:
html,body { height:100%; }
You may need to adjust padding and margins as well, but this will get you 90% of the way there.If you need to make it work with all browsers you will have to mess around with it a bit.
This site has some excellent examples:
http://www.brunildo.org/test/html_body_0.html
http://www.brunildo.org/test/html_body_11b.html
http://www.brunildo.org/test/index.html
I also recommend going to http://quirksmode.org/
I'll try to answer the question directly in the title, rather than being hell-bent on sticking a footer to the bottom of the page.
Make div extend to the bottom of the page if there's not enough content to fill the available vertical browser viewport:
Demo at (drag the frame handle to see effect) : http://jsfiddle.net/NN7ky
(upside: clean, simple. downside: requires flexbox - http://caniuse.com/flexbox)
HTML:
<body>
<div class=div1>
div1<br>
div1<br>
div1<br>
</div>
<div class=div2>
div2<br>
div2<br>
div2<br>
</div>
</body>
CSS:
* { padding: 0; margin: 0; }
html, body {
height: 100%;
display: flex;
flex-direction: column;
}
body > * {
flex-shrink: 0;
}
.div1 { background-color: yellow; }
.div2 {
background-color: orange;
flex-grow: 1;
}
ta-da - or i'm just too sleepy
Try playing around with the following css rule:
#content {
min-height: 600px;
height: auto !important;
height: 600px;
}
Change the height to suit your page. height is mentioned twice for cross browser compatibility.
you can kinda hack it with the min-height declaration
<div style="min-height: 100%">stuff</div>
You can use the "vh" length unit for the min-height property of the element itself and its parents. It's supported since IE9:
<body class="full-height">
<form id="form1">
<div id="header">
<a title="Home" href="index.html" />
</div>
<div id="menuwrapper">
<div id="menu">
</div>
</div>
<div id="content" class="full-height">
</div>
</body>
CSS:
.full-height {
min-height: 100vh;
box-sizing: border-box;
}
While it isn't as elegant as pure CSS, a small bit of javascript can help accomplish this:
<html>
<head>
<style type='text/css'>
div {
border: 1px solid #000000;
}
</style>
<script type='text/javascript'>
function expandToWindow(element) {
var margin = 10;
if (element.style.height < window.innerHeight) {
element.style.height = window.innerHeight - (2 * margin)
}
}
</script>
</head>
<body onload='expandToWindow(document.getElementById("content"));'>
<div id='content'>Hello World</div>
</body>
</html>
The min-height property is not supported by all browsers. If you need your #content to extend it's height on longer pages the height property will cut it short.
It's a bit of a hack but you could add an empty div with a width of 1px and height of e.g. 1000px inside your #content div. That will force the content to be at least 1000px high and still allow longer content to extend the height when needed
Try Ryan Fait's "Sticky Footer" solution,
http://ryanfait.com/sticky-footer/
http://ryanfait.com/resources/footer-stick-to-bottom-of-page/
Works across IE, Firefox, Chrome, Safari and supposedly Opera too, but haven't tested that. It's a great solution. Very easy and reliable to implement.
Try:
html, body {
height: 102%;
}
.wrapper {
position: relative;
height: 100%;
width: 100%;
}
.div {
position: absolute;
top: 0;
bottom: 0;
width: 1000px;
min-height: 100%;
}
Haven't tested it yet...
Sticky footer with fixed height:
HTML scheme:
<body>
<div id="wrap">
</div>
<div id="footer">
</div>
</body>
CSS:
html, body {
height: 100%;
}
#wrap {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -60px;
}
#footer {
height: 60px;
}
Try http://mystrd.at/modern-clean-css-sticky-footer/
The link above is down, but this link https://stackoverflow.com/a/18066619/1944643 is ok. :D
Demo:
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="author" content="http://mystrd.at">
<meta name="robots" content="noindex, nofollow">
<title>James Dean CSS Sticky Footer</title>
<style type="text/css">
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;
}
</style>
</head>
<body>
<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>
</body>
</html>
I think the issue would be fixed just making the html fill 100% also,
might be body fills the 100% of the html but html doesn't fill 100% of the screen.
Try with:
html, body {
height: 100%;
}
Also you might like this: http://matthewjamestaylor.com/blog/ultimate-2-column-left-menu-pixels.htm
It isn't quite what you asked for, but it might also suit your needs.
I dont have the code, but I know I did this once using a combination of height:1000px and margin-bottom: -1000px; Try that.
Depending on how your layout works, you might get away with setting the background on the <html> element, which is always at least the height of the viewport.
It is not possible to accomplish this using only stylesheets (CSS). Some browsers will not accept
height: 100%;
as a higher value than the viewpoint of the browser window.
Javascript is the easiest cross browser solution, though as mentioned, not a clean or beautiful one.
#content {
height: calc(100% - the amount of pixels the content div is away from the top);
}
So if your div is 200px from the top, the code you need would be
#content {
height: calc(100% - 200px);
}
I know this is not the best method, but I couldnt figure it out without messing my header, menu, etc positions. So.... I used a table for those two colums. It was a QUICK fix. No JS needed ;)