修改 css 就可实现 位置调换 ,如下:
优点 : 交换 <div class="sidebar">固定</div> 、 <div class="main">自适应</div>顺序 ,实现主要内容优先加载渲染。
缺点 :absolute 定位,脱离文档流,当 sidebar 列的高度,超过 main 列的高度,会遮住下面的元素。需要给父盒子设置 overflow 属性。
也支持位置调换:
缺点 :不能实现主要内容优先加载渲染。
位置调换:
这里有一点需要注意: .sidebar 没有设置高度,会和 .container 保持一样的高度。 .container 的高度是被 .main 撑开的,也就是和 .main 高度一样。
位置调换:
这里 .main 和 .sidebar 高度不单独设置的话,也是同样的高度。
位置调换:
位置调换:
这里让 .main 成为 BFC 主要是消除 .sidebar 因 float 带来的影响,只要能让 .main 成为 BFC 就行。
此外留给大家一个思考题,还有没有其他方式呢?
手机浏览器一般都不支持float
浮动,所以可以用
display:
inline-block
来代替它,当然你也可以用table来布局。用两个
display:inline-block
的块元素,并设定它们的宽度:width:49%
就可以实现一栏两列的布局了。
以下方式供参考:
html代码:
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title>练习使用HTML</title>
<link rel="stylesheet" href="css/index.css" />
</head>
<body>
<div id="d1">
<span>DIV</span>
</div>
<div id="d2">
<span>DIV</span>
</div>
<div id="d3">
<span>DIV</span>
</div>
</body>
</html>
css代码:
#d1{position: absolute
width: 100px
height: 100px
background-color: red
}
#d2{
position: absolute
margin-left: 100px
width: 500px
height: 100px
background-color: blue
}
#d3{
position: absolute
margin-top: 100px
width: 600px
height: 100px
background-color: yellow
}
效果: