在面试中咱们常常会被问到一个简单的问题,那就是"实现一个三栏布局,左右固定,中间自适应"。这个问题就是考察知识点就是页面布局,咱们是实际的项目开发中,也常常会遇到这个问题。其实我发现一个前端开发人员有一个通病,包括我本身也是,"以为CSS不是很重要,不须要深刻的学习",这实际上是一个比较糟心的想法。在项目中常常出现错乱的布局,可是并不知道缘由。因此咱们今天来简单总结一下有那些方式能够实现三栏布局。css
<template>
<div class="box">
<div class="left">1</div>
<div class="right">3</div>
<div class="content">2</div>
</div>
</template>
<style type="text/css">
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
.box > div {
min-height: 100px;
}
.left {
float: left;
width: 100px;
background: red;
}
.content {
background: yellow;
}
.right {
float: right;
width: 100px;
background: blue;
}
</style>
复制代码
这种布局方式是利用浮动脱离文档流实现的,这里须要特别注意一下,left、right都在content前面布局,若是按照正常的顺序left-content-right,中间元素造成定位,那么就会将right顶下去。没法实现三栏布局。 前端
浮动元素特征:脱离文档流,容许文本和内联元素环绕它。该元素从网页的正常流动(文档流)中移除,尽管仍然保持部分的流动性。 也就是说,若是一个父盒子中有两个子元素,其中一个子元素浮动,若另外一个元素为块级元素,则会无视浮动元素, 被浮动元素覆盖;若另外一个元素为内联元素,则会环绕浮动元素。 面试
<template>
<div class="box">
<div class="left">1</div>
<div class="content">2</div>
<div class="right">3</div>
</div>
</template>
<style type="text/css">
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
.box {
position: relative;
}
.box > div {
min-height: 100px;
position: absolute;
}
.left {
left: 0px;
width: 100px;
background: red;
}
.content {
left: 100px;
right: 100px;
background: yellow;
}
.right {
right: 0px;
width: 100px;
background: blue;
}
</style>
复制代码
<template>
<div class="box">
<div class="left">1</div>
<div class="content">2</div>
<div class="right">3</div>
</div>
</template>
<style type="text/css">
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
.box {
display: flex;
}
.box > div {
min-height: 100px;
}
.left {
width: 100px;
background: red;
}
.content {
flex: 1;
background: yellow;
}
.right {
width: 100px;
background: blue;
}
</style>
复制代码
<template>
<div class="box">
<div class="left">1</div>
<div class="content">2</div>
<div class="right">3</div>
</div>
</template>
<style type="text/css">
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
.box {
display: table;
width: 100%;
min-height: 100px;
}
.box > div {
display: table-cell;
}
.left {
width: 100px;
background: red;
}
.content {
background: yellow;
}
.right {
width: 100px;
background: blue;
}
</style>
复制代码
<template>
<div class="box">
<div class="left">1</div>
<div class="content">2</div>
<div class="right">3</div>
</div>
</template>
<style type="text/css">
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
.box {
display: grid;
grid-template-columns: 100px 1fr 100px;
}
.box > div {
min-height: 100px;
}
.left {
background: red;
}
.content {
background: yellow;
}
.right {
background: blue;
}
</style>
复制代码
<template>
<div class="box">
<div class="left">1</div>
<div class="content">2</div>
<div class="right">3</div>
</div>
</template>
<style type="text/css">
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
.box > div {
min-height: 100px;
}
.left {
float: left;
width: 100px;
background: red;
position: relative;
z-index: 9999;
}
.content {
float: left;
width: 100%;
margin-left: -100px;
background: yellow;
padding: 0 100px;
/* z-index: 1; */
position: relative;
}
.right {
float: left;
width: 100px;
margin-left: -100px;
background: blue;
z-index: 1;
position: relative;
}
</style>
复制代码