便宜vps推荐!

css--元素居中常用方法总结

时间:|浏览:| 评论 ()

元素居中是日常开发和学习中最常见的问题,同时也是面试中经常考察的知识点,本文来总结一下这方面的知识点。

  1、水平居中

  (1)子父元素宽度固定,子元素设置 margin:auto,并且子元素不能设置浮动,否则居中失效。

<div class="wrap">
        <div class="inner"></div>
    </div>
    <style>
        .wrap {
            width: 500px;
            background-color: skyblue;
        }
        .inner {
            width: 200px;
            height: 100px;
            background-color: sandybrown;
            margin: auto;
        }
    </style>

  (2)子父元素宽度固定,父元素设置 text-align:center,子元素设置display:inline-block,并且子元素不能设置浮动,否则居中失效。


    <div class="wrap">
        <div class="inner"></div>
    </div>
    <style>
        .wrap {
            width: 500px;
            background-color: skyblue;
            text-align: center;
        }

        .inner {
            width: 200px;
            height: 100px;
            display: inline-block;
            background-color: sandybrown;
        }
    </style>

2、水平垂直居中

  (1)子父元素宽高度固定,子元素相对于父元素绝对定位(子绝父相),子元素 top,left 设置 50%,子元素 margin-top 和 margin-left 减去各自宽高的一半或者 transform :translate(-50%,-50%)。


    <div class="wrap">
        <div class="inner"></div>
    </div>
    <style>
        .wrap {
            width: 500px;
            height: 200px;
            background-color: skyblue;
            position: relative;
        }

        .inner {
            width: 200px;
            height: 100px;
            background-color: sandybrown;
            position: absolute;
            top: 50%;
            left: 50%;
            /* transform: translate(-50%, -50%); */
            margin-top: -50px;
            margin-left: -100px;
        }
    </style>

  (2)子父元素宽高度固定,子元素相对于父元素绝对定位(子绝父相),使用calc达到上面效果。


    <div class="wrap">
        <div class="inner"></div>
    </div>
    <style>
        .wrap {
            width: 500px;
            height: 200px;
            background-color: skyblue;
            position: relative;
        }
        .inner {
            width: 200px;
            height: 100px;
            background-color: sandybrown;
            position: absolute;
            top: calc(50% - 50px);
            left: calc(50% - 100px);

        }
    </style>

 

   (3)子父元素宽高度固定,子元素相对于父元素绝对定位(子绝父相),子元素上下左右全为 0,然后设置子元素margin:auto。


    <div class="wrap">
        <div class="inner"></div>
    </div>
    <style>
        .wrap {
            width: 500px;
            height: 200px;
            background-color: skyblue;
            position: relative;
        }
        .inner {
            width: 200px;
            height: 100px;
            background-color: sandybrown;
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            margin: auto;
        }
    </style>

 

   (4)子元素相对定位,子元素 top,left 值为 50%,transform:translate(-50%,-50%)。


    <div class="wrap">
        <div class="inner"></div>
    </div>
    <style>
        .wrap {
            width: 500px;
            height: 200px;
            background-color: skyblue;
        }
        .inner {
            width: 200px;
            height: 100px;
            background-color: sandybrown;
            position: relative;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
    </style>

   (5)文本水平垂直居中 父元素设置text-algin:center使得子元素水平居中,子元素设置line-height为父元素高度,使得子元素垂直居中。


    <div class="wrap">
        <span class="inner">321311111111111111</span>
    </div>
    <style>
        .wrap {
            width: 500px;
            height: 200px;
            text-align: center;
            background-color: skyblue;
        }

        .inner {
            line-height: 200px;
            background-color: sandybrown;
        }
    </style>

 

  (6)利用line-height,vertical-align实现元素水平垂直居中。


    <div class="wrap">
        <div class="inner">321</div>
    </div>
    <style>
        .wrap {
            width: 500px;
            height: 200px;
            background-color: skyblue;
            line-height: 200px;
            text-align: center;
        }
        .inner {
            width: 200px;
            height: 100px;
            background-color: sandybrown;
            display: inline-block;/* 将子元素设置为行内块级元素 */
            vertical-align: middle;/* 垂直居中 */
            text-align: left;/* 默认继承了父元素的水平居中,这里需要修正修正文字 */
        }
    </style>

 

   (7)父元素设置弹性盒子,display:flex; justfy-content:center ;align-item:center。


    <div class="wrap">
        <div class="inner"></div>
    </div>
    <style>
        .wrap {
            width: 500px;
            height: 200px;
            background-color: skyblue;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .inner {
            width: 200px;
            height: 100px;
            background-color: sandybrown;
        }
    </style>

 

   (8)父元素设置 display:table-cell vertical-align:middle,子元素设置 margin:auto。


    <div class="wrap">
        <div class="inner"></div>
    </div>
    <style>
        .wrap {
            width: 500px;
            height: 200px;
            background-color: skyblue;
            display: table-cell;
            vertical-align: middle
        }
        .inner {
            width: 200px;
            height: 100px;
            background-color: sandybrown;
            margin: auto;
        }
    </style>

 

  (9)网格布局实现水平垂直居中display: grid;place-items: center。


    <div class="wrap">
        <div class="inner"></div>
    </div>
    <style>
        .wrap {
            width: 500px;
            height: 200px;
            background-color: skyblue;
            display: grid;
            place-items: center;
        }

        .inner {
            width: 200px;
            height: 100px;
            background-color: sandybrown;
        }
    </style>

 

以上就是本文的全部内容,希望给读者带来些许的帮助和进步,方便的话点个关注,小白的成长之路会持续更新一些工作中常见的问题和技术点。

标签:css 前端
声明:VPSNO仅为VPS服务器信息分享,任何IDC都有倒闭和跑路可能,本站无法作任何保障,请网友购买前自行斟酌,衡量评估风险,自负责任。数据勤备份是最佳选择。本文由VPSNO网站发布,转载请保留链接:《css--元素居中常用方法总结》站长联系QQ:22⑧2六8⑦肆8,邮箱为QQ邮箱。
网友评价
评论列表
共有 条评论

精选便宜VPS - VPS排行

  • No.1 最便宜的大厂云服务器
    UCloud云服务器

    UCloud云服务器

    UCloud(优刻得)是国内最大的中立公有云服务商,全球包含香港、台湾共25个数据中心,52元/年起

  • No.2 国内用户最多的云服务器
    阿里云服务器

    阿里云服务器

    阿里云是国内云服务器第一梯队头部商家,目前国内用户选择最多的云服务器商。

  • No.3 号称“良心云”的云服务器
    腾讯云服务器

    腾讯云服务器

    和阿里云规模相当的国内云服务器商,套路少,给100%CPU资源,拥有更好的性价比。

  • 热度飙升最快的国外VPS
    RackNerd

    RackNerd

    RackNerd是2019年成立的主机商,以提供便宜VPS、大带宽VPS闻名,近年来在国内用户中知名度颇高。

  • 最受国人欢迎的国外VPS
    bandwagonhost搬瓦工

    bandwagonhost搬瓦工

    搬瓦工VPS可能是中国用户数量最多的服务商,专为大陆用户定制了大量功能,线路有特别优化,快而稳定。

  • 实力雄厚的老牌国外VPS
    vultr

    vultr

    Vultr是成立于2014年的老牌IDC,全球16个机房,硬件配置极高,新用户注册赠送100美元余额。