视觉艺术

css实现三栏布局的多种方式

2020-03-28  本文已影响0人  halapro_liu

前言

在PC端系统中,三栏布局是很常见的。那么有哪些方式来实现三栏布局呢。

flex布局

<div class="box">
    <div class="left"></div>
    <div class="center"></div>
    <div class="right"></div>
</div>
.box {
  width: 100%;
  height: 100px;
  line-height: 100px;
  text-align: center;
  display: flex;
}

.left {
  width: 200px;
  background-color: pink;
}

.center {
  flex: 1;
  background-color: orange;
}

.right {
  width: 200px;
  background-color: skyblue;
}

float布局

这里有个主意点,right的div必须在center前,不然右边会塌陷。

<div class="box">
    <div class="left"></div>
    <div class="right"></div>
    <div class="center"></div>
</div>
.box {
  width: 100%;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.left {
  float: left;
  background: red;
}

.center {
  background: orange;
}

.right {
  float: right;
  background: skyblue;
}

同时要注意div的特点

postion布局

<div class="box">
    <div class="left"></div>
    <div class="right"></div>
    <div class="center"></div>
</div>
.box {
  width: 100%;
  height: 100px;
  line-height: 100px;
  text-align: center;
  position: relative;
}

.left {
  position: absolute;
  left: 0;
  width: 200px;
  background-color: pink;
}

.center {
  background-color: orange;
}

.right {
  position: absolute;
  right: 0;
  width: 200px;
  background-color: skyblue;
}

table布局

<div class="box">
    <div class="left"></div>
    <div class="center"></div>
    <div class="right"></div>
</div>
.box {
  width: 100%;
  height: 100px;
  line-height: 100px;
  text-align: center;
  display: table;
}

.left {
  display: table-cell;
  background: pink;
  width: 400px;
  height: 100%;
}

.center {
  display: table-cell;
  background: orange;
  height: 100%;
}

.right {
  display: table-cell;
  background: skyblue;
  width: 400px;
  height: 100%;
}

网格布局

<div class="box">
    <div class="left"></div>
    <div class="center"></div>
    <div class="right"></div>
</div>
.box {
  width: 100%;
  height: 100px;
  line-height: 100px;
  text-align: center;
  display: grid;
  grid-template-columns: 200px auto 200px;
}

.left {
  background: pink;
}

.center {
  background: orange;
}

.right {
  background: skyblue;
}
上一篇 下一篇

猜你喜欢

热点阅读