Dual Column Layout in CSS with dynamic height in IE - css

I have a dual column layout using CSS:
<div id="nav"></div>
<div id="left_column"></div>
<div id="right_column"></div>
<div id="footer"></div>
#left_column{
float: left;
width: 463px;
display: inline-block;
margin-left: 12px;
}
#right_column{
float: right;
width: 463px;
display: inline-block;
margin-right: 12px;
}
#footer{
clear:both;
text-align: center;
padding: 40px;
}
This works fine in Firefox, but creates problems in IE. The main problem is that the content of these two columns is dynamic and never amounts to the same height with either column sometimes having a bigger height than the other. In IE, the left and right columns align with the bottom which creates a variable amount of space between the shorter column and the nav. What I would like is to have the columns align with the top/nav and then when an element is inserted with ajax at the top of the left_column, it just pushes the content in that column down and the two columns remained aligned at the top. I hope that made sense, any help is appreciated.
...
Using igoogle layout as an example, I have added the following css and it seems to be working:
<div id="outside">
<div id="left_column"></div>
<div id="right_column"></div>
</div>
<div id="footer"></div>
#outside{
display: block;
float: none;
margin: 0 0 1em;
overflow: hidden;
position: static;
width: auto;
}
.column {
display: block;
width: 40%;
float: left;
margin-left: 0px;
}
I have yet to fully implement it, but this seems to be aligning the columns with the top regardless of height and expands/contracts with the top as a reference point.

Maybe this resource will be of help to you:
http://matthewjamestaylor.com/blog/equal-height-columns-cross-browser-css-no-hacks
Since you want to have only 2 columns,
have a look at the given example here:
http://matthewjamestaylor.com/blog/equal-height-columns-2-column.htm
Kind regards, mtness.

Related

Centered website layout with varying sized columns and responsive?

I'm looking to create a website (or at the very least a homepage) like Joules.com
I essentially want to create boxes side by side in varying sizes but want them to resize or move to a new line with the browser window resizing (responsive?). It's also necessary for them to be centered. I can get to the point where I have the divs side by side but they don't seem to be centered... Here's what I have so far. Any help would be greatly appreciated. I'm kind of nooby in this department but wanting to learn!
CSS
#container {
width: 100%;
margin: 0 auto;
overflow: hidden;
}
#Womens {
height: auto
width: 241px;
float: left;
margin: 0 auto;
text-align:center;
}
#Mens {
height: auto
margin: 0 auto;
width: 241px;
float: left;
text-align:center;
}
#Footwear {
height: auto
margin: 0 auto;
width: 241px;
float: left;
text-align:center;
}
#Accessories {
height: auto
margin: 0 auto;
width: 241px;
float: left;
text-align:center;
}
HTML
<body><center>
<div id="container">
<div id="Womens">Womens</div>
<div id="Mens">Mens</div>
<div id="Footwear">Footwear</div>
<div id="Accessories">Accessories</div>
</div>
First at all you don't need to use an ID for each element, since your CSS code is the same for everyone use a classname instead:
<div id="container">
<div class="column">Womens</div>
<div class="column">Mens</div>
<div class="column">Footwear</div>
<div class="column">Accessories</div>
</div>
Then don't use float because you can't center those elements, use inline-block:
#container {
font-size:0;
text-align:Center;
}
.column {
font-size:14px;
display:inline-block;
}
Check this Demo Fiddle
Your CSS could be much simpler by using a class (Don't Repeat Yourself ;) ).
If you put text-align: center; on the container instead the container itself and its child contents will be centered. If you want you could then override the setting for the separate columns, or just for their content.
You've also used fixed pixel values for the column width, so they can't really be "responsive." You can use percentage values there as well, but that can have some screwy side effects. Note that 4 columns even with auto margins still need to be < 100% or else they wrap oddly. They also might collapse or overlap at smaller sizes. You can set a min-width on the container or the columns to help prevent this, along with a margin-bottom to keep them separate if they do wrap.
Also, if you just use percentage width and inline-block, the columns will be aligned at the bottom. Using vertical-align: top; fixes that. You said initially you wanted different heights, but if you didn't you could set a min- or max-height & put something like overflow:scroll on the content.
#container {
width: 100%;
min-width: 320px;
margin: 0 auto;
overflow: hidden;
text-align:center;
}
.box {
margin: 0 auto;
margin-bottom: 10px;
width: 20%;
min-width: 90px;
padding: 1%;
vertical-align: top;
display: inline-block;
background-color: #678;
color: #fff;
}
.content {
background-color: #fff;
color: #333;
padding: 1em;
text-align: left;
}
<body>
<div id="container">
<div id="Womens" class="box">Womens
<div class="content">This is some left-aligned women's content about/for women. View the Code Snippet in fullscreen!</div>
</div>
<div id="Mens" class="box">Mens
<div class="content">This is some left-aligned men's content about/for men. If you resize the browser the columns will be responsive, but break after a certain point.</div>
</div>
<div id="Footwear" class="box">Footwear
<div class="content">This is some left-aligned footwear content about/for feet. Feet are weird.</div>
</div>
<div id="Accessories" class="box">Accessories
<div class="content">This is some left-aligned accessory content about stuff you men or women could potentially put on their feet, or whatever.</div>
</div>
</div>
</body>

