0

I'm currently using 1 table to align 2 main portions of the site. It's causing problems for me now, so I'd like to use pure CSS.

I have a 205px wide navbar column on the left. Occupying the rest of the space on the right, I'd like to have a container (So on the right side of the screen, taking up screen width - 200 pixels) This container would not have a fixed height, but I want its top to be aligned with the top of the navbar.

Here's a demo of what I currently have .

I would like the solution to be similar to that, but not use tables, and have the top of the container aligned with the top of the sidebar.

I've made several attempts at doing this (before I started using the table, and after) but none of them even remotely worked. I've come here as a last resort, so I hope that someone could help.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jacob Brunson
  • 1,482
  • 4
  • 23
  • 37
  • That is a very common, very solved problem. [CSS - Equal Height Columns?](http://stackoverflow.com/questions/2114757/css-equal-height-columns) – Madara's Ghost Jul 29 '12 at 16:29
  • @Truth That is not what I am trying to do. – Jacob Brunson Jul 29 '12 at 16:44
  • @Truth I don't want them to be the same size, one of the columns is a fixed size and fixed position, and the right column is allowed to expand farther down. – Jacob Brunson Jul 29 '12 at 16:57
  • So, I've FINALLY come up with a solution that works perfectly cross-browser, doesn't use tables, and doesn't use javascript. (I feel so proud :3) Rather than explaining it, you can view my completed fiddle demo here: http://jsfiddle.net/af9L5/ – Jacob Brunson Jul 30 '12 at 21:01

2 Answers2

1

Fiddle

.container{height: 100%; border: 1px solid #0f0; display: table;}
#sidebar{
    width:40%; height: 100%; display: table-cell; background: #ccc;    
}
#sidebar2{
    width:60%; height: 100%; display: table-cell; background: #f00;    
}
body, html {
    height:100%;
}

HTML

<div class="container">
  <div id="sidebar">links and whatnot go here</div>
  <div id="sidebar2">this is the container (but its top is not aligned with the sidebar as I would like)</div>    
</div>

Note: table-cell property is supported by supports IE8+

EDIT: If you can't use table-cell then you have to use some jquery to calculate height. Check this Fiddle

Dipak
  • 11,930
  • 1
  • 30
  • 31
  • I would still like the sidebar to be fixed on the left and vertically centered, as in my original fiddle. Plus this is nearly the same as using tables (so any peace of mind I gain from not using tables is negated) – Jacob Brunson Jul 29 '12 at 16:47
0

I would do something like this:

. HTML:

<div id="container">
    <aside id="sidebar">Links and whatnot</aside>
    <section id="content">Content and whatnot</section>
</div>

. CSS:

html, body {
    width: 100%;
    height: 100%;
}

div#container {
    height: 100%;
}

aside#sidebar {
    background-color: #f00;
    width: 205px;
    min-height: 100%;
    float: left;
}

section#content {
    background-color: #0f0;
    min-height: 100%;
}

You can see it working in this fiddle.

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
Viktor
  • 487
  • 2
  • 8
  • 26