常见布局:左栏固定右栏自适应

这节我们要实现的目的只有一个,就是一栏固定一栏自适应。

1、使用div,这样就可以自动填满父标签宽度,设想方案如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo</title>
    <style>
    body{padding: 0;margin:0;}
        #wrap{
            overflow: hidden;*zoom: 1;
        }
        #sitebar{background: #D7B9FF; float: left; width: 300px;}
        #content{
            background: #36CEC5;margin-left:300px;
        }
    </style>
</head>
<body>
    <div id="wrap">
         <div id="sitebar">我是固定左栏</div>
        <div id="content">
            我是右栏内容
        </div>
    </div>
</body>
</html>

但这个有个限制,就是sidebar必须在content之前!

2、遵循网页优化的角度,主要内容应该放前面,那么需要把content和sitebar位置换一下,然后固定蓝使用绝对定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo</title>
    <style>
    body{padding: 0;margin:0;}
        #wrap{
            *zoom: 1; position: relative;
        }
        #sitebar{background: #D7B9FF; width: 300px;position: absolute;left:0;top: 0;}
        #content{
            background: #36CEC5;margin-left: 300px;
        }
    </style>
</head>
<body>
    <div id="wrap">
        <div id="content">
            我是右栏内容
        </div>
         <div id="sitebar">我是固定左栏</div>    

    </div>

</body>
</html>

但是这样也有一个问题,因为左栏使用了绝对定位,那么再有其他层的话就会被它挡住。

3、加多一个层,使用浮动和margin结合

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo</title>
    <style>
    body{padding: 0;margin:0;}

        #sitebar{background: #D7B9FF; width: 300px;float: left;}
        #content{
            background: #36CEC5;float: right;width: 100%;margin-right: -300px;
        }
            #content_wrap{
                margin-right: 300px;
            }
    </style>
</head>
<body>
    <div id="wrap">
       <div id="content_wrap">
            <div id="content">
            我是内容
        </div>
         <div id="sitebar">我是固定左栏</div>
       </div>

    </div>

</body>

总结该布局应具备条件:

1、content自适应,sidebar固定

2、content在sidebar之前

3、后面的元素不受影响

时间: 2024-08-25 10:49:34

常见布局:左栏固定右栏自适应的相关文章

实现一个三列布局(左右侧固定,中间自适应宽度)

html代码(第一二种方法): <div class="left">左侧固定区</div> <div class="right">右侧固定区</div> <div class="mid">中间自适应区</div> css代码: 第一种方法(定位): .left { position: absolute; top: 0; left: 0; width: 100px; heigh

margin+absolute布局:右栏固定主内容自适应 demo

margin+absolute布局:右栏固定主内容自适应 demo 头部 Aside侧边栏区域 Main主内容区域 底部 #demo{width:80%;margin:auto;height:300px;} #hd2,#ft2{height:50px;background-color:#aaa;} #bd2{position:relative;margin:10px 0;} #aside2{position:absolute;top:0;right:0;width:200px;backgroun

网页上中下三分布局,上下固定,中间自适应

<!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> <!-- 禁止浏览器从本地缓存中调阅页面.--> <

页面布局方案-上固定,下自适应

上固定,下自适应 两行布局,上固定,下自适应 效果: 代码: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>上边固定,下面自适应</title> 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 6 <style type="text/css&

CSS布局——左定宽度右自适应宽度并且等高布局

今天有位朋友一早从妙味课堂转来一个有关于CSS布局的面试题,需要解决,花了点时间写了几个DEMO,放上来与大家分享受.那么我们在看DEMO之前一起先来看看这个面试题的具体要求吧: 左侧固定宽,右侧自适应屏幕宽: 左右两列,等高布局: 左右两列要求有最小高度,例如:200px;(当内容超出200时,会自动以等高的方式增高) 要求不用JS或CSS行为实现: 仔细分析试题要求,要达到效果其实也并不是太难,只是给人感觉像有点蛋疼的问题一样.但是你仔细看后你会觉得不是那么回事: 左边固定,右边自适应布局,

布局:左宽度固定,右边自适应

需求: 左侧固定宽,右侧自适应屏幕宽: 左右两列,等高布局: 左右两列要求有最小高度,例如:200px;(当内容超出200时,会自动以等高的方式增高) 方法1:浮动布局 这种方法我采用的是左边浮动,右边加上一个margin-left值,让他实现左边固定,右边自适应的布局效果 这种实现方法最关键之处就是自适应宽度一栏"div#content"的"margin-left"值要等于固定宽度一栏的宽度值 <!DOCTYPE html> <html lang

网页html结构右侧栏固定,左侧自适应大小。

最近写了一个项目,写页面的结构,html树形结构是有header,container,footer部分,其中container部分是右侧栏是固定宽度,左侧是自适应宽度与屏幕高度. 第一次写的博客文章是container部分是左侧栏固定,右侧是自适应效果.左侧栏固定是很好写,但右侧栏固定却不很好写,以下是基本的结构与样式. <div class="container" style="overflow:hidden;"> <div class=&quo

css三栏布局:左右固定宽中间自适应

圣杯布局 直接上代码:html: <div class="container> <div class="middle"></div> <div class="left"></div> <div class="right"></div> </div> css: .middle,.left,.right {float: left; positio

两列布局,左边固定,右边自适应的三种方法

<!-- 方法一:使用CSS3属性calc()进行计算.注意:calc()里的运算符两边必须有空格 --> <div> <div style="float:left;width:200px;height:500px;background-color:blue;"> </div> <div style="float:left;width:calc(100% - 200px);height:500px;background-c