box model extra pixels width

Thank you for reading this
I'm having an odd problem with layouts. I've used Meyer's CSS reset. I have a div containing 3 divs as follows. I've not added any content yet.
<div id="container">
<div class="gallery"></div>
<div class="gallery"></div>
<div class="gallery"></div>
</div>
The following styles are applied
#container{
width: 954px;
}
.gallery {
display: inline-block;
width: 270px;
margin: 24px;
}
So in my mind each gallery is 24px + 270px + 24px = 318px wide. The combined width of the 3 gallery divs is therefore 318px * 3 = 954px.
However the divs will only display across a single row when I increase the width of #container to 960px.
Where are the 6 mystery pixels coming from? I've double checked all other border, margin and padding values are 0. I've even set outline to 0px although this shouldn't have an effect either way.
The inner divs are set to display: inline-block;. This means that any white-space between them will result in a single space added between them in the layout.
Option 1
While not a pleasant solution, you will not see the extra space if you do this.
<div id="container">
<div class="gallery"></div><!--
--><div class="gallery"></div><!--
--><div class="gallery"></div>
</div>
Option 2
Another solution that is unpleasant for different reasons, is to set font-size: 0; on the #container. If you intend to have text inside, you will need to set the font-size of the elements inside to a pixel value.
#container{
width: 954px;
font-size: 0;
}
.gallery {
display: inline-block;
width: 270px;
margin: 24px;
}
Option 3
The cleanest solution however would probably be to float the inner divs left, and clear-fix the wrapper.
#container{
width: 954px;
}
#container:after {
content: "";
display: table;
clear: both;
}
.gallery {
display: inline-block;
width: 270px;
margin: 24px;
float: left;
}
Have you tried to "nowrap" the white-space?

Responsive CSS - how to 'dynamically' align div to parent width with padding/margin?

I can't make it any clearer than this, sorry. I want to properly align 4 divs (on a width of 1150px as that is max-width of the content div) and upon resizing when it can't do 4, 3 in the center etc etc)
On >1150px screens it would/should like this: http://i.imgur.com/KaOPqZK.png. Now, the closest I can come is this: http://i.imgur.com/6khwQkR.png. I can set the first-child margin to 0 on the left one, but as there are multiple rows, those would still have the padding. Creating new rows as divs isn't possible either, because that would ruin everything when it's resized and only shows 3/1 on both rows.
When resizing it should center, with even margins on all sides, and not like this as it is right now: http://i.imgur.com/GiR1nZ2.png.
Basically all the code I have right now is this, simply because I know of no other way.
div.project-container {
float: left;
margin: 0 8px 30px 8px;
position: relative;
width: 270px;
}
I'm guessing it has to be Javascript who rescues the day, and I'm fine with that. Pointers in the right direction, examples on the internets, all is welcome. Thank you.
Adapted from an old answer :
HTML
<div id="container">
<div class="obj">1</div>
<div class="obj">2</div>
<div class="obj">3</div>
<div class="obj">4</div>
<div class="obj">5</div>
<div class="obj">6</div>
<div class="obj">7</div>
<div class="obj push"></div>
<div class="obj push"></div>
<div class="pushend"></div>
</div>
CSS
#container
{
max-width: 980px;
background-color: lavender;
display: inline-block;
text-align: justify;
}
.obj
{
width: 180px;
height: 180px;
background-color: lightgreen;
display: inline-block;
margin-bottom: 20px;
}
.obj.push {
height: 0px
}
.pushend {
width: 100%;
height: 0px;
display: inline-block;
}
demo

css - padding propagating to next column

