码迷,mamicode.com
首页 > Web开发 > 详细

css 居中布局方案

时间:2020-02-11 19:13:29      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:content   一点   通过   splay   尺寸   ems   sky   article   transform   

position(transform css3  有些浏览器不兼容)

<article id="one">
  <section id="section"></section>
</article>

  <style>
    #one {
      position: relative;  //此处不设置的话  会一直往上找 找到body
      width: 300px;
      height: 300px;
      background: lightskyblue;
    }
      #section {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 100px;
        height: 100px;
        background: aliceblue;
        transform: translate(-50%, -50%);
      }
  </style>

2.margin-top(需要知道具体尺寸计算)

<article id="one">
  <section id="section"></section>
</article>

  <style>
    #one {
      position: relative;
      width: 300px;
      height: 300px;
      background: lightskyblue;
    }
    #section {
      position: absolute;
      left: 50%;
      top: 50%;
      width: 100px;
      height: 100px;
      margin-left: -50px; // 需要通过 计算  但是兼容性好
      margin-top: -50px;
      background: aliceblue;
    }
  </style>

3.flex(简单,兼容问题)

<article id="one">
  <section id="section"></section>
</article>

  <style>
    #one {
      display: flex;
      width: 300px;
      height: 300px;
      background: lightskyblue;
      justify-content: center;
      align-items: center;
    }
    #section {
      width: 100px;
      height: 100px;
      background: aliceblue;
    }
  </style>

4.gred

<article id="one">
  <section id="section"></section>
</article>  

<style>
    #one {
      display: grid;  //和flex就一点不同   ????
      width: 300px;
      height: 300px;
      background: lightskyblue;
      justify-content: center;
      align-items: center;
    }
    #section {
      width: 100px;
      height: 100px;
      background: aliceblue;
    }
  </style>

 

css 居中布局方案

标签:content   一点   通过   splay   尺寸   ems   sky   article   transform   

原文地址:https://www.cnblogs.com/-constructor/p/12296203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!