• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
00fairy00の学习园地
博客园    首页    新随笔    联系   管理    订阅  订阅
NO.24 关于左侧div固定宽度,右侧自适应时footer不能在最底部的问题

当用position:absolute;来完成左边div固定宽度,右边div百分百自适应的时候,如果左边div的高度比右边div的高,就会出现footer被挤上来

这是因为position:absolute;是不占位的,所以footer是贴着右边div的下边的,如下图:

我用了2种方法来解决此问题:

1.用js来控制

   首先用js获取左边div的高度,然后设定content为此高度,footer就自觉的下去啦

 

代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<style>
*
{
    padding
:0px;
    margin
:0px;
}
body
{
    color
:#FFF;
    font-size
:24px;
    text-align
:center;
}
.container
{
    padding-left
:120px;
}
.left
{
    width
:120px;
    position
:absolute;
    left
:0px;
    background
:#F00;
}
.right
{
    width
:100%;
    height
:300px;
    background
:#36F;
}
.foot
{
    width
:100%;
    height
:30px;
    background
:#000;
}
</style>

<script>
$(
function(){
    
var left_height = $("#left").height();
    alert(left_height)
    $(
"#content").height(left_height);
})
</script>
</head>

<body>
<div class="container" id="content">
    
<div class="left" id="left">
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
        右边
<br />
    
</div>
    
<div class="right">左边</div>
</div>
<div class="foot">footer</div>
</body>
</html>

 

2.用css来实现

 不设置左边的div为position为absolute,由于container设置了一个padding-left,所以这里将左边div设置负的margin-left就可以了

 

代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
*
{
    padding
:0px;
    margin
:0px;
}
body
{
    color
:#FFF;
    font-size
:24px;
    text-align
:center;
}
.left
{
    width
:120px;
    height
:1200px;
    float
:left;
    margin-left
:-130px;
    _margin-left
:-65px;
    background
:#F00;
}
.right
{
    width
:100%;
    height
:300px;
    background
:#36F;
}
.container
{
    padding-left
:130px;
}
.foot
{
    width
:100%;
    height
:30px;
    background
:#000;
}
</style>
</head>

<body>
<div class="container">
    
<div class="left">左</div>
    
<div class="right">右</div>
    
<div style="clear:both"></div>
</div>
<div class="foot">footer</div>
</body>
</html>

 

 

  

posted on 2009-12-23 10:31  00fairy00  阅读(1877)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3