zoukankan      html  css  js  c++  java
  • css常用居中方式

    一、水平居中

    1.内联元素

    父级元素加 text-align: center 即可

    html

    <div class="container">
      <a>内联元素</a>
    </div>
    

    css

    .container {
      text-align: center;
    }
    

    2.块级元素

    给定宽度,然后 margin 上下为 0,左右 auto 即可

    html

    <div class="container">
      块级元素
    </div>
    

    css

    .container {
       200px;
      margin: 0 auto;
    }
    

    3.多个块级元素

    第一种方式

    子元素设置成内联,父级元素加 text-align:center即可

    html

    <div class="container">
      <div>
        第一个块
      </div>
      <div>
        第二个块
      </div>
      <div>
        第三个块
      </div>
    </div>
    

    css

    .container {
      text-align: center;
    }
    .container div {
      display: inline-block;
    }
    

    第二种方式

    利用 flexbox 弹性布局

    html

    <div class="container">
      <div>
        第一个块
      </div>
      <div>
        第二个块
      </div>
      <div>
        第三个块
      </div>
    </div>
    

    css

    .container {
      display: flex;
      justify-content: center;
    }
    

    二、垂直居中

    1.内联元素

    第一种方式

    设置 padding

    html

    <div class="container">
      需要垂直居中的内容(内联)
    </div>
    

    css

    .container {
      padding: 40px 40px;
    }
    

    第二种方式

    按照父级元素的高度,设置子元素的行高

    html

    <div class="container">
      需要垂直居中的内容(内联)
    </div>
    

    css

    .container {
      height: 100px;
      line-height: 100px;
    }
    

    第三种方式

    利用 flexbox,父级元素需给定高度

    html

    <div class="container">
      <a href="">爷要垂直居中</a>
      <a href="">爷要垂直居中</a>
      <a href="">爷要垂直居中</a>
    </div>
    

    css

    .container {
       200px;
      height: 200px;
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    

    2.块级元素

    第一种方式

    父元素相对定位 position:relative,子元素绝对定位 position: absolute

    html

    <div class="container">
      <div>爷要垂直居中</div>
    </div>
    

    css

    .container {
      position: relative;
       200px;
      height: 200px;
    }
    .container div {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
    }
    

    第二种方式

    利用 flexbox

    html

    <div class="container">
      <div>爷要垂直居中</div>
    </div>
    

    css

    .container {
       200px;
      height: 200px;
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    

    三、水平和垂直居中

    第一种方式

    父元素相对定位 position:relative,子元素绝对定位 position: absolute

    html

    <div class="container">
      <div>爷要水平和垂直居中</div>
    </div>
    

    css

    .container {
      position: relative;
       200px;
      height: 200px;
    }
    .container div {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: red;
    }
    

    第二种方式

    使用 flexbox

    html

    <div class="container">
      <div>我要垂直居中啊a我要垂直居中啊a我要垂直居中啊</div>
    </div>
    

    css

    .container {
       200px;
      height: 200px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .container div {
       100px;
      height: 100px;
    }
    

    lalala

    最后说一点,如果具体宽高已知,给定具体数值就可以直接实现

    The_End

  • 相关阅读:
    python连接SMTP的TLS(587端口)发邮件python发邮件(带认证,587端口)202010
    JAVA抓取通过JS渲染的网站(动态)网页数据
    moviepy音视频剪辑:与大小相关的视频变换函数详解
    区块链知识博文1: 共识算法之争(PBFT,Raft,PoW,PoS,DPoS,Ripple)
    MoviePy v2.0.0.dev1尚不成熟,不建议大家使用
    区块链学习2:一些概念性的基础知识笔记
    老猿学5G:3GPP 5G规范中的URI资源概念
    区块链学习1:Merkle树(默克尔树)和Merkle根
    老猿Python博客文章目录索引
    老猿学5G:融合计费场景的Nchf_ConvergedCharging_Create、Update和Release融合计费消息交互过程
  • 原文地址:https://www.cnblogs.com/codehhr/p/13863600.html
Copyright © 2011-2022 走看看