CSS Layout that uses 100% height - html

Guys, I have a CSS Layout that I am using that has a header, footer and a sidebar on the left. It works great, but the only problem is, I would like the sidebar and the footer to extend to the bottom of the screen if there is not enough content to fill the main content. How do I do this in CSS? I have posted the css here so you can see what I'm working with:
<style type="text/css">
body
{
font: 100% Verdana, Arial, Helvetica, sans-serif;
background: #666666;
margin: 0;
padding: 0;
text-align: center;
color: #000000;
}
.twoColHybLtHdr #container
{
width: 80%;
background: #FFFFFF;
margin: 0 auto;
border: 1px solid #000000;
text-align: left;
}
.twoColHybLtHdr #header
{
background: #DDDDDD;
padding: 0 10px;
}
.twoColHybLtHdr #header h1
{
margin: 0;
padding: 10px 0;
}
.twoColHybLtHdr #sidebar1
{
float: left;
width: 8em;
background: #EBEBEB;
padding: 15px 0;
}
.twoColHybLtHdr #sidebar1 h3, .twoColHybLtHdr #sidebar1 p
{
margin-left: 10px;
margin-right: 10px;
}
.twoColHybLtHdr #mainContent
{
margin: 0 20px 0 9em;
}
.twoColHybLtHdr #footer
{
padding: 0 10px;
background: #DDDDDD;
}
.twoColHybLtHdr #footer p
{
margin: 0;
padding: 10px 0;
}
.fltrt
{
float: right;
margin-left: 8px;
}
.fltlft
{
float: left;
margin-right: 8px;
}
.clearfloat
{
clear: both;
height: 0;
font-size: 1px;
line-height: 0px;
}
</style>
And an example of how to use it:
<div id="container">
<div id="header" style="text-align: center"> Header goes here </div>
<div id="sidebar1">Sidebar is here</div>
<div id="mainContent">Main Content here</div>
<br class="clearfloat" />
<div id="footer">Footer Here</div>
</div>

Checkout sticky footers
http://ryanfait.com/sticky-footer/
http://www.cssstickyfooter.com/
<div id="wrap">
<div id="main" class="clearfix">
</div>
</div>
<div id="footer">
</div>
and
* {margin:0;padding:0;}
html, body, #wrap {height: 100%;}
body > #wrap {height: auto; min-height: 100%;}
#main {padding-bottom: 150px;} /* must be same height as the footer */
#footer {position: relative;
margin-top: -150px; /* negative value of footer height */
height: 150px;
clear:both;}
/* CLEAR FIX*/
.clearfix:after {content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;}
.clearfix {display: inline-block;}
/* Hides from IE-mac \*/
* html .clearfix { height: 1%;}
.clearfix {display: block;}
/* End hide from IE-mac */
Kind of duplicate CSS: fixed to bottom and centered

take a look at http://ryanfait.com/sticky-footer/
you will need to adjust your code slightly as all the content apart from the footer needs to be in a wrapper div.
Josh

It can be done using display:table with 100% height but then naturally IE doesn't support that either. I often do table layouts using divs with the class names table, tr and td and then get IE to replace those classes with the equivalent tag. This way I get around the whole table as layout debate. If you don't care about semantics you can always ignore the purists and do things like this with real tables. It's one thing to be a CSS purist, but it's an expensive religion when the highest market-share browser is an outdated product with 90's technology.

You should look at the min-height CSS property, however be careful because it is not supported correctly on all browsers (notably IE.. - as if that was news to anybody)
You may also want to check out CSS min-height hacks on Google

Related

Two elements - Fixed and flexible width (100% - 170px)

