CSS居中的几种方式

水平居中方式

  • 1.margig:0 auto;(不能受到float影响)
<style>
    *{
        padding: 0;
        margin: 0;
    }
        .box{
            width: 300px;
            height: 300px;
            border: 3px solid red;
            /*text-align: center;*/
        }
        img{
            display: block;
            width: 100px;
            height: 100px;
            margin: 0 auto;
        }
    </style>
<!--html-->
<body>
    <div class="box">
        ![](..)
    </div>
</body>
  • 2.text-align: center;(文本水平居中)
    设置在父元素上
<style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            width: 300px;
            height: 300px;
            border: 5px solid red;
            text-align: center;
        }
        img{
            display: block;
            width: 100px;
            height: 100px;
            /*margin: 0 auto;*/
        }
    </style>
<body>
<div class="box">
    ![](images/1.jpg)
    <p>文本内容</p>
</div>
</body>

水平垂直居中方式

  • 1.子绝父相定位居中
  • 子绝父相定位到top值50%,left值50%。需要定位的元素的margin-top/margin-left减去宽高的一半。
  • 这种方法的局限性在于需要知道需要垂直居中的宽高才能实现,经常使用这种方法
<style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            width: 300px;
            height: 300px;
            border:5px solid red;
            position: relative;           //父相
        } 
        img{
            width: 100px;
            height: 100px;
            position: absolute;       //子绝
            top: 50%;         
            left: 50%;
            margin-top: -50px;
            margin-left: -50px;
        }
    </style>
<body>
<div class="box" >
    ![](images/2.jpg)
</div>
</body>
  • 2.子绝父相定位和margin:auto;
    这个方法也很实用,不用受到宽高的限制,也很好用
   <style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            width: 300px;
            height: 300px;
            border:5px solid red;
            position: relative;
        }
        img{
            width: 100px;
            height: 100px;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
    </style>
<body>
<div class="box" >
    ![](images/3.jpeg)
</div>
</body>
  • 3.子绝父相定位和transfrom
    这个方法比较高级,用到了形变,据我所知很多大神喜欢使用这个方法进行定位,逼格很高的方法。
<style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            width: 300px;
            height: 300px;
            border:5px solid red;
            position: relative;
        }
        img{
            width: 100px;
            height: 100px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);      //平移(水平值,垂直值)
        }
    </style>
<body>
<div class="box" >
    ![](images/4.jpg)
</div>
</body>
  • 4.diplay:table-cell
    其实这个就是把其变成表格样式,再利用表格的样式来进行居中,很方便
 <style>
     *{
            padding: 0;
            margin: 0;
        }
        .box{
            width: 300px;
            height: 300px;
            border:5px solid red;
            display: table-cell;
            vertical-align: middle;             //垂直居中
            text-align: center;                  //水平居中
          /*margin: 100px auto;*/           //受影响
        }
        img{
            width: 100px;
            height: 100px;
        }
    </style>
<body>
<div class="box" >
    ![](images/5.jpg)
</div>
</body>
  • 5.display:flex;居中
    这个用了C3新特性flex,非常方便快捷,在移动端使用完美,pc端有兼容性问题。
 <style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            width: 300px;
            height: 300px;
            border:5px solid red;
            display: flex;
            justify-content: center;
            align-items:center;
            /*margin: 100px auto;*/                 //不受影响
        }
        img{
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body>
<div class="box" >
    ![](images/4.jpg)
</div>
</body>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、垂直对齐 如果你用CSS,则你会有困惑:我该怎么垂直对齐容器中的元素?现在,利用CSS3的Transform,...
    kiddings阅读 3,195评论 0 11
  • 不会用代码框,所以看着有些乱套,,,,html部分 <!DOCTYPE html> 迅雷看看 ...
    这就是个帅气的名字阅读 1,560评论 0 0
  • 各种纯css图标 CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出。直接用CSS3画出这些图形,要比...
    剑残阅读 9,661评论 0 8
  • 今天升级了一下Android Studio,同时升级了一下gradle的版本,升级之后项目运行不起来,而且Grad...
    小狮子365阅读 2,782评论 0 1
  • 不知不觉中,小区内一半以上的商家接入了支付宝口碑平台,这件事情触动我不小,之前过度关注微信,没有想到过支付宝对于线...
    quentinwang阅读 1,117评论 0 51