摘要:在文本前面插入一个高度为百分百的伪元素,让文本与其垂直对齐。块级元素或者使用垂直水平居中或者使用
</>复制代码
翻译自 https://css-tricks.com/centering-css-complete-guide/
预先给出这样的样式
水平居中 子元素为 inline 或者 inline-* 元素(例如 text 或者 links)</>复制代码
使用 text-align: center; 的方法
子元素为 block 元素</>复制代码
- .container {
- text-align: center;
- }
- .child {
- display: inline-block;
- }
使用 margin: 0 auto; 的方法
有多个 block 元素</>复制代码
- .container {}
- .child {
- margin: 0 auto;
- }
如果你有两个或更多的 block-level 元素需要在一行内居中
方法一:
改变 display 的类型为 inline-block
</>复制代码
- .container {
- text-align: center;
- }
- .child {
- display: inline-block;
- }
方法二:
使用 flexbox
垂直居中 子元素为 inline 或者 inline-* 元素(例如 text 或者 links) 只有一行文本</>复制代码
- .container {
- display: flex;
- justify-content: center;
- }
- .child {}
一个小技巧是将 height 与 line-height 设置为相同的值
有多行文本</>复制代码
center
- .container {}
- .child {
- height: 150px;
- line-height: 150px;
- }
可以尝试将父元素的 display 设置为 table ,同时设置该元素的 display 为 table-cell,然后设置vertical-align: middle
</>复制代码
I"m vertically centered multiple lines of text in a CSS-created table layout.
- .container {
- display: table;
- width: 150px;
- }
- .child {
- display: table-cell;
- vertical-align: middle;
- }
或者实际的将其放入 table 中
</>复制代码
- I"m vertically centered multiple lines of text in a real table cell.
- table {
- width: 150px;
- height: 200px;
- background-color: azure;
- }
- table td {
- background-color: bisque;
- }
或者使用 flexbox
</>复制代码
I"m vertically centered multiple lines of text in a flexbox container.
- .container {
- display: flex;
- justify-content: center;
- flex-direction: column;
- }
- .child {
- height: auto;
- }
如果上面的方法都不起作用的话,可以使用一个幽灵元素。在文本前面插入一个高度为百分百的伪元素,让文本与其垂直对齐。
块级元素</>复制代码
I"m vertically centered multiple lines of text in a flexbox container.
- .container::before {
- content: "";
- display: inline-block;
- height: 100%;
- vertical-align: middle;
- }
- .child {
- height: auto;
- display: inline-block;
- vertical-align: middle;
- }
</>复制代码
- .container {
- position: relative;
- }
- .child {
- position: absolute;
- top: 50%;
- transform: translateY(-50%);
- }
或者使用 flexbox
垂直 & 水平居中</>复制代码
- .container {
- display: flex;
- flex-direction: column;
- justify-content: center;
- }
- .child {}
</>复制代码
- .container {
- position: relative;
- }
- .child {
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- }
或者使用 flexbox
</>复制代码
- .container {
- display: flex;
- justify-content: center;
- align-items: center;
- }
- .child {}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/115271.html
摘要:但是部分浏览器存在兼容性的问题。核心代码宽高不固定水平垂直居中演示使用布局垂直水平居中核心代码使用布局垂直水平居中演示使用布局垂直水平居中核心代码使用布局垂直水平居中演示 CSS居中完全指南——构建CSS居中决策树 showImg(https://segmentfault.com/img/bV8tDq); 本文总结CSS居中,包括水平居中和垂直居中.本文相当于CSS决策树,下次再遇到...
摘要:但是部分浏览器存在兼容性的问题。核心代码宽高不固定水平垂直居中演示使用布局垂直水平居中核心代码使用布局垂直水平居中演示使用布局垂直水平居中核心代码使用布局垂直水平居中演示 CSS居中完全指南——构建CSS居中决策树 showImg(https://segmentfault.com/img/bV8tDq); 本文总结CSS居中,包括水平居中和垂直居中.本文相当于CSS决策树,下次再遇到...
摘要:正文居中是常被开发者抱怨的问题之一。注意你不能将浮动元素居中。水平且垂直居中你可以将前面的方法任意组合起来,实现完美的居中效果。这样就可以使元素水平且垂直居中,并具有良好的浏览器兼容性。 参考 Centering in CSS: A Complete Guide BY CHRIS COYIER ON SEPTEMBER 2, 2014 附:文中涉及到了flex的一些用法,如果没有接...
摘要:水平水平居中有行内元素和块元素,行内元素有文字图片链接等块元素主要是等元素。块元素对于一个块元素,可以设置其和自动,就像这样在线查看无论块元素的宽度是否已知,都可以实现水平居中。 这里主要参考的是CHRIS COYIER写的一篇的文章(点击查看),主要讲了关于css水平、垂直居中的一些方法,每个方法后面都有一个demo,可以在线查看效果。 1 水平 水平居中有行内元素和块元素,行内元素...
阅读 734·2023-04-25 18:37
阅读 2887·2021-10-12 10:12
阅读 8574·2021-09-22 15:07
阅读 627·2019-08-30 15:55
阅读 3244·2019-08-30 15:44
阅读 2269·2019-08-30 15:44
阅读 1706·2019-08-30 13:03
阅读 1632·2019-08-30 12:55