美文网首页
css各种布局和居中

css各种布局和居中

作者: 易景平 | 来源:发表于2020-01-10 17:46 被阅读0次

1.左右布局:一栏定宽,一栏自适应。

方法:1.float + margin

<div class="right">自适应</div>
/*下面是css部分*/
.left{
  width: 200px;
  background: pink;
  float: left;
  display: block;
  text-align: center;
  line-height: 600px; 
}
.right{
  margin-left: 210px;
  background: skyblue;
  text-align: center;
  line-height: 600px;
}

方法2:使用position:absolute

<div class="right">自适应</div>
/*下面是css部分*/

.left{
  width: 200px;
  background: pink;
  position: absolute;
  top: 9px;
  display: block;
  text-align: center;
  line-height: 400px;
  
}
.right{
  margin-left: 210px;
  display: block;
  background: skyblue;
  text-align: center;
  line-height: 400px;
}
左右布局.png

2.左中右布局:两边定宽,中间自适应。

方法1:左右float,中间用margin撑开。缺点是HTML文档里左右两块写在前,中间块写在后。

<div class="right">右栏</div>
<div class="middle">中间栏</div>

.left{
  width: 200px;
  line-height: 400px;
  text-align: center;
  background: skyblue; 
  float: left;    
}
.right{
  width: 150px;
  line-height: 400px;
  text-align: center;
  background: pink;
  float: right;
}
.middle{
  line-height: 400px;
  text-align: center; 
  background: lightgreen; 
  margin-left: 220px; 
  margin-right: 160px;
}

方法2:左右使用position定位,中间用margin撑开

<div class="middle">中间栏</div>
<div class="right">右栏</div>

* {margin: 0;text-align: center;}
.left{
  width: 150px;
  line-height: 400px;
  background: skyblue; 
  position: absolute;   
  top: 0;
  left: 0;
}
.middle{
  line-height: 400px;
  background: lightgreen; 
  margin: 0 160px;
}
.right{
  width: 150px;
  line-height: 400px;
  background: pink;
  position: absolute;
  top: 0;
  right: 0;
}
左中右布局.png

方法3:flex布局

<div class="middle">中间</div>
<div class="right">右栏</div>
    
 .wrapper{
    display: flex;
    text-align: center;
}
.left{
    width: 200px;
    line-height: 300px;
    background: lightgreen;
}
.middle{
    width: 100%;
    background: pink;
    marign: 0 20px;
}
.right{
    width: 200px;
    line-height: 300px;
    background: skyblue;
}  
三栏flex布局.png

3.水平居中

1.行内元素水平居中,一个属性搞定

.center{
        text-align:center;
}

2.块级元素固定宽度,左右margin设为auto

    width: 600px;
    margin-left: auto;
    margin-right: auto;
}

3.居中的元素设置display:table;然后设置margin: 0 auto.这个和上方法2方法有点类似。

      display:table;
      margin:0 auto;
      border:1px solid;
  }
  <div class="t">利用table水平居中</div>

4.多个块级元素设置inline-block,其父元素设置text-align,这个利用分方法1中的特性

      text-align:center;
  }
  .inner{
      display:inline-block;
  }
  <div class="wrap">
      <div class="inner">内部的块元素变成inline-block</div>
      <div class="inner">内部的块元素变成inline-block</div>
  </div>

5.使用flex布局。父元素上设置display: flex;justify-content: center

      display:flex;
      justify-content:center;
  }
  <div class="father">
      <div class="son">使用flex布局居中</div>
      <div class="son">使用flex布局居中</div>
  </div>

4.垂直居中

1.单行文本垂直居中

方法1:设置padding-top和padding-bottom值相等
方法2:设置line-height的值等于height

2.多行文本垂直居中,使用display:table ,table-cell和vertical-align: middle

    <span>
      这里是第一行文字。<br>
      这里是第二行文字。
    </span>
</div>
  
div{
  display:table; 
  width:550px; 
  height:300px; 
}
span{
  display:table-cell; 
  vertical-align:middle;
}

3.块级元素flex垂直居中。利用flex布局中的align-items:center

    <div>块级元素需要垂直居中</div>
</div>
  
.wrap{
  display:flex; 
  align-items: center;
  width:550px; 
  height:300px; 
  border:4px solid #beceeb; 
  color:#069; 
}

4.使用position+top以及负margin(或者transform或者margin:auto)居中,可以同时在水平方向也居中。

这里包含3种比较相似的方法。

    <div class="son">这个是要居中的块,长宽已知</div>
</div>
/*以下部分是css*/
.parent {
  width: 400px;
  height: 400px;
  position: relative; //也可以是absolute或fixed
  border: 2px solid;
}
.son {
  width:120px;
  height: 120px;
  border: 2px dashed;
  position: absolute;
  //方法1.负margin使用需要知道居中元素的尺寸,这里垂直和水平都居中
  top: 50%;
  left: 50%;
  margin-left: -60px; 
  margin-top: -60px; 
  //方法2.使用transform,不需要知道居中元素的尺寸,这里垂直和水平都居中
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  //方法3:可以对固定尺寸或图片这种自身包含尺寸的元素使用,这里垂直和水平都居中
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
}

相关文章

  • css各种布局和居中

    1.左右布局:一栏定宽,一栏自适应。 方法:1.float + margin 方法2:使用position:abs...

  • css布局:各种居中

    1. margin设为auto 此方法只能进行水平居中,且对浮动元素或绝对定位元素无效。 2. 使用text-al...

  • CSS布局

    HTML CSS + DIV实现整体布局必备知识利用HTML和CSS实现常见的布局 单列布局 css 实现竖直居中...

  • 页面布局居中问题

    css页面布局水平垂直居中问题 居中问题

  • html编程技巧

    字体外部描边 Css 基于flex布局的盒子上下居中 Css 基于flex布局的盒子左右居中 Css 基于flex...

  • CSS常用布局实现

    该系列用于整理记录常用的CSS布局实现。 CSS常用布局实现01-水平居中 CSS常用布局实现02-垂直居中 CS...

  • CSS布局(不完全)总结

    CSS布局(不完全)总结 实现水平居中布局的几种方式 方法一: 通过以下CSS代码实现水平居中布局 方法二: 通过...

  • CSS布局&CSS居中&媒体查询

    关于CSS布局&CSS居中&媒体查询三者的见解 css布局: 单栏布局: 分为最大宽度和固定宽度,80%以上的页面...

  • CSS各种居中实现方式

    原文地址:CSS各种居中实现方式 CSS居中是每次布局都需要面对的问题,但是同一个居中方法并不是任何元素都能使用的...

  • web前端教程:CSS 布局十八般武艺都在这里了

    CSS布局 布局是CSS中一个重要部分,本文总结了CSS布局中的常用技巧,包括常用的水平居中、垂直居中方法,以及单...

网友评论

      本文标题:css各种布局和居中

      本文链接:https://www.haomeiwen.com/subject/vprmactx.html