css居中解决方案
水平居中
行内或者具有行内元素性质的元素(比如文字或者链接)?
让一个父元素为块级元素的行内元素水平居中,可以:
CSS:
|
1
2
3
|
.center-children {
text-align: center;
}
|
单个块级元素?
你可以设置块级元素的 margin-left 和 margin-right 为 auto 来使它水平居中(这个块级元素是被设置了一个 width 属性了,否则它会占满宽度,这时候已经不需要居中了),有一种速记的写法:
CSS:
|
1
2
3
|
.center-me {
margin: 0 auto;
}
|
多个块级元素?
如果有两个或者更多的块级元素需要在被一行里水平居中,那么你最好设置他们不同的 display 属性来达到效果了。这里有两个例子:一个是设置为 inline-block, 一个是设置为 flexbox 。
HTML:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<main class="inline-block-center">
<div>
I'm an element that is block-like with my siblings and we're centered in a row.
</div>
<div>
I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
</div>
<div>
I'm an element that is block-like with my siblings and we're centered in a row.
</div>
</main>
<main class="flex-center">
<div>
I'm an element that is block-like with my siblings and we're centered in a row.
</div>
<div>
I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
</div>
<div>
I'm an element that is block-like with my siblings and we're centered in a row.
</div>
</main>
|
CSS:
|
1
2
3
4
5
6
7
8
9
10
11
|
.inline-block-center {
text-align: center;
}
.inline-block-center div {
display: inline-block;
text-align: left;
}
.flex-center {
display: flex;
justify-content: center;
}
|
除非你是想让多个块级元素堆积在彼此的顶部(一列堆积啦),那么 margin: auto 还是依然适用的:
CSS:
|
1
2
3
4
5
6
7
|
main div {
background: black;
margin: 0 auto;
color: white;
padding: 15px;
margin: 5px auto;
}
|
垂直居中
在CSS里,垂直居中是有那么一点点麻烦了。
行内或者具有行内元素性质的元素(比如文字或者链接)?
单行?
有时候行内元素或者文字显示为垂直居中,仅仅是因为它们的上下内边距相等:
CSS:
|
1
2
3
4
|
.link {
padding-top: 30px;
padding-bottom: 30px;
}
|
如果 padding 出于某些原因不能用,并且你要使一些不换行的文字居中,这里有一个技巧,就是设置文字的 line-height 和 height 的值相等。
CSS:
|
1
2
3
4
5
|
.center-text-trick {
height: 100px;
line-height: 100px;
white-space: nowrap;
}
|
多行?
上边距和下边距相等也能让多行文字达到垂直居中的效果,但是如果这种方法不奏效的话,可能需要设置文字所在的元素为一个 table cell,不管它直接是 table 还是你用CSS使这个元素表现的像一个 table cell,vertical-align 属性可以处理这种情况,它与我们通常所做的在行上处理元素对齐的方式不同:
CSS:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
table {
background: white;
width: 240px;
border-collapse: separate;
margin: 20px;
height: 250px;
}
table td {
background: black;
color: white;
padding: 20px;
border: 10px solid white;
/* default is vertical-align: middle; */
}
.center-table {
display: table;
height: 250px;
background: white;
width: 240px;
margin: 20px;
}
.center-table p {
display: table-cell;
margin: 0;
background: black;
color: white;
padding: 20px;
border: 10px solid white;
vertical-align: middle;
}
.flex-center-vertically {
display: flex;
justify-content: center;
flex-direction: column;
height: 400px;
}
|
如果没法用类table方式,可能你需要用 flexbox ?单个的 flex 子元素可以非常简单的被一个 flex 父元素垂直居中:
CSS:
|
1
2
3
4
5
6
|
.flex-center-vertically {
display: flex;
justify-content: center;
flex-direction: column;
height: 400px;
}
|
请记住这个方法仅仅适用于父容器具有一个固定的额高度(px,%,等等),这也是为什么容器有一个高度。
如果上面的方法都不能用,你可以试试 ”虚元素“ 技术:其中一个完整高度的伪元素放置在容器内,并与文本垂直对齐。
CSS:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
.ghost-center {
position: relative;
}
.ghost-center::before {
content: " ";
display: inline-block;
height: 100%;
width: 1%;
vertical-align: middle;
}
.ghost-center p {
display: inline-block;
vertical-align: middle;
}
|
块级元素?
知道元素的高度?
不知道元素的高度是比较常见的,有很多原因:如果宽度改变,文本回流会改变高度;文字样式改变会改变高度;文字数量改变会改变高度;一个固定比例的元素,比如图片,当重置尺寸的时候也会改变高度,等等。
但如果你知道高度,你可以这样垂直居中元素:
CSS:
|
1
2
3
4
5
6
7
8
9
|
.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; */
}
|
元素高度未知?
可以通过 transform 来达到目的:
CSS:
|
1
2
3
4
5
6
7
8
9
|
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
|
能用 flexbox 吗?
毫无疑问,用 flexbox 简单太多了:
CSS:
|
1
2
3
4
5
|
.parent {
display: flex;
flex-direction: column;
justify-content: center;
}
|
垂直水平居中
你可以通过不同的方式结合上面的技术来得到一个完美的居中水平垂直居中元素。但是我发现,这些方法通常都属于以下三种阵营:
元素有固定的宽和高?
用负的 margin 值使其等于宽度和高度的一半,当你设置了它的绝对位置为 50% 之后,就会得到一个很棒的跨浏览器支持的居中:
CSS;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
.parent {
position: relative;
}
.child {
width: 300px;
height: 100px;
padding: 20px;
position: absolute;
top: 50%;
left: 50%;
margin: -70px 0 0 -170px;
}
|
元素的宽和高未知?
如果你不知道元素的高度和宽度,你可以用 transform 属性,用 translate 设置 -50%(它以元素当前的宽和高为基础)来居中:
CSS:
|
1
2
3
4
5
6
7
8
9
|
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
|
能用 flexbox 吗?
为了让元素在 flexbox 两个方向都居中,你需要两个居中属性:
CSS:
|
1
2
3
4
5
|
.parent {
display: flex;
justify-content: center;
align-items: center;
}
|
css居中解决方案的更多相关文章
- 【前端】这可能是你看过最全的css居中解决方案了~
1.水平居中:行内元素解决方案 适用元素:文字,链接,及其其它inline或者inline-*类型元素(inline-block,inline-table,inline-flex) html部分代码: ...
- CSS居中完全解决方案
上次面试面试官问到了,问了个定宽局中和不定宽局中,下来我把所有有关CSS居中都总结了一下 原文摘自我的前端博客,欢迎大家来访问 http://www.hacke2.cn 水平居中 行内元素 把行内元素 ...
- CSS居中的实现用法实例
转载的一篇文章,讲解css内容居中的. 网上有关css 居中的文章不胜枚举,不过大多没有做系统的总结.这里分享的这篇有关css居中的文章,个人感觉不错,值得收藏. 一.水平居中1,将元素水平居中(us ...
- vue—你必须知道的 js数据类型 前端学习 CSS 居中 事件委托和this 让js调试更简单—console AMD && CMD 模式识别课程笔记(一) web攻击 web安全之XSS JSONP && CORS css 定位 react小结
vue—你必须知道的 目录 更多总结 猛戳这里 属性与方法 语法 计算属性 特殊属性 vue 样式绑定 vue事件处理器 表单控件绑定 父子组件通信 过渡效果 vue经验总结 javascript ...
- CSS居中demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...
- layer弹出层不居中解决方案
layer弹出层不居中解决方案 代码头中加入以下代码即可 <!doctype html>
- css居中那点事儿
css居中那点事儿 在css中对元素进行水平居中是非常简单的,然而使元素垂直居中就不是一件简单的事情了,多年以来,垂直居中已经成为了CSS领域的圣杯,因为它是极其常见的需求,但是在实践中却不是一件简单 ...
- css居中学习笔记
css居中学习笔记 一.水平居中 以下面的代码为例: <body> <div class="parent"> <div class="chi ...
- 理解CSS居中
我想很多在前端学习或者开发过程中,肯定会遇到如何让你的元素居中的问题,网上google肯定会有很多的解决方法.今天我就个人的项目与学习经验谈谈个人理解css如何让元素居中. 要理解css的居中,首先必 ...
随机推荐
- otter双主同步安装与配置
otter是阿里的开源数据同步项目,资源地址就不用说了哈,网上找,阿里云论坛关于单方向同步的配置已经很清楚了,理论上说,双主同步也不复杂,但是毕竟 是数据库,比较重要,配置双主的时候,总觉得心里没底, ...
- Powershell获取并导出指定日期EventLog
$date = Get-Date 28/07/16 Get-EventLog -After $date "Application"|Export-Csv c:\ming.csv
- Linux下调试程序方法
您可以用各种方法来监控运行着的用户空间程序:可以为其运行调试器并单步调试该程序,添加打印语句,或者添加工具来分析程序.本文描述了几种可以用来调试在 Linux 上运行的程序的方法.我们将回顾四种调试问 ...
- 关于JS 沙箱(转)
javascript中的沙箱并非传统意义上的沙箱,只是一种语法上的hack写法而已,javascript中处理模块依赖关系的闭包被称之为沙箱,和 ajax一样,这种sandbox coding风格是一 ...
- adb devices 不识别设备(或者偶尔识别设备) -破解
问题:当在CMD中输入adb devices时,没有设备信息显示,居然显示下面的信息 问题是有时候可以识别,有时候不可以识别.当被别人连接后,自己在连接是好的,过了一会以后又不好了
- 去空格 whitespaceAndNewlineCharacterSet和过滤字符串
一.过滤字符串 可以使用stringByTrimmingCharactersInSet函数过滤字符串中的特殊符号 首先自己定义一个NSCharacterSet, 包含需要去除的特殊符号 NSChara ...
- cmd运行的程序的工作目录
如图所示,cmd通过输入自己编写的程序的实际路径,或者将程序放在环境变量中然后在cmd中执行,用start执行,该程序运行时的工作目录都是cmd当前所在目录:在cmd中输入该程序的快捷方式执行该程序, ...
- Android之ImageSwitcher
要点: (查看Api总结) 1:ImageSwitcher 继承 ViewSwitcher, (ViewSwitcher 有继承FrameLayout ) 2: 要实现切图必须实现 ViewSwitc ...
- jQueryDOM操作笔记
attr(name[,value]):value(任意|函数) $('*').attr('title',function(index,previousValue){ return previousVa ...
- linux修改主机名称
http://blog.csdn.net/qq_20480611/article/details/51017033 ========================================== ...