I'm trying to use the CSS table display to layout my page and I'm having trouble getting my main content area to take up the entire area (vertically) between the header and the footer. The main div contains some floated elements that don't necessarily extend the length of the screen. Basically, no matter what I do, the area of my main content is decided by the vertical height of these elements. Is there anything I can do about this? Thanks!
Html:
<div id="all-container">
<div id="header">
...
</div>
<div id="table-container">
<div id="content">
<div id="side-bar">
...
</div>
<div id="main">
... some content that's floated ...
</div>
</div>
</div>
<div id="footer"></div>
</div>
CSS:
#all-container {
margin:0px;
position:relative;
min-height:100%;
background-color:#E6DCD8;
}
#header {
height:60px;
padding-left:20px;
padding-right:20px;
background-color:#685642;
}
#table-container {
display:table;
height:100%;
}
#content {
display:table-row;
height:100%;
}
#side-bar {
display:table-cell;
vertical-align:top;
padding-right:100px;
height:100%;
padding-bottom:60px;
}
#main {
display:table-cell;
vertical-align:top;
border-left-style:solid;
border-left-width:normal;
border-left-color:#685642;
padding-bottom:60px;
height:100%;
}
#footer {
position:absolute;
width:100%;
height:50px;
bottom:0px;
background-color:#685642;
}
I'm going to take a shot in the dark at trying to answer. These are my suggestions, not necessarily the canonical correct answer you're looking for.
Not answering exactly to the question on table layout per say, but I'm offering other ways to achieve the same desired result.
This is your original code in the fiddle http://jsfiddle.net/CRzfS/
I think you have at least two design objectives here you want to achieve:
make a full screen height layout
make a 2-column layout
I'll have to put it forward first, that there are many ways to achieve the objective, but all has their limitations due to browser support. I also advise against table layouts unless it is necessary.
For me, display: table is only used for one reason mostly: Making vertical-align work in a fixed-height container, especially vertical-align: middle. There are also relevant uses for the auto calculation of table-cell widths from a fixed-width table, but it all depends on how you want to present data or information.
We'll face the issues one by one.
Full Height
First is the layout's height issue. Height flexibility has always been a sore point in web design layouts.
To fill screen height only, you can look at this sticker footer implementation:
http://www.cssstickyfooter.com/
Here's an example fiddle with full screen height, not taking footer implementation into account: http://jsfiddle.net/CRzfS/3/
CSS:
html, body {
height: 100%;
min-height: 100%;
}
For liquid height layout you can look at this: http://www.mightymeta.co.uk/superstretch-a-vertically-fluid-layout-using-css/
For a proper flexible height, you'll have to use CSS Flexbox.
http://caniuse.com/#feat=flexbox
You can try it out here http://flexiejs.com/playground/
Your example implemented using CSS Flexbox: http://jsfiddle.net/CRzfS/4/
CSS:
html, body {
height: 100%;
min-height: 100%;
margin: 0;
padding: 0;
}
#all-container {
display: -webkit-box;
display: -moz-box;
display: box;
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
box-orient: vertical;
margin:0px;
position:relative;
min-height:100%;
height: 100%;
background-color:#E6DCD8;
}
#header {
height:60px;
padding-left:20px;
padding-right:20px;
background-color:#685642;
}
#table-container {
display: -webkit-box;
display: -moz-box;
display: box;
-webkit-box-flex: 1;
-moz-box-flex: 1;
box-flex: 1;
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
box-orient: vertical;
}
#content {
display: -webkit-box;
display: -moz-box;
display: box;
-webkit-box-flex: 1;
-moz-box-flex: 1;
box-flex: 1;
overflow: hidden;
}
#side-bar {
vertical-align:top;
min-width: 150px;
}
#main {
-webkit-box-flex: 1;
-moz-box-flex: 1;
box-flex: 1;
vertical-align:top;
border-left-style:solid;
border-left-width:normal;
border-left-color:#685642;
}
#footer {
width:100%;
height:50px;
background-color:#685642;
}
Two-column Layout
There are many ways to achieve this. Considerations have to be made for the differing screen sizes you're supporting(which is a major headache). Each has their own drawbacks. And also it depends on your width requirements i.e. fixed width, flexible width.
semantic way by absolute positioning the sidebar and setting margin for main content
common method used by layout frameworks via floating sidebar and container so they are side by side
using display: inline-block to the same effect as #2.
The first method here, sets your #side-bar after the #main in the HTML. Then using CSS absolute positioning to set #side-bar to the left side, and setting margin-right for your #main. http://jsfiddle.net/CRzfS/2/
HTML:
<div id="table-container">
<div id="content">
<div id="main">
... some content that's floated ...
</div>
<div id="side-bar">
...
</div>
</div>
</div>
CSS:
#table-container {
position: relative;
}
#content {
height: 200px;
}
#side-bar {
width: 200px;
position: absolute;
left: 0;
top: 0;
background-color: rgba(255,255,255,0.5);
height: 100%;
}
#main {
margin-left: 200px;
height: 100%;
overflow: hidden; // for floated elements within
}
The second method here, using the original HTML, you'll only need to set the CSS. http://jsfiddle.net/CRzfS/5/
#table-container {
overflow: hidden;
}
#content {
width: 100%;
}
#side-bar {
width: 33%;
float:left;
}
#main {
width: 66%;
float: left;
vertical-align:top;
border-left-style:solid;
border-left-width:normal;
border-left-color:#685642;
padding-bottom:60px;
min-height: 100px;
overflow: hidden; // for floated elemnts
}
How to combine these 2 layout requirements together will be difficult if I'm not sure of what you exactly require for the vertical height part.
I'll need more information before I can give a relevant answer tailored to your question.
Resources
If you're open to layout grid systems framework, I'll suggest you take a look at: http://twitter.github.com/bootstrap/scaffolding.html#gridSystem
Even if you don't want to use it, just looking at the CSS implementation will yield you interesting insights.
I'll be adding other jsfiddle examples as more information on the question comes.
Edit: More information and explanations added.
Related
So I am learning a bit more about using CSS flex instead of using static positioning of content. However, I have defined my link styles as well as bold styles. My guess is that it's adapting to the container that is in (which is using flex feature) and that is why it is stretching across the size of the container it is inside. My question now is, how do I fix this? I've seen that I can do "display:inline-block" on the link, but that has not fixed it.
Here is my code:
.container{
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
width: 80%;
margin: auto;
background-color:#fff;
overflow: hidden;
font-size: 15px;
line-height: 20px;
padding:1em;
}
.container > * {
padding: 15px;
-webkit-flex: 1 100%;
flex: 1 100%;
}
a{
text-decoration:none;
border-bottom-style:double;
border-bottom-width:2px;
color:#99d3df;
display:inline-block;
padding:0px;
overflow: hidden;
}
i{
display:inline-block;
color:#88bbd6;
text-decoration:italic;
}
And what I have:
This is a Google Link<BR>
Google is <i>extremely helpful</i>!
This is what it looks like for reference.
Problem image
It seems you missed the .container wrapper div in the markup you provided.
Let's look at this code:
<!-- HTML -->
<div class="container">
<span>This is a </span><a href="http://google.com">Google Link</a
</div>
<div class="container">
<span>Google is </span><i>extremely helpful</i>!
</div>
<!-- /HTML -->
/* CSS */
.container > * {
padding: 15px;
-webkit-flex: 1 100%;
flex: 1 100%;
}
Property flex with value of 1 100% means we tell the browser to style any elements (the asterisk *) nested in .container to have 100% width of their parent's width.
I would suggest you to just remove that part to fix the problem.
Here's my approach to your markup.
.container {
display: flex;
width: 80%; /* flexible value */
flex-direction: row; /* this is to make sure that we'll have side-to-side layout within .container */
}
a {
text-decoration: none;
border-bottom-style: double;
border-bottom-width: 2px;
color: #99d3df;
display: inline-block;
padding: 0px;
}
a, i{
margin-left: 5px; /* this is to give a 10px spacing */
}
<div class="container"><span>This is a </span>random link<span></span></div>
<div class="container"><span>Google </span><i>is extremely helpful! </i></div>
It is working fine when I tried your code in js fiddle
see in this image
May be some other css is affecting your links to stretch it out.
I'm trying to get some list items to stretch across a list
This is the relevant code
#navbar ul
{
list-style-type: none;
margin: 0;
padding: 0;
width: 100%;
}
#navbar li
{
display: inline;
float: left;
width: 33.33%;
}
Here's what it normally looks like:
But sometimes when I leave the page and come back later (not after reloading) this happens:
Setting the individual item width to 33.3% makes it one pixel short and making it 33.333% makes the problem worse...
You could easily achieve this layout using css tables instead. Widely supported and semantically sound.
#navbar ul {
width: 100%;
display: table;
table-layout: fixed; /* makes all cells equal width */
}
#navbar li {
display: table-cell;
}
http://jsfiddle.net/kBnrz/1/
remove padding of parent of "ul"
Just fake it:
#navbar ul li{
width:33%;
}
#navbar ul li:last-child{
width:34%;
}
Also include this style:
* { box-sizing: border-box }
ref: http://www.paulirish.com/2012/box-sizing-border-box-ftw/
Suggestion:
#Miro try CSS Flexbox layout, it will help you, but it works only in modern browsers.
CSS Flexbox
The CSS Flexible Box Layout Model, or "flexbox", is one of the specification in CSS3. It provides for the arrangement of elements on a page such that the elements behave predictably when the page layout must accommodate different screen sizes and different display devices. For many applications, the flexible box model provides an improvement over the block model in that it does not use floats, nor do the flex container's margins collapse with the margins of its contents.
Here is one example
Html
<div class="box">
<div class="A">A</div>
<div class="B">B</div>
<div class="C">C</div>
</div>
StyleSheet
html, body{
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
.box {
display: flex;
flex-flow: row nowrap;
justify-content: center;
align-content: center;
align-items: flex-start;
}
.box div.A {
order:1;
flex: 1 1 auto;
align-self: auto;
min-width: 0;
min-height: auto;
}
.box div.B {
order:2;
flex: 1 1 auto;
align-self: auto;
min-width: 0;
min-height: auto;
}
.box div.C {
order:2;
flex: 1 1 auto;
align-self: auto;
min-width: 0;
min-height: auto;
}
Here is the Demo
This Link will help you.
I want to create an automatic scaling layout without using the height property. I use a float layout between two divs, as shown in the image. The boxes in the middle have content of different size and I want the boxes to scale in such way, that all have the same height.
Try reading this article at css-tricks.
My favorite choice is probably the one taken from Paul Irish's blog at HTML5Rocks - however it does rely on modern browsers. I've created a JSFiddle based on his code:
CSS
.box {
/* basic styling */
width: 100%;
height: 95px;
border: 1px solid #555;
font: 14px Arial;
/* flexbox setup */
display: -webkit-box;
-webkit-box-orient: horizontal;
display: -moz-box;
-moz-box-orient: horizontal;
display: box;
box-orient: horizontal;
}
.box > div {
-webkit-box-flex: 1;
-moz-box-flex: 1;
box-flex: 1;
}
/* our colors */
.box > div:nth-child(1){ background : #FCC; }
.box > div:nth-child(2){ background : #CFC; }
.box > div:nth-child(3){ background : #CCF; }
HTML
<div class="box">
<div>A</div>
<div>B</div>
<div>C</div>
</div>
However note this won't work with legacy browsers, and if you're targeting those, I suggest you just adopt a table layout.
I have made a JsFiddle.
What I basically do, is use the position:absolute in combination with the top and bottom CSS property to force the inner div to take full height.
HTML:
<div id="top"></div>
<div id="middle"></div>
<div id="bottom"></div>
CSS:
body{
margin: 0px;
padding: 0px;
border: 0px;
}
#top{
width: 100%;
height: 30px;
background: blue;
position: absolute;
}
#bottom{
width: 100%;
height: 30px;
background: yellow;
position: absolute;
bottom: 0px;
}
#middle{
width: 30%;
position: absolute;
top: 30px;
bottom: 30px;
background: gray;
}
place the middle one in a div
and make the three divs inside it and give them height 100%
Use min-height. Examples of this property are provided below.
MDN
MSDN
after researching the flexible box model for a whole day, I must say I really like it. It implements the functionality I implement in JavaScript in a fast and clean way. One thing however bugs me:
I can't expand a div to take the full size calculated by the flexible box model!!!
To illustrate it I'll proved an example. In it the two flexible places take the exact with and height, but the div inside it only takes the height of the "<p>...</p>" element. For this example it doesn't matter but what I originally was trying was placing a "flexible box model" inside another "flexible box model" and this must be possible in my opinion
html, body {
font-family: Helvetica, Arial, sans-serif;
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
}
#box-1 {
background-color: #E8B15B;
}
#box-2 {
background-color: #C1D652;
}
#main {
width: 100%;
height: 100%;
overflow-x: auto;
overflow-y: hidden;
}
.flexbox {
display:-moz-box;
display:-webkit-box;
display: box;
text-align: left;
overflow: auto;
}
H1 {
width: auto;
}
#box-1 {
height: auto;
-moz-box-orient: vertical;
-webkit-box-orient: vertical;
box-orient: vertical;
-moz-box-flex: 3;
-webkit-box-flex: 3;
box-flex: 3;
}
#box-2 {
height: auto;
min-width: 50px;
-moz-box-orient: vertical;
-webkit-box-orient: vertical;
box-orient: vertical;
-moz-box-flex: 1;
-webkit-box-flex: 1;
box-flex: 1;
}
#fullsize{
background-color: red;
height: 100%;
}
<div id="main" class="flexbox">
<div id="box-1" class="flexbox">
<div id="fullsize">
<p>Hallo welt</p>
</div>
</div>
<div id="box-2" class="flexbox">
</div>
</div>
I've been wrestling with this myself, but have finally managed to come up with a solution.
See this jsFiddle, although I have only added webkit prefixes so open in Chrome.
You basically have 2 issues which I will deal with separately.
Getting the child of a flex-item to fill height 100%
Set position:relative; on the parent of the child.
Set position:absolute; on the child.
You can then set width/height as required (100% in my sample).
Fixing the resize scrolling "quirk" in Chrome
Put overflow-y:auto; on the scrollable div.
The scrollable div must have an explicit height specified. My sample already has height 100% but if none is already applied you can specify height:0;
See this answer for more information on the scrolling issue.
You must also make the div you want to expand a flex-box as well and add a flex value.
This fixes the problem.
#fullsize{
background-color: red;
display: -webkit-box;
display: box;
display: -moz-box;
box-flex:1;
-webkit-box-flex:1;
-moz-box-flex:1;
}
I have following html site structure:
<body>
<div id="header"></div>
<div id="container">
<div id="util_header"></div>
<div id="contentwrapper" class="frontpage">Content</div>
</div>
</body>
Now I want to center the #container. The works when I apply following css:
#container {
width: 960px;
margin: auto;
background:red;
}
#util_header{
width: 100%; height:32px;
position: relative;
background:url('../images/logo.png') no-repeat #eeeeee;
border-top:1px solid #b6bac0;
}
#header {
width: 100%; height:32px;
position: absolute;
background:#eeeeee;
border-top:1px solid #b6bac0;
}
#contentwrapper {
float: left;
position: relative;
height: auto;
background:red;
}
The magin: auto; centers the container. My problem is that I need the container to be larger, but when I increase width from 960 to 980 I get a vertical scrollbar. I played around with the css but got no clue how to manage that problem.
Any ideas?
#ArtWorkAD,
CSS3 introduced the Flexible box model, maybe you can use it depending the audience of your website...
So to Vertically & Horizontally center block Level elements in the body element, you'd just have to write this CSS declaration:
body {
display: box;
box-orient: horizontal;
/* horizontally centered */
box-pack: center;
/* vertically centered */
box-align: center;
width: 100%;
height : 100%;
}
http://hacks.mozilla.org/2010/04/the-css-3-flexible-box-model/
edit
To have wide browser support, you can always rely on CSS hacks and do some negative margin trickery as seen on http://www.jakpsatweb.cz/css/css-vertical-center-solution.html
;)
Oh and if you don't want a scrollbar at all, make sure you have put an overflow:hidden on the body element.