CSS transition 过渡

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

1 前言

水平居中、垂直居中是前端面试百问不厌的问题。

其实现方案也是多种多样常叫人头昏眼花。

水平方向可以认为是内联方向垂直方向认为是块级方向。

下面介绍一些常见的方法。

2 内联元素的水平垂直居中

首先常见内联元素有a、span、em、b、strong、i、button

<div class="container">
    <span class="innerText">Hello,World!</span>
</div>

2.1 使用display: flex

dispaly: flex将父级容器设置为弹性布局然后可以通过justify-content: center控制水平居中使用align-items: center控制垂直居中。

.container {
    height: 100px;
    width: 200px;
    background-color: cadetblue;
    display: flex;
    /* 水平居中 */
    justify-content: center;
    /* 垂直居中 */
    align-items: center;
}

2.2 使用text-align和line-hight

设置父级容器的text-align: center实现文本水平居中对齐关键的一步设置行高line-height为容器的高度即可实现文本垂直居中。

为什么设置行高line-height为容器的高度就能实现文本垂直居中

因为文本的基线位于行高的中间基线也是文本的中线设置行高等于高度后文字就垂直居中了。

.container {
    height: 100px;
    width: 200px;
    background-color: antiquewhite;
    /* 水平居中 */
    text-align: center;
    /* 垂直居中行高等于高度 */
    line-height: 100px;
}

2.3 使用text-align和display: table-cell

将元素设置为单元格再使用vertical-align: center实现垂直居中。

.container {
    height: 100px;
    width: 200px;
    background-color: antiquewhite;
    display: table-cell;
    /* 水平居中 */
    text-align: center;
    /* 垂直居中 */
    vertical-align: middle;
}

2.4 使用display: grid

.container {
    height: 100px;
    width: 200px;
    background-color: antiquewhite;
    display: grid;
    place-items: center;
}

place-items是align-items、justify-items的简写。

3 块级元素的水平垂直居中

常见块级元素有h1-h6、p、div、ul、ol、li等。

<div class="container">
    <div class="innerText"></div>
</div>

前面介绍的内联元素的水平垂直居中方法也适用于块级元素。下面就不再重复介绍。

3.1 定位+间距的多种组合

设置父元素为相对定位。

为什么要设置父元素为相对定位

  1. 创建一个提供给子元素的定位坐标系使得子元素在该坐标系内定位。
  2. 绝对定位的元素会脱离正常文档流并相对于最近的已定位组件进行定位。
  3. margin: auto计算居中位置依赖于外部的相对定位的父元素。

设置子元素为绝对定位其top、left、right、bottom的值设为0margin为auto即可让子元素自动调整四周距离一样实现水平垂直居中。

.container {
    height: 100px;
    width: 200px;
    background-color: cadetblue;
    position: relative;
}
.innerText {
    background-color: black;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    width: 50px;
    height: 50px;
    /* 水平垂直居中 */
    margin: auto;
}

使用负margin值实现元素的平移。

.container {
    height: 100px;
    width: 200px;
    background-color: cadetblue;
    position: relative;
}
.innerText {
    width: 50px;
    height: 50px;
    background-color: black;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -25px 0 0 -25px;
}

使用translate实现元素的平移。

.container {
    height: 100px;
    width: 200px;
    background-color: cadetblue;
    position: relative;
}
.innerText {
    width: 50px;
    height: 50px;
    background-color: black;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
}
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: css

“CSS transition 过渡” 的相关文章

通过url删除ES中的数据

curl -XDELETE "http://test-hostname:9200/product*_201704*"...

Python自带哪些库 - 编程语言

这篇文章主要讲解了“Python自带哪些库”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python自带哪些库”吧! 1.CounterCounter的主要用途从名字就可以看出,它是用来进行...

vue-router4版本第一次打开界面不匹配路由问题怎么解决 - 开发技术

这篇文章主要介绍“vue-router4版本第一次打开界面不匹配路由问题怎么解决”,在日常操作中,相信很多人在vue-router4版本第一次打开界面不匹配路由问题怎么解决问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”vue-route...

cmd

C:\Users\zhangzhang>ping -a 10.144.48.104 正在 Ping zhangzhang.ds.company.com.cn [10.144.48.104] 具有 32 字节的数据:来自 10.144.48.104 的回复: 字节=32 时间<1...

StopWath

StopWath是apache commons lang包下的一个任务执行时间监视器主要方法: start(); //开始计时 split(); //设置split点 getSplitTime(); //获取从start 到 最后一次split的时间...

Script Engine使用

Java SE 6最引人注目的新功能之一就是内嵌了脚本支持。在默认情况下,Java SE 6只支持JavaScript,但这并不以为着Java SE 6只能支持JavaScript。在Java SE 6中提供了一些接口来定义一个脚本规范,也就是JS...