At the top level of my website layout are 4 div tags.
The first one is a full width header section, with css:
#header {
margin-top: 0px;
height: 70px;
border: 4px double rgb(255,255,255);
border-radius: 20px;
background: rgb(88,150,183) no-repeat fixed left top;
padding: 0px;
}
At the bottom is a full width footer:
#footer {
clear: both;
margin: 0px;
color:#cdcdcd;
padding: 10px;
text-align: center;
border: 4px double rgb(88,150,183);
border-radius: 20px;
}
On the left is my main menu section:
#categories {
float:left;
width:150px;
border: 4px double rgb(88,150,183);
border-radius: 20px;
}
All of those 3 elements work fine. They're in the right place and that doesn't change whatever screen resolution the user has on their monitor, or whether they view it on not maximum screen size.
My problem is with the main element of the page - where all the interesting stuff is. It's directly to the right of the menu div - or rather, it should be. My css is:
#main {
float:right;
min-height: 440px;
width: 80%;
margin-bottom: 20px;
padding:20px;
border: 4px double rgb(88,150,183);
border-radius: 20px;
}
width 80% works OK for most of my users, but for those with less resolution, the main element shifts below the menu, which is ghastly.
What I would ideally like is for the width set in the css #main to be something like (100% - 170px), thus leaving a nice margin between the menu and the main bit at all times and never pushing it below the menu. However, css standards don't fulfil that desire yet!
Could someone suggest how I amend my css to give me a nice clean page that's clean for all my users? Or do I need to go back to setting out my page using tables?
Using CSS3 flex
* { box-sizing: border-box; margin: 0; }
#parent{
display: flex;
}
#aside{
width: 170px; /* You, be fixed to 170 */
background: #1CEA6E;
padding: 24px;
}
#main{
flex: 1; /* You... fill the remaining space */
background: #C0FFEE;
padding: 24px;
}
<div id="parent">
<div id="aside">Aside</div>
<div id="main">Main</div>
</div>
Using CSS3 calc
width: calc(100% - 170px);
Example:
* { box-sizing: border-box; margin: 0; }
#aside {
background: #1CEA6E;
width: 170px;
float: left;
padding: 24px;
}
#main {
background: #C0FFEE;
width: calc(100% - 170px);
float: left;
padding: 24px;
}
<div id="aside">Aside</div>
<div id="main">Main</div>
Using float: left; and overflow
* { box-sizing: border-box; margin: 0; }
#aside{
width: 170px; /* You, be fixed to 170 */
float: left; /* and floated to the left */
padding: 24px;
background: #1CEA6E;
}
#main {
background: #C0FFEE;
padding: 24px;
overflow: auto; /* don't collapse spaces */
/* or you could use a .clearfix class (Google for it) */
}
<div id="aside">Aside</div>
<div id="main">Main</div>
Using style display: table;
* { box-sizing: border-box; margin: 0; }
#parent{
display: table;
border-collapse: collapse;
width: 100%;
}
#parent > div {
display: table-cell;
}
#aside{
width: 170px; /* You, be fixed to 170 */
background: #1CEA6E;
padding: 24px;
}
#main{
background: #C0FFEE;
padding: 24px;
}
<div id="parent">
<div id="aside">Aside</div>
<div id="main">Main</div>
</div>
Is this what you are looking for? You don't need any css3
Dont need any css3
.wrapper {
width: 800px;
height: 800px;
background-color: blue;
}
.content {
width: auto;
height: 100%;
background-color: yellow;
}
.menu {
width: 170px;
height: 100%;
float: left;
background-color: red;
}
<div class="wrapper">
<div class="menu">Menu</div>
<div class="content">
Aside
</div>
</div>
You can use 'calc' function supported by all modern browsers and IE9+, or switch to flexbox (supported by IE11+)
See this pen: https://codepen.io/neutrico/pen/MyXmxa
width: calc(100% - 170px);
Keep in mind that all borders matter unless you set 'box-sizing' to 'border-box' (or just remove these borders and apply them on child elements).

Centering div inside of a div & alignment of it's blocks

