SS中文字居中显示的方式有以下五种:
将text-align属性值设置为center可以将文本居中显示。
.center {
text-align: center;
}
将vertical-align属性值设置为middle可以将文本垂直居中显示。
.center {
vertical-align: middle;
}
将line-height属性值设置为比字体大小略大的值,可以使文本在容器中垂直居中显示。
.center {
line-height: 20px;
}
display: flex属性将父元素设置为弹性布局,并使用align-items: center属性将子元素在交叉轴上居中对齐。
.center {
display: flex;
align-items: center;
}
position: absolute属性和transform: translateY(-50%)将子元素相对于其父元素垂直居中对齐。
// 父容器
.center {
position: relative;
height: 200px;
}
// 子容器
.center > div {
position: absolute;
top: 50%;
left: 50%;
transform: translateY(-50%) translateX(-50%);
height: 100px;
width: 200px;
background-color: #ccc;
}
以上就是CSS中文字居中显示的几种方式,根据实际需求选择合适的方式即可。
● ●
在网页上使 HTML 元素居中看似一件很简单的事情. 至少在某些情况下是这样的,但是复杂的布局往往使一些解决方案不能很好的发挥作用。
在网页布局中元素水平居中比元素垂直居中要简单不少,同时实现水平居中和垂直居中往往是最难的。现在是响应式设计的时代,我们很难确切的知道元素的准确高度和宽度,所以一些方案不大适用。据我所知, 在CSS中至少有六种实现居中的方法。我将使用下面的HTML结构从简单到复杂开始讲解:
<div class="center"> <img src="jimmy-choo-shoe.jpg" alt></div>
鞋子图片会改变,但是他们都会保持500px
X500px
的大小。 HSL colors 用于使背景颜色保持一致。
有时显而易见的方案是最佳的选择:
div.center { text-align: center; background: hsl(0, 100%, 97%);
}
div.center img { width: 33%; height: auto;}
这种方案没有使图片垂直居中:你需要给<div>
添加padding
或者给内容添加margin-top
和margin-bottom
使容器与内容之间有一定的距离。
这种方式实现水平居中和上面使用text-align的方法有相同局限性。
div.center { background: hsl(60, 100%, 97%);}
div.center img { display: block; width: 33%; height: auto; margin: 0 auto;}
注意: 必须使用display: block
使margin: 0 auto
对img
元素生效。
使用 display: table-cell
, 而不是使用table
标签; 可以实现水平居中和垂直居中,但是这种方法需要添加额外的元素作为外部容器。
<div class="center-aligned"> <div class="center-core"> <img src="jimmy-choo-shoe.jpg"> </div></div>
CSS:
.center-aligned { display: table; background: hsl(120, 100%, 97%); width: 100%;}.center-core { display: table-cell; text-align: center; vertical-align: middle;}
.center-core img { width: 33%; height: auto;}
注意:为了使div
不折叠必须加上width: 100%
,外部容器元素也需要加上一定高度使得内容垂直居中。给html
和body
设置高度后,也可以使元素在body
垂直居中。此方法在IE8+浏览器上生效。
这种 方案 有非常好的跨浏览器支持。有一个缺点就是必须显式声明外部容器元素的height
:
.absolute-aligned { position: relative; min-height: 500px; background: hsl(200, 100%, 97%);}.absolute-aligned img { width: 50%; min-width: 200px; height: auto; overflow: auto; margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0;}
Stephen在他的 博客 中演示了这种方案的几种变化。
Chris Coiyer 提出了一个使用 CSS transforms 的新方案。 同样支持水平居中和垂直居中:
.center { background: hsl(180, 100%, 97%); position: relative; min-height: 500px;}.center img { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 30%; height: auto;}
但是有以下几种缺点:
CSS transform 在部分就浏览器上需要使用 前缀。
不支持 IE9 以下的浏览器。
外部容器需要设置height
(或者用其他方式设置),因为不能获取 绝对定位 的内容的高度。
如果内容包含文字,现在的浏览器合成技术会使文字模糊不清。
当新旧语法差异和浏览器前缀消失时,这种方法会成为主流的居中方案。
.center { background: hsl(240, 100%, 97%); display: flex; justify-content: center; align-items: center;}.center img { width: 30%; height: auto;}
在很多方面 flexbox 是一种简单的方案, 但是它有新旧两种语法以及早期版本的IE缺乏支持 (尽管可以使用 display: table-cell
作为降级方案)。
现在规范已经最终确定,现代浏览器也大都支持,我写了一篇详细的教程 教程。
在某些情况下比flexbox更全面:
.center { background: hsl(300, 100%, 97%); min-height: 600px; position: relative;}.center img { width: 40%; height: auto; position: absolute; top: calc(50% - 20%); left: calc(50% - 20%);}
很简单,calc
允许你基于当前的页面布局计算尺寸。在上面的简单计算中, 50% 是容器元素的中心点,但是如果只设置50%会使图片的左上角对齐div
的中心位置。 我们需要把图片向左和向上各移动图片宽高的一半。计算公式为:
top: calc(50% - (40% / 2));left: calc(50% - (40% / 2));
在现在的浏览其中你会发现,这种方法更适用于当内容的宽高为固定尺寸:
.center img { width: 500px; height: 500px; position: absolute; top: calc(50% - (300px / 2)); left: calc(50% - (300px – 2)); }
我在 这篇文章 中详细讲解了calc
。
这种方案和flex一样有许多相同的缺点: 虽然在现代浏览器中有良好的支持,但是在较早的版本中仍然需要浏览器前缀,并且不支持IE8。
.center img { width: 40%; height: auto; position: absolute; top: calc(50% - 20%); left: calc(50% - 20%);}
当然还有 其他更多的方案。理解这六种方案之后,web开发人员在面对元素居中的时候会有更多的选择。
关注“网页设计自学平台”订阅号回复以下|关键字|
|dw教程|js教程|淘宝案例|软件下载|搜狐案例|网站模板
|ps教程|ai教程 |ui教程|腾讯案例| ae教程 |字体下载|
|上课素材|前端特效|华润万家案例|最新dw案例|
戳“阅读原文”入群获取最新高清前端视频!
中,是我们编码过程中最常见的,那么,我们平时常见的居中方式,下面一一罗列出来,有错误的地方,望码友多多包涵并加以矫正。
水平居中
1、多块级元素,设置display:inline-block;使之在一行排列,在父级样式里,设置text-align:center;就可以实现水平居中的效果
body {
text-align: center;
}
div{
width: 100px;
height: 100px;
border: 1px solid;
display: inline-block;
}
2、内联元素,利用text-align:center;可以实现块级元素内部的内联元素的水平居中
div {
border: 1px solid red;
width: 100px;
height: 100px;
text-align: center;
}
<div>
<span>块级元素中的行内元素的水平居中</span>
</div>
3、块级元素,通过把固定宽高的块级元素的margin-left和margin-right设置为auto,方可实现
div{
width: 100px;
height: 100px;
border: 1px solid;
margin: 0 auto;
}
<div></div>
4、利用弹性盒子(display: flex;)
给父级定宽定高,然后设置display: flex;以及justify-content: center;方可达到水平居中效果
body {
width: 500px;
height: 500px;
display: flex;
justify-content: center;
border: 1px solid red;
}
div {
width: 100px;
height: 100px;
border: 1px solid;
}
<body>
<div></div>
</body>
垂直居中
1、内联元素(单行)
通过设置元素的height和line-height,方可达到居中效果
2、多行元素,利用表布局(table)
通过给想要居中的元素的父级设置display: talbe-cell;以及vertical-align:enter;方可居中
3、弹性盒子(flex)
给父级设置display: flex;变成弹性盒子。
分两种,
(1),主轴方向为水平,直接设置 align-items: center;
(2),主轴方向为垂直,设置flex-direction: column;改变主轴方向,然后设置justify-content: center;
弹性盒模型主轴不同,居中的方式也不同,灵活应用。
4、固定宽高的块级元素
利用父相子绝的定位原理,实现垂直居中
position: absolute;
left: 50%;
top: 50%;
margin-left: (自身高度的一半);
5,未知宽高的块级元素
利用transform: translateY(-50%);方可实现
position: absolute;
top: 50%;
transform: translateY(-50%);
水平垂直方向的居中
1、固定宽高
通过margin平移整体宽高的一半,实现水平垂直居中
{
position: absolute;
width: 100px;
height: 100px;
border: 1px solid;
left: 50%;
top: 50%;
margin-top: -50px;
margin-left: -50px;
}
2、未知宽高
利用transform中的translate()属性实现
{
position: absolute;
border: 1px solid;
left: 50%;
top: 50%;
transform: translateY(-50%);
transform: translateX(-50%);
}
3、弹性盒子(flex)
通过display:flex,把父级变成弹性盒模型,利用align-items: center;justify-content: center;方可实现居中。
注意:弹性盒子容器中,多行项目的居中方式另加计算。
body {
border: 1px solid;
width: 300px;
height: 300px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
div {
border: 1px solid;
width: 100px;
height: 100px;
}
随着学习的不断深入,居中方式可以有很多种,我们要善于利用,更加明确什么情况下用怎样的居中方式。
*请认真填写需求信息,我们会在24小时内与您取得联系。