当前位置: 首页 > news >正文

CSS transition 过渡

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的值设为0,margin为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%);
}
http://www.lryc.cn/news/154980.html

相关文章:

  • Unity中Shader的UV扭曲效果的实现
  • Automotive 添加一个特权APP
  • 自定义TimeLine
  • 如何使用SQL系列 之 如何在SQL中使用WHERE条件语句
  • leetcode:1941. 检查是否所有字符出现次数相同(python3解法)
  • Echarts 各种点击事件监听
  • 《智能网联汽车自动驾驶功能测试规程》
  • NVIDIA CUDA Win10安装步骤
  • Elasticsearch、Kibana以及Java操作ES 的快速使用
  • 逐鹿人形机器人,百度、腾讯、小米卷起来
  • AndroidStudio推荐下载和配置
  • mysql异常占用资源排查
  • requests 库:发送 form-data 格式的 http 请求 (python)
  • 行测图形推理规律(一)元素组成
  • 【python爬虫】13.吃什么不会胖(爬虫实操练习)
  • 深入理解联邦学习——联邦学习与现有理论的区别与联系
  • 基于Python+DenseNet121算法模型实现一个图像分类识别系统案例
  • 旋转图片两种方法
  • 10 mysql tiny/small/medium/big int 的数据存储
  • UI自动化测试之Jenkins配置
  • 电视盒子什么品牌好?数码博主盘点目前性能最好的电视盒子
  • 对于枚举类型的输出
  • solidity开发环境配置,vscode搭配remix
  • chatGPT生成代码--go组合算法
  • 推荐6款普通人搞副业做自媒体AI工具
  • vs中git提交合并分支的步骤记录
  • PostgreSQL 备份恢复:pg_probackup
  • 博客程序系统其它功能扩充
  • MATLAB 2023安装方法之删除旧版本MATLAB,安装新版本MATLAB
  • 全国唯一一所初试考Java的学校!平均300分拿下