I have this basic HTML structure:
<div id="left-column"></div>
<div id="content"></div>
<div id="right-column"></div>
And this css:
#left-column
{
padding-top: 25px;
width: 80px;
background: url('../../common/images/black70.png');
height: 100%;
float: left;
margin-right: 5px;
}
#content
{
padding: 5px;
}
#right-column
{
padding-top: 25px;
width: 190px;
background: url('../../common/images/black40.png');
height: 100%;
float: right;
}
The problem is content padding is being propagated to right column:
How can I avoid this?
Thanks
The problem is your #right-column is after #content so in the document flow, it will start after the content, which has 10px height from its top and bottom padding.
If you re-order your HTML like so, it fixes your issue.
<div id="left-column"></div>
<div id="right-column"></div>
<div id="content"></div>
Here's a jsfiddle
If you've got floating things and non-floating things, the floating things should always come before the non-floating ones in the source.
In your case, the content is rendered first, and then the right-column below that.

2 columns div for ie8 and ie7

i want a row with 2 cells, the row and the 2 cellsmust be in percent.
i had tried to do it like this:
#container
{
width: 100%;
display:inline-table;
}
#sidebar1
{
float: left;
width: 30%;
}
#mainContent
{
float: left;
width: 70%;
}
<div id="container">
<div id="sidebar1">
<telerik:RadFileExplorer ID="RadFileExplorer1" runat="server" EnableCreateNewFolder="False"
EnableOpenFile="False" Skin="WebBlue" Width="" ExplorerMode="FileTree" TreePaneWidth="">
<Configuration SearchPatterns="*.*" ViewPaths="/" />
</telerik:RadFileExplorer>
</div>
<div id="mainContent">
<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" Skin="WebBlue" />
</div>
</div>
but it doesnt display correctl, when i resize, the right columns go down the first column.
Because of the way the CSS box model works, having 2 boxes next to each other whose combined width adds up to 100% (or the width of its container) will cause one box to be pushed down below the other in many cases.
You should make sure you have no margins, padding, or borders on your two column elements. This will be added to the width of your elements, in addition to the 70%/30% widths you have for your columns.
UPDATE: As ricebowl mentioned in the comments, many browsers have rounding errors that result in more than 100% width being rendered. This can be seen on PositionIsEverything's demo page. I've updated my answer to take this into consideration.
I've also updated my solution to include a fix for the column overflow problem you mentioned. Now, if the content is too large for the columns, a scrollbar will appear. If you don't want scrollbars, you can use overflow-x: hidden instead, however this will cut content off.
/* Reset all margin, padding, border to baseline */
#container, #sidebar1, #mainContent {
margin: 0px;
padding: 0px;
border: 0px;
}
/* Apply styles for column layout */
#container {
width: 100%;
overflow: auto
}
#sidebar1 {
width: 29%;
float: left;
overflow-x: auto
}
#mainContent {
width: 69%;
float: right;
overflow-x: auto
}
A live demo of this can be seen here: http://jsbin.com/eyise
I just tested this out in Firefox 3.5, IE7, IE8, & Google Chrome 3. It works identically in all three browsers. I would avoid using display: inline-table;. In my experience, I've never had very much luck using it consistently across all browsers.
If you need to add a border, do so using faux columns on the container element. And since you're obviously doing a liquid layout, you may also want to read this article on liquid faux columns.
<style type="text/css">
#main {
margin: auto;padding: 0px;width: 100%;}
#mainleft {
float: left; width: 100%; clear: none; background-color: #993300;
}
#mainright {
float: right;
width: 30%;
clear: none;
position: relative;
background-color: #0033FF;
}
</style>
</head>
<body>
<div id="main"><div id="mainleft"><div id="mainright"></div>
</div>
</div>
</body>
Make the mainContent 69%
And remove the container display.
I have just copied and paste this code from a project I am working on, should help
#main {
margin: auto;
padding: 0px;
width: 100%;
}
#mainleft {
float: left; width: 70%; clear: none;
}
#mainright {
float: right; width: 30%; clear: none; position: relative;
}
Just to extend the possibilities for others who get here: use display: table-cell;, works pretty well and has no rounding errors. Example:
HTML
<div class="table">
<div class="cell">textytext</div>
<div class="cell">textytext</div>
</div>
CSS
.table {
display: table;
width: 100%;
}
.table .cell {
display: table-cell;
text-align: left;
vertical-align: top;
}
jsfiddle
To add some spacing you can apply left-/right-padding to the cell, it won't interfere with the layout, plus you can easily make it a 3-column text by adding a cell. This only works for IE8, since the thread is about IE8 and 7, but I think it's a really nice solution.

Resources