Elton's Blog

DIV+CSS侧边栏自适应高度

by on 八.09, 2009, under Web

1
2
3
4
5
6
7
8
9
10
<div id="container">
  <div id="sidebar1">
    <!-- end #sidebar1 -->
  </div>
 
  <div id="mainContent">
  </div>
 
  <!-- end #container -->
</div>

如上面这个比较经典的两栏布局。 如果sidebar1使用了背景平铺图片或者背景颜色,这时候,如果如果另一侧的mainContent比sidebar1高的话,就会出现sidebar1上半部分是之前指定sidebar1的背景,而下面则是这个container的背景。也就是说sidebar1定义的背景并没有自动随着页面高度的增加而自动延伸下来。

即使你把sidebar1的高度设置成100%也是没有效果的。

解决办法很简单,因为另一侧的mainContent的内容增加了,高度变高了,会自动导致包含它的container的高度也自然增加,所以如果把sidebar1的背景设置在container中就解决了。

如:

1
background: #fff url(../images/left_bg.jpg) repeat-y;




:, ,

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...