Grit的前端之路

常见CSS布局

2019-01-07  本文已影响0人  Grit0821

- 左右布局

左右布局.png

方法1:利用float实现左右布局

  1. 给所有子元素加 float: left
  2. 给父元素加 clearfix 类,清除浮动影响
<body>
  <div class="wrap clearfix">
    <div class="left-box">
     左边
    </div>
    <div class="right-box">
     右边
    </div>
  </div>
</body>
body{
  margin: 0;
}
.clearfix::after{
  content: "";
  display: block;
  clear: both;
}
.wrap{
  width: 1000px;
  margin: 50px auto 0;
}
.left-box{
  float: left;
  background: #ff9e2c;
  font-size: 30px;
  width: 50%;
  text-align: center;
  height: 300px;
  color: #fff;
  line-height: 300px;
}
.right-box{
  float: left;
  background: #b6701e;
  font-size: 30px;
  width: 50%;
  text-align: center;
  height: 300px;
  color: #fff;
  line-height: 300px;
}

方法2:使用绝对定位实现

  1. 子元素绝对定位 position:absolute;
  2. 父元素相对定位 position:relative;
body{
  margin: 0;
}
.wrap{
  position: relative;
  width: 1000px;
  margin: 50px auto 0;
}
.wrap div{
  position: absolute;
  font-size: 30px;
  width: 50%;
  text-align: center;
  height: 300px;
  color: #fff;
  line-height: 300px;
}
.left-box{ 
  top: 0;
  left: 0;
  background: #ff9e2c;
}
.right-box{
  top: 0;
  right:0;
  background: #b6701e;
}

方法3:使用表格样式

  1. 将父容器转换为表,给父容器设置display: table
  2. 通过display: table-cell,将子容器转换为表中的单元格
使用假表格
body{
  margin: 0;
}
.wrap{
  display: table;
  width: 1000px;
  margin: 50px auto 0;
}
.wrap div{
  display: table-cell;
  font-size: 30px;
  width: 50%;
  text-align: center;
  height: 300px;
  color: #fff;
  line-height: 300px;
}
.left-box{
  background: #ff9e2c;
}
.right-box{
  background: #b6701e;
}

方法4:使用inline-block

  1. 给父元素中的子元素添加display: inline-block;
  2. 需要将两个div靠在一起,具体请看下面的代码,因为如果不靠在一起的话,会产生一个空格间隔,导致布局发生改变
body{
  margin: 0;
}
.wrap{
  width: 1000px;
  margin: 50px auto 0;
}
.wrap div{
  display: inline-block;
  font-size: 30px;
  width: 50%;
  text-align: center;
  height: 300px;
  color: #fff;
  line-height: 300px;
}
.left-box{
  background: #ff9e2c;
}
.right-box{
  background: #b6701e;
}

方法5:使用Flexbox

Flexbox是一种非常棒的方式,只需注意它仅限于IE 10及以上版本,可能需要使用前缀和值来获得最佳支持

  1. 将父容器转换为一个灵活的盒子
  2. 子容器占用相同的空间份额。无需设置宽度或高度
  3. 为了确保它们是正确的,设置flex: 1;在两侧是一个很好的计划。
body{
    margin: 0;
}
.wrap{
    display: flex;
    width: 1000px;
    margin: 50px auto 0;
}
.wrap div{
    flex: 1;
    font-size: 30px;
    width: 50%;
    text-align: center;
    height: 300px;
    color: #fff;
    line-height: 300px;
}
.left-box{
    background: #ff9e2c;
}
.right-box{
    background: #b6701e;
}

方法6:使用网格布局grid

定义一个容器,然后将其拆分成可以用子元素灵活填充的列和单元格

body{
  margin: 0;
}
.wrap{
  display: grid;
  width: 1000px;
  margin: 50px auto 0;
}
.wrap div{
  font-size: 30px;
  text-align: center;
  height: 300px;
  color: #fff;
  line-height: 300px;
}
.left-box{
  grid-column: 1;
  background: #ff9e2c;
}
.right-box{
  grid-column: 2;
  background: #b6701e;
}

- 左中右布局

上面实现左右布局的方法都可以实现左中右布局,这里就挑3个比较实用的方法


左中右布局

方法一: float

<div class="wrap clearfix">
    <div class="left-box">左边</div>
    <div class="middle-box">中间</div>
    <div class="right-box">右边</div>
</div>
body{
  margin: 0;
}
.clearfix::after{
  content: "";
  display: block;
  clear: both;
}
.wrap{
  width: 1000px;
  margin: 50px auto 0;
}
.wrap div{
  float: left;
  height: 300px;
  font-size: 30px;
  text-align: center;
  color: #fff;
  line-height: 300px;
}
.left-box{
  width: 30%;
  background: #ff9e2c;
}
.middle-box{
  width: 36%;
  background: orange;
  margin: 0 2%;
}
.right-box{
  width: 30%;
  background: #b6701e;
}

方法二:使用Flexbox

body{
    margin: 0;
}
.wrap{
    display: flex;
    width: 1000px;
    margin: 50px auto 0;
}
.wrap div{
    height: 300px;
    font-size: 30px;
    text-align: center;
    color: #fff;
    line-height: 300px;
}
.left-box{
    width: 30%;
    background: #ff9e2c;
}
.middle-box{
    width: 36%;
    background: orange;
    margin: 0 2%;
}
.right-box{
    width: 30%;
    background: #b6701e;
}

方法三:使用网格布局

body{
    margin: 0;
}
.wrap{
    display: grid;
    width: 1000px;
    margin: 50px auto 0;
}
.wrap div{
    height: 300px;
    font-size: 30px;
    text-align: center;
    color: #fff;
    line-height: 300px;
}
.left-box{
    grid-column: 1;
    background: #ff9e2c;
}
.middle-box{
    grid-column: 2;
    background: orange;
    margin: 0 15px;
}
.right-box{
    grid-column: 3;
    background: #b6701e;
}

- 水平居中

1. 内联元素水平居中

在块级父元素内水平居中内联元素

.center-children {
  text-align: center;
}

2. 块级元素水平居中

margin设置左右auto

.center-me {
  margin: 0 auto;
}

3. 多个块级元素水平居中

-垂直居中

1.inline 或 inline-* 元素单行垂直居中

.link {
  padding-top: 30px;
  padding-bottom: 30px;
}
.center-text-trick {
  height: 100px;
  line-height: 100px;
  white-space: nowrap;
/*nowrap文本内的换行无效内容只能在一行显示*/
}

2. 多行文本垂直居中

.center-table {
  display: table;
}
.center-table p {
  display: table-cell;
  vertical-align: middle;
}
.flex-center-vertically {
  display: flex;
  justify-content: center;
  flex-direction: column;
  height: 400px;
}

3. block元素垂直居中

.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  height: 100px;
  margin-top: -50px;
}
.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}
.parent {
  display: flex;
  flex-direction: column;
  justify-content: center;
}
上一篇下一篇

猜你喜欢

热点阅读