网站首页 > 知识剖析 正文
这里是工作狂的聚集地 | ||||
职场 | 学术 | 新媒体 | 设计 | 极客 |
专门治愈处女座强迫症。
本文为CSS入门
翻译 redman9
原载CSS-Trick
人们经常抱怨在 CSS 中居中元素的问题,其实这个问题并不复杂,只是因为方法众多,需要根据情况从众多方法中选取一个出来。接下来,我们做一个 "决定树" 来帮我们把问题变的简单一点。首先你需要居中:
—— 水平 ——
?需要居中inline
或者inline-*
元素(如文字或者链接)?
? 需要居中block
类的元素?
? 需要居中多个block
元素?
—— 垂直 ——
?需要居中inline
或者inline-*
元素(如文字或者链接)?
?需要居中block
类的元素?
——既水平又垂直 ——
?固定宽高
?不固定宽高
?使用flexbox
● ● ●
水平居中
水平居中inline
或者inline-*
元素▼
你可以轻松的在一个block
元素中水平居中一个inline
元素,以下代码对inline
,inline-block
,inline-table
和inline-flex
等有效。
.parent {text-align: center;}
水平居中block
类的元素▼
在block
元素被设定固定宽度的情况下,可以使用设置元素margin-left
和margin-right
的值为auto
的方法实现水平居中。
.child {width: 400px;margin: 0 auto;}
水平居中多个block
类的元素▼
通过inline-block
实现
.parent {
text-align: center;
}
.child {
display: inline-block;
text-align: left;
}
通过flexbox
实现
.parent {
display: flex;
justify-content: center;
}
● ● ●
垂直居中
垂直居中inline
或者inline-*
元素▼
【单行】
inline/text
元素可以简单的用设置相同的上下padding
值达到垂直居中的目的。
.center {
pading-top: 30px; padding-bottom: 30px;
}
如果因为某种原因不能使用padding
的方法,你还可以设置line-height
等于height
来达到目的。
.center {
height: 100px; line-height: 100px; white-space: nowrap;
}
【多行】
相同的上下padding
也可以适用于此种情况,如果不能生效,你可以尝试将该元素的父元素的display
设置为table
,同时该元素的display
设置为table-sell
,然后设置vertical-align
。
.parent {
display: table;
width: 200px; height: 400px;
} .child {
display: table-cell;
vertical-align: middle;
}
如果上述方法不能使用,你可以尝试使用flexbox
,一个单独的flexbox
子元素可以轻易的在其父元素中居中。谨记,这种方法需要父元素有固定的高度。
.parent {
display: flex; justify-content: center;
flex-direction: column; height: 400px;
}
如果上述两种方式均不能使用,你可以使用“幽灵元素”技术,这种方法采用伪元素::before
撑开高度 ,文字垂直居中。
.parent {
position: relative;
} .parent::before {
content: " "; display: inline-block; height: 100%; width: 1%; vertical-align: middle;
} .child {
display: inline-block;
vertical-align: middle; }
垂直居中block
类的元素▼
【已知元素高度】
.parent {
position: relative; } .child {
position: absolute;
top: 50%;
height: 100px;
margin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */
}
【未知元素高度】
.parent {
position: relative; } .child {
position: absolute;
top: 50%;
transform: translateY(-50%); }
【使用flexbox
】
.parent {
display: flex;
flex-direction: column;
justify-content: center; }
● ● ●
既水平又垂直
【固定宽高】
.parent {
position: relative; } .child {
width: 300px;
height: 100px;
padding: 20px;
position: absolute;
top: 50%;
left: 50%;
margin: -70px 0 0 -170px; }
【不固定宽高】
.parent { position: relative; } .child { position: absolute; top: 50%; left: 50%;
transform: translate(-50%, -50%); }
【使用flexbox
】
.parent { display: flex; justify-content: center; align-items:center; }
- 上一篇: HTML行内元素与块级元素有哪些及区别?
- 下一篇: 初识CSS——元素的显示和隐藏
猜你喜欢
- 2024-11-23 Python的selenium实现等待某个元素加载完成后返回结果
- 2024-11-23 DOM 操作之DOM概念和获取元素
- 2024-11-23 html 哪些是块级元素、哪些是行内元素?
- 2024-11-23 前端面试题-CSS-可替换元素和非替换元素
- 2024-11-23 小白总结:前端HTML基础知识点(1)——元素总结
- 2024-11-23 HTML Video 元素在 Vue 中消失的 muted 属性
- 2024-11-23 用CSS 实现元素垂直居中,有哪些好的方案?
- 2024-11-23 JavaScript知识点总结(7)
- 2024-11-23 Java 0基础入门 (Html表单、表单元素)
- 2024-11-23 《css大法》之使用伪元素实现超实用的图标库
- 最近发表
-
- 测试进阶:实现跨请求地保持登录的神器session你get了么?
- Python 爬虫入门五之 Cookie 的使用
- 在Node应用中实施Web认证的四大方法
- PHP防火墙代码,防火墙,网站防火墙,WAF防火墙,PHP防火墙大全
- 程序员和IT人都应该懂的知识:HTTP入门图解
- 如何请求一个需要登陆才能访问的接口(基于cookie)——apipost
- 提高 PHP 代码质量的 36 计(如何提高php技术)
- 彻底搞懂Token、Session和Cookie(token和cookie sessions什么区别)
- 一文详解Python Flask模块设置Cookie和Session
- 超详细的网络抓包神器 tcpdump 使用指南
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)