开发者

CSS: two colums (fixed/fluid) - same height [duplicate]

This question already has answers here: How do I keep two side-by-side div elements the sam开发者_StackOverflowe height? (24 answers) Closed last month.

How can I do this:

  • left column, fixed width, background say yellow
  • main column, rest of the screen

main column will contain text of indefinite length, and I want the left column to be as high as the main no matter what the contents are. No header, no footer, just the screen split in two but the left must fill up all the space the main takes. No JavaScript.

Can this (very basic layout) be done without tricks like background images or absurd margin values? I'm trying to move on from the despised table approach, but so far I'm seeing that CSS only layouts always require some trickery even to do the easiest things.


I usually have two divs: the outer one would contain the sidebar content that I want to have the same height as the main column, and the inner div would be the main column.

-----------------------------
|      -------------------- |
|      |                  | |
|      |                  | |
|      |                  | |
|      -------------------- |
-----------------------------

This way, the inner/main div forces the outer/sidebar div to be the same height as the inner/main. Just float:right the inner/main div, set the outer/sidebar div to be width:100%, and specify that the inner/main div has a left margin, to allow for the sidebar, e.g. margin-left:20%.

<div class="outer">
  <div class="inner">
    --main content--
  </div>
  --sidebar content--
</div>

Update: sorry, I had the sidebar content shown in the wrong place. It should be below the inner/main div so that the inner/main div shows up alongside it to the right when it is floated. Example CSS:

div.outer {
  width: 100%;
}

div.inner {
  margin-left: 20%; /* width of sidebar */
  float: right;
}


Here's a QUICK mockup. The main column fills up horizontally as you use it. You could set the main column width if you know the left column width already.

<html>
<head>
<style>
#leftCol
{
width:350px;
height:100%;
float:left;
background-color:#FFFF33;
}
#mainCol
{
width:53%;
height:100%;
float:left;
background-color:#CC0033;
}
#wrapper
{
width:100%;
}
.clearFix
{
clear:both;
}
</style>
</head>

<body>
<div id="wrapper">
<div id="leftCol">tfgsrrtgrt

</div>
<div id="mainCol">gtrgdrtgthdyhtyhtyhydthtr

</div>
<div class="clearFix"></div>
</div>
</body>
</html>

Hope this works for you.


So basically the answer to the question "can this be done without tricks" is: No.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