百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>CSS教程> Css实现左栏固定宽度,右栏自适应宽度实例
分享文章到:

Css实现左栏固定宽度,右栏自适应宽度实例

发布时间:01/15 来源: 浏览: 关键词:
本文章来给大家详细介绍关于Css实现左栏固定宽度,右栏自适应宽度实例,有需要了解的朋友可进入参考。

 代码如下

    <!doctype html>
    <html lang="zh-CN">
    <head>
            <meta charset="UTF-8">
            <title>左栏固定宽度,右栏自适应之绝对定位法</title>
            <style type="text/css">
            body{
                    margin: 0;
            }
            #nav{
                    top: 0;
                    left: 0;
                    width: 230px;
                    height: 600px;
                    background: #ccc;
                    position: absolute;
            }
            #main{
                    height: 600px;
                    margin-left: 230px;
                    background: #0099ff;
            }
            </style>
    </head>
    <body>
            <div id="container">
                    <div id="nav">
                            左栏
                    </div>
                    <div id="main">
                            右栏
                    </div>
            </div>
    </body>
    </html>

看起来很美好,但是。。

由于左栏使用绝对定位,脱离了文档流,因此有一个缺陷,即当左栏高度大于右栏时,无法将container撑开,这个缺陷单单只看两栏布局并没有太大影响,但如果两栏布局下面有一个底栏,就会出现底栏与左栏重叠的情况:

 代码如下

 

    <!doctype html>
    <html lang="zh-CN">
    <head>
            <meta charset="UTF-8">
            <title>左栏固定宽度,右栏自适应之绝对定位法</title>
            <style type="text/css">
            body{
                    margin: 0;
            }
            #nav{
                    top: 0;
                    left: 0;
                    width: 230px;
                    height: 600px;
                    background: #ccc;
                    position: absolute;
            }
            #main{
                    height: 400px;
                    margin-left: 230px;
                    background: #0099ff;
            }
            #footer{
                    text-align: center;
                    background: #009000;
            }
            </style>
    </head>
    <body>
            <div id="container">
                    <div id="nav">
                            左栏
                    </div>
                    <div id="main">
                            右栏
                    </div>
            </div>
            <div id="footer">
                    底栏
            </div>
    </body>
    </html>

我们再来看看第二种方法,左栏固定宽度,右栏自适应之负margin法:

 代码如下

 

    <!doctype html>
    <html lang="zh-CN">
    <head>
            <meta charset="UTF-8">
            <title>左栏固定宽度,右栏自适应之负margin法</title>
            <style type="text/css">
            body{
                    margin: 0;
            }
            #container{
                    margin-left: 230px;
                    _zoom: 1;
                    /*兼容IE6下左栏消失问题*/
            }
            #nav{
                    float: left;
                    width: 230px;
                    height: 600px;
                    background: #ccc;
                    margin-left: -230px;
                    position: relative;
                    /*兼容IE6下左栏消失问题,IE6真不让人省心啊>_<*/
            }
            #main{
                    height: 600px;
                    background: #0099ff;
            }
            </style>
    </head>
    <body>
            <div id="container">
                    <div id="nav">
                            左栏
                    </div>
                    <div id="main">
                            右栏
                    </div>
            </div>
    </body>
    </html>

这样无论两栏的高度如何变化都不会有问题了:

 代码如下

 

    <!doctype html>
    <html lang="zh-CN">
    <head>
            <meta charset="UTF-8">
            <title>左栏固定宽度,右栏自适应之负margin法</title>
            <style type="text/css">
            body{
                    margin: 0;
            }
            #container{
                    margin-left: 230px;
                    _zoom: 1;
                    /*兼容IE6下左栏消失问题*/
            }
            #nav{
                    float: left;
                    width: 230px;
                    height: 600px;
                    background: #ccc;
                    margin-left: -230px;
                    position: relative;
                    /*兼容IE6下左栏消失问题,IE6真不让人省心啊>_<*/
            }
            #main{
                    height: 400px;
                    background: #0099ff;
            }
            #footer{
                    clear: both;
                    text-align: center;
                    background: #009000;
            }
            </style>
    </head>
    <body>
            <div id="container">
                    <div id="nav">
                            左栏
                    </div>
                    <div id="main">
                            右栏
                    </div>
            </div>
            <div id="footer">
                    底栏
            </div>
    </body>
    </html>

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有4人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板