Well, as it is shown on the screenshot I've linked below, there's a problem with centering this div containing two Tumblr posts columns. I want to have it centered in the part of the page, where no sidebar is given. Moreover, I would like to make posts in two columns following each other without any space. IMG: http://i.stack.imgur.com/VLkkr.jpg
CSS:
body {
margin: 0px;
background-color: antiquewhite;
text-align: center;
word-wrap: break-word;
}
body #content {
width: 900px;
display: inline-block;
margin: 15px 15px 15px 15px;
}
body #content #wrapper {
display: inline-block;
max-width: 900px;
margin-left: auto;
margin-right: auto;
}
body #content #wrapper #posts {
display: inline-block;
background-color: white;
width: 400px;
margin: 0 15px 15px 0px;
padding: 10px;
float: left;
text-align: left;
}
body .sidebar {
display: table;
width: 250px;
height: 100%;
position: fixed;
top: 0px;
right: 0px;
}
body .sidebar .sidebar-inside {
display: table-cell;
max-width: 250px;
margin: 0 auto;
vertical-align: middle;
}
/* etc */
HTML:
<!-- These two columns -->
<div id='content'>
<div id='wrapper'>
{block:Posts}
<div id='posts'>
{block:Photo}
<!-- Here are posts. -->
</div>
{/block:Posts}
</div>
</div>
<!-- Sidebar -->
<div class='sidebar'>
<div class='sidebar-inside'>
</div>
</div>
Help me out, guys! Please!
Maybe a solution (for one thing) with CSS3 Column: (and do not use mulitple times the id atrribute.. use classes.. )
body #content #wrapper {
-moz-column-count:2;
-webkit-column-count:2;
column-count:2;
-moz-column-gap:405px;
-webkit-column-gap:405px;
column-gap:405px;
}
body #content #wrapper #posts {
<strike>float: left;</strike> /*delete this one..*/
-webkit-column-break-inside: avoid;
-moz-column-break-inside: avoid;
column-break-inside: avoid;
}
Edit: user ask for more..
the reason is that the sidebar is position:absolute; so it does not count in space available for centering..
place this just behind </div> from wrapper
<div class="Gh2"></div>
.Gh2 {
width: 250px; /*sidebar width*/
float: right; /*place it to the right where sidebar is*/
height: 1px; /*need some height..*/
}
than:
body #content {
/* width: 900px; deleted those unwanted settings*/
/* display: inline-block; deleted those unwanted settings*/
margin: 15px 15px 15px 15px;
}

How to make nav element 100% width (so other elements will be under it, not right after it)

Sorry for my not very good english.
To understand my problem look this fiddle and this fiddle.
Take a look on the <nav></nav> part. If there are not many elements in menu, next block <section></section> will be right after menu. If there are many elements in menu all fine, because nav has enought width.
How to make next :
And nav must be 100% width not depending on amount of elements.
html <body> code:
<body>
<div class="wrapper">
<header>
<center>
<!-- logo and headers here -->
</center>
</header>
<nav>
<!-- menu here -->
</nav>
<section>
<header><h1>Lorem ipsum</h1></header>
<!-- main content here -->
</section>
<aside>
<section>
<header>
<h1>Lorem ipsum</h1>
<!-- block content here -->
</header>
</section>
</aside>
<footer>
</footer>
</div>
</body>
css code:
body {
color: #8f8f8f;
background-color: #f8f8f8;
background: url(../images/background.jpg) 50% 50% no-repeat #f8f8f8;
/* border: 5px solid #7e7e7e;*/
margin: 0;
}
.wrapper {
max-width: 960px;
margin: auto;
border: 1px solid #7e7e7e;
}
header {
padding: 20px 0;
margin: auto;
}
aside, section {
margin-top: 10px;
}
section {
float: left;
width: 700px;
padding-bottom: 50px;
border: 5px solid #ccc;
}
aside {
float: right;
width: 240px;
border: 1px solid #ccc;
}
aside section
{
width: 230px;
margin-top: 0px;
}
footer {
clear: both;
}
h1, h2, h3, h4, h5, h6 {
margin: 0px;
padding: 0px;
}
section header {
background-color: #CCCCCC;
padding: 5px 0;
margin: 0px;
}
section header h1 {
padding-left: 20px;
}
section p {
padding: 10px 20px;
margin: 0px;
}
/* css for nav */
The nav is appearing on the left because you haven't cleared your floating lis. If you add the following style then it should work:
nav:after {clear:both; display:block; content:'';}
Example
I make it a point of always adding a .clearfix css class to my reset stylesheet, as you will most likely need to clear out floated regions more than once in your site. It helps to give containers with only floated content back their height.
Add this to your stylesheet
CSS
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix{
*zoom:1;
}
Then add class="clearfix" to your <nav> element (or even on your <ul class="nav">)
Here is a JSfiddle: http://jsfiddle.net/Uuyp8/8/
This clearfix solution is based upon the (famous?) micro clearfix hack explained in more detail here: http://nicolasgallagher.com/micro-clearfix-hack/
Make nav float:left and the pink section clear:left (Or nav clear:right, same effect).

Layout problems with CSS

