页面布局
题目:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px,中间自适应

页面布局的变通
两栏布局
- 左边定宽,右边自适应方案:
/*方案1 float + margin */
.left{
width:120px;
float:left;
}
.right{
margin-left:120px;
}
/*方案2 float + calc*/
.left{
width:120px;
float:left;
}
.right{
width:calc(100% - 120px);
float:left;
}
- 左右两边定宽,中间自适应
.wrap {
width: 100%;
height: 200px;
}
/*这里涉及到一个知识点, > 是子类选择器
下面是有关子类选择器的教程
(http://www.w3school.com.cn/css/css_selector_adjacent_sibling.asp)
*/
.wrap > div {
height: 100%;
}
/* 方案1 */
.left {
width: 120px;
float: left;
}
.right {
float: right;
width: 120px;
}
.center {
margin: 0 120px;
}
/* 方案2 */
.left{
width:120px;
float:left;
}
.right{
width:120px;
float:right;
}
.center{
width:calc(100% - 240px);
margin-left:120px;
}
/*方案3*/
.wrap{
display:flex;
}
.left{
width:120px;
}
.right{
width:120px;
}
/*要注意的一点是flex 是 flex-grow、flex-sharink、flex-basis的缩写
具体详细解释( https://blog.csdn.net/aliven1/article/details/78853725 )
*/
.center{
flex:1;
}
- 左右居中
- 行内元素:text-align:center
- 定宽块元素:左右 margin 值为 auto
- 不定宽块状元素:table布局,position + transform
/*方案1*/
.wrap{
text-align:center;
}
.center{
display:inline;
}
/*or*/
/* display:inline-block; */
/*方案2*/
.center{
width:100px;
margin: 0 auto;
}
/*方案3*/
.wrap{
position:relative;
}
.center{
position:absolute;
left:50%;
transform:translateX(-50%); /*元素往左位移自身宽度50%的距离*/
}
- 上下垂直居中:
- 不定高:position + transform、flex、IFC + vertical-align:middle
/*不定高方案1*/
.center{
display:flex;
align-items:center;
}
/*不定高方案2*/
.center{
position:absolute;
top:50%;
transform:translateY(-50%);
}
/*不定高方案3*/
/*设置inline-block 则会在外层产生 IFC,高度设为 100% 撑开 wrap 的高度 */
三栏布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
html *{
padding: 0;
margin: 0;
}
.layout{
margin-top: 20px;
}
.layout article div{
min-height: 100px;
}
</style>
</head>
<body>
<!-- 浮动布局解决方案 -->
<!-- 缺点:需要清除浮动,即处理好与周围元素的关系
优点:有好的兼容性 -->
<section class="layout float">
<style media="screen">
.layout.float .left{
float: left;
width: 300px;
background: red;
}
.layout.float .right{
float: right;
width: 300px;
background:blue;
}
.layout.float .center{
background: yellow;
}
</style>
<article class="left-right-center">
<div class="left"></div>
<div class="right"></div>
<div class="center">
<h1>浮动解决方案</h1>
1. 这是三栏布局中间部分
2. 这是三栏布局中间部分
</div>
</article>
</section>
<!-- 绝对定位解决方案 -->
<!-- 优点:实现简单,不容易出问题
缺点:布局脱离了文档流了,那么所有的子元素也脱离的文档流,可使用性比较差 -->
<section class="layout absolute">
<style>
.layout.absolute .left-center-right>div{
position: absolute;
}
.layout.absolute .left{
left:0;
width: 300px;
background: red;
}
.layout.absolute .center{
left:300px;
right: 300px;
background: yellow;
}
.layout.absolute .right{
right: 0;
width: 300px;
background: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>绝对定位解决方案</h2>
1. 这是三栏布局绝对定位中间部分
2. 这是三栏布局绝对定位中间部分
</div>
<div class="right"></div>
</article>
</section>
<!-- flexbox解决方案 -->
<!-- 优点:弥补了 float布局与绝对布局的缺点,是现在比较流行的布局方式 -->
<section class="layout flexbox">
<style>
.layout.flexbox{
margin-top: 140px;
}
.layout.flexbox .left-center-right{
display: flex;
}
.layout.flexbox .left{
width: 300px;
background: red;
}
.layout.flexbox .center{
flex: 1;
background: yellow;
}
.layout.flexbox .right{
width: 300px;
background: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>flexbox解决方案</h2>
1. 这是三栏布局flexbox中间部分
2. 这是三栏布局flexbox中间部分
</div>
<div class="right"></div>
</article>
</section>
<!-- 表格布局解决方案 -->
<!-- 优点:有好的兼容性,flex布局不兼容时可以考虑 表格布局 -->
<section class="layout table">
<style>
.layout.table .left-center-right{
width: 100%;
display: table;
height: 100px;
}
.layout.table .left-center-right>div{
display: table-cell;
}
.layout.table .left{
width: 300px;
background: red;
}
.layout.table .center{
background: yellow;
}
.layout.table .right{
width: 300px;
background: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>表格布局解决方案</h2>
1. 这是三栏布局表格中间部分
2. 这是三栏布局表格中间部分
</div>
<div class="right"></div>
</article>
</section>
<!-- 网格布局的解决方案 -->
<section class="layout grid">
<style>
.layout.grid .left-center-right{
display: grid;
width: 100%;
grid-template-rows: 100px;
grid-template-columns: 300px auto 300px;
}
.layout.grid .left{
background: red;
}
.layout.grid .center{
background: yellow;
}
.layout.grid .right{
background: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h2>网格布局解决方案</h2>
1. 这是三栏布局网格中间部分
2. 这是三栏布局网格中间部分
</div>
<div class="right"></div>
</article>
</section>
</body>
</html>
三栏布局(练习)
- 左又宽度固定,中间自适应
- 上下高度固定,中间自适应
两栏布局 - 左宽度固定,右自适应
- 右宽度固定,左自适应
- 上高度固定,下自适应
- 下高度固定,上自适应
网友评论