8种垂直居中的方法

八种垂直居中的方法垂直居中的需求经常遇到,通过资料实践了八种垂直居中的方法。以下的方法都围绕着该HTML展开HTML代码<divstyle=”width:300px;height:300px;”class=”wrap”><divstyle=”width:100px;height:100px”class=”box”></div></div>CSS方法1(常用):display:flex.wra

大家好,又见面了,我是你们的朋友全栈君。

八种垂直居中的方法

垂直居中的需求经常遇到,通过资料实践了八种垂直居中的方法。

以下的方法都围绕着该HTML展开
HTML代码

    <div class="wrap">
        <div class="box"></div>
    </div>

CSS
方法1(常用):display:flex

.wrap{ 
   
	width:300px;
	height:300px;
    border: 1px solid red;
    display:flex;
    justify-content:center;
    align-items:center;
}
.box{ 
   
	height:100px;
	width:100px
    boder:1px solid blue;
}

方法2: table-cell

vertical-align 属性设置一个元素的垂直对齐方式。
该属性定义行内元素的基线相对于该元素所在行的基线的垂直对齐。允许指定负长度值和百分比值。这会使元素降低而不是升高。在表单元格中,这个属性会设置单元格框中的单元格内容的对齐方式。

        .wrap{ 
   
            width: 300px;
            height: 300px;
            border: 1px solid red;
            display: table-cell;
            text-align: center;
            vertical-align: middle;
        }
        .box{ 
   
            width: 100px;
            height: 100px;
            border: 1px solid blue;
            display: inline-block;
        }

方法3: margin,transform配合

.wrap{ 
   
            width: 300px;
            height: 300px;
            background-color: pink;
            /* border: 1px solid red; */
            /*防止外边距塌陷。解决外边距塌陷的方法: 父元素加overflow:hidden或加上边框*/
            overflow: hidden;
        }
        .box{ 
   
            width: 100px;
            height: 100px;
            background-color: plum;
            margin:50% auto;
            transform: translateY(-50%); 
        }

方法4: inline-block+vertical-aligin

        .wrap { 
   
            width: 300px;
            height: 300px;
            background-color: pink;
            text-align: center;
            line-height: 300px;
        }
        .box { 
   
            width: 100px;
            height: 100px;
            /* 重新设置子元素的行高,否则会继承父元素的行高*/
            line-height: 100px;
            background-color: plum;
            display: inline-block;
            /* middle 把此元素放置在父元素的中部。 */
            vertical-align: middle;
        }

方法5:absolute+负margin

        .wrap{ 
   
            width: 300px;
            height: 300px;
            position: relative;
            background-color: plum;
        }
        .box{ 
   
            width: 100px;
            height: 100px;
            position: absolute;
            left: 50%;
            top: 50%;
            /*宽高的一半*/
            margin-left: -50px;
            margin-top: -50px;
            background-color: powderblue;
        }

方法6 absolute+margin:auto
和方法5类似,当宽度和高度未知时使用

        .wrap{ 
   
            width: 300px;
            height: 300px;
            position: relative;
            background-color: plum;
        }
        .box{ 
   
            width: 100px;
            height: 100px;
            position: absolute;
            left: 0;
            top: 0;
            bottom:0;
            right:0;
            margin:auto;
            background-color: powderblue;
        }

方法7:absolute+transform
与方法5类似

 .wrap { 
   
            width: 300px;
            height: 300px;
            position: relative;
            background-color: plum;
        }

        .box { 
   
            width: 100px;
            height: 100px;
            position: absolute;
            left: 50%;
            top: 50%;
            
            transform: translate(-50%,-50%);
            background-color: powderblue;
        }

方法8 强大的grid
阮一峰大佬Grid 网格布局教程

.wrap { 
   
            width: 300px;
            height: 300px;
            display: grid;
            background-color: plum;
        }

        .box { 
   
            width: 100px;
            height: 100px;
            align-self: center;
            justify-self: center;
            background-color: powderblue;
        }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/124788.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Vagrant-安装教程及常见问题

    Vagrant-安装教程及常见问题

    2021年10月28日
    68
  • sqlserver事务锁死_sql触发器格式

    sqlserver事务锁死_sql触发器格式一、触发器触发器(trigger)是SQL server 提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表 事件相关的特殊的存储过程,它的执行不是由程序调用,也不是手工启动,而是由事件来触发,当对一个表 进行操作( insert,delete, update)时就会激活它执行。触发器经常用于加强数据的完整性约束和业务 规则等。 触发器可以从 DBA_TRIGGERS ,USER_TR…

    2022年8月18日
    7
  • kubeadm部署高可用kubernetes

    kubeadm部署高可用kubernetes1准备环境(所有主机执行)1.1主机列表cat>>/etc/hosts<<EOF192.168.3.71k8s-master01192.168.3.72k8s-master02192.168.3.73k8s-master03192.168.3.74k8s-worker01192.168.3.75k8s-worker02192.168.3.76k8s-worker03192.168.3.77k8s-worker04

    2022年7月25日
    9
  • nginx安装与fastdfs配置–阿里云

    nginx安装与fastdfs配置–阿里云

    2021年6月18日
    121
  • HandlerSocket_handler的使用

    HandlerSocket_handler的使用HandlerSocket的原理http://www.mysqlops.com/2011/10/19/handlersocket-principle.htmlHandlerSocket的性能测试http://www.mysqlops.com/2011/10/20/handlersocket-perf.htmlHandlerSock

    2022年8月24日
    7
  • lvs常用命令

    lvs常用命令lvs命令(具体参数的意思可以参看其他文章)1,查看配置ipvsadm-ln2,清空所有配置ipvsadm-Clistener添加:ipvsadm-A-t192.168.11.60:80-srr修改:ipvsadm-E-t192.168.11.60:80-swrr删除:ipvsadm-D-t192.168.11.60:80realserv…

    2022年7月23日
    25

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号