I got some problems with layouting in CSS. Here is the code I am talking about: Fiddle.
The <div id="header"> should have the height of the <div id="menubuttons"> which I marked red.
I always thought that if you don't state the height of a div it will get the height of it's children.
The <div class="contentLine> is stuck to the <div id="theme"> although I defined margin-top: 20px;.
The right column always has greater margin than the left column. I want both to have the same margin to the browser window.
CSS
body {
margin: 0 auto;
padding: 0;
font-family:'Share', cursive;
}
#header {
width: 100%;
vertical-align: middle;
}
#header_logo {
width:;
float: left;
margin: 11px 20px 20px 20px;
background-color:;
}
#menubuttons {
margin-right: 0;
margin-top: 0;
height: 2.5em;
line-height: 2.5em;
display: inline-block;
background-color: red;
}
#menubuttons ul {
list-style-type: none;
margin:0;
padding:0;
}
#menubuttons li {
float: left;
margin-right: 20px;
}
a {
font-family:'Share', cursive;
}
a:link {
text-decoration:none;
}
a:visited {
text-decoration:none;
}
a:hover {
text-decoration:underline;
}
a:active {
text-decoration:underline;
}
#theme {
width: 100%;
height: 400px;
background-color: green;
margin-top: 0;
float: left;
}
.contentLine {
margin: 0 auto;
margin-top: 20px;
width: 96%;
}
.contentLine .column {
float: left;
margin: 0;
width: 30%;
margin-right: 1%;
padding: 1%;
position: inherit;
/* shadow for seeing div boundaries */
box-shadow: 0 0 1px black inset;
}
.contentLine #last {
margin-right: 0;
}
Let me go 1 by 1
1) Your <div id="header"> contains floated elements, you need to clear that, so use overflow: hidden; on parent element i.e #header
2) Again, you've floated #theme but you've set it to width: 100%; so you don't need float there.
3) About the last you need to set the margins accordingly, right now it's 1% so you need to calculate this correctly, I would like to suggest you to use box-sizing: border-box; and set 33% width for each element and than apply padding-right
Demo
Also make sure you clear your floating elements which are nested inside contentLine.
If you are not one of those IE fans, than you can use the snippet below, which will self clear the parent element in a better way.
.clear:after { /* Much much better than overflow: hidden; */
content: "";
display: table;
clear: both;
}
Update your html
</ul>
<!--Menu ends here -->
</div>
<!--menubuttons ends here -->
<!--Add following div to your code -->
<div class="clear"></div>
</div>
<div id="theme">
Update your CSS
.clear{
clear:both;
}
This should help.
- will be reusable also.

Sticky Footer in Css gets in the way of container, or creates extra space

Soooooo I'm making a sticky footer in Css. It doesn't work the way I want it to. The footer sticks to the bottom, but I also want 100% height for the page. This doesn't work, and I've tried a lot. Currently, the footer gets in the way of the container, and they overlap. If i give the container margin-bottom: 70px;, it creates extra unwanted space when the content is very small, making an unnecessary scrollbar.
Here's my code:
<html><head>
<style type='text/css'>
body {
font-family: 'Open Sans', sans-serif;
font-size: 20px;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
.container {
margin-left: auto;
margin-right: auto;
text-align: left;
width: 800px;
height: auto !important;
min-height: 100%;
}
.bold-show {
font-family: Helvectica, sans-serif;
font-size: 96px;
background-color: rgba(0, 0, 0, 0.95);
color: #eeeeee;
padding: 50px;
}
#footer {
position: relative;
height: 70px;
width: 100%;
text-align: center;
display: table;
margin-top: -70px;
background-color: rgba(0, 0, 0, 0.9);
color: #eeeeee;
}
#footer div {
display: table-cell;
vertical-align: middle;
}
</style>
</head><body>
<div class='container'>
<div class='bold-show'>
Donuts. Food for thought. This is my place, this fox will guide you. Random filler text for the win.
</div>
</div>
<div id='footer'>
<div>
We support a free and open internet.
</div>
</div>
</body></html>
Also, this is not the actual site. Just testing to implement on real site.
I think that this is what you are looking for:
<div id="wrapper">
<header></header>
<div id="main"></div>
<footer></footer>
</div>
body, html, #wrapper { height: 100%; } /* Create space for elements to be 100% */
#wrapper { display: table; } /* Create a table-structure */
#wrapper > * { display: table-row; } /* All direct children behave as rows */
#wrapper > header,
#wrapper > footer { min-height: 100px; } /* These must be at least 100px */
#main { height: 100%; } /* Let the mid section fill up the remaining space */