首页技术css垂直居中对齐,css水平垂直居中的几种方法

css垂直居中对齐,css水平垂直居中的几种方法

编程之家2026-06-29633次浏览

大家好,今天来为大家分享css垂直居中对齐的一些知识点,和css水平垂直居中的几种方法的问题解析,大家要是都明白,那么可以忽略,如果不太清楚的话可以看看本篇文章,相信很大概率可以解决您的问题,接下来我们就一起来看看吧!

css垂直居中对齐,css水平垂直居中的几种方法

HTML CSS中如何实现DIV中的图片水平垂直居中对齐

HTML CSS中实现DIV中的图片水平垂直居中对齐的方法:

所谓的图片水平垂直居中就是把图片放在一个容器元素中(容器大于图片尺寸或是指定了大小的容器),并且图片位居此容器正中间(中间是指元素容器的正中间),而图片不是以背景图片(background-image)形式展示,是以<img>元素形式展示的。如下图所示:

1、解决水平居中的办法:如果图片左浮动并且"display:inline"时,只要给图片设置一个"text-align:center"属性,就顺利解决了水平居中。

2、解决垂直居中的办法:使用display:table-cell和设置了display:inline-block的线合span。

完整例子:

html代码:

css垂直居中对齐,css水平垂直居中的几种方法

<ul class="imgWrap clearfix">

<li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#" class="imgBox"><span></span><img src="images/img1.jpg" alt=""/></a></li>

<li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#" class="imgBox"><span></span><img src="images/img2.jpg" alt=""/></a></li>

<li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#" class="imgBox"><span></span><img src="images/img3.jpg" alt=""/></a></li>

<li><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#" class="imgBox"><span></span><img src="images/img4.jpg" alt=""/></a></li>

</ul>

css垂直居中对齐,css水平垂直居中的几种方法

css代码:

<style type="text/css">

.imgWrap li{

float: left;

border: solid 1px#666;

margin: 10px 10px 0 0;

list-style: none;

border-collapse: collapse;

}

.imgWrap a{

background:#ffa url(images/gridBg.gif) repeat center;

width: 219px;

height: 219px;

display: table-cell;/*图片容器以表格的单元格形式显示*/

text-align: center;/*实现水平居中*/

vertical-align: middle;/*实现垂直居中*/

}

.imgWrap a:hover{

background-color:#dfd;

}

.imgWrap img{

border: solid 1px#66f;

vertical-align: middle;/*图片垂直居中*/

}

</style>

实现效果如下:

CSS实现水平垂直居中的几种方法介绍

CSS实现水平垂直居中对齐在CSS中实现水平居中,会比较简单。常见的,如果想实现inline元素或者inline-block元素水平居中,可以在其父级块级元素上设置text-align: center实现;如果想实现块级元素的水平居中对齐,可以设置magin: auto。而如果想实现垂直居中对齐,或许就不太容易。

以下,我总结了一些实现水平垂直居中对齐的一些方法。如果有什么不足之处,望指出。

水平垂直居中的实现可以分为两大内容,一是高度随内容自适应变化,一是固定高度。

固定高度实现水平垂直居中方法一最常用的方法是使用height+ line-height的方式,设置同样的值,配合text-align的使用,即可实现文本的水平垂直居中对齐

<div class="container">Hello World!</div>

.container{

width: 300px;

height: 300px;

line-height: 300px;

text-align: center;

border: 1px solid red;

}缺点:固定高度,无法实现两行文本的垂直居中对齐

方法二使用绝对定位的方法,配合margin负值使用。可以实现元素的水平垂直居中效果。

<div class="container">Hello World!</div>

.container{

position: absolute;

left: 50%;

top: 50%;

margin-left:-150px;

margin-top:-150px;

width: 300px;

height: 300px;

border: 1px solid red;

}当然了,可以使用CSS3的calc函数简化上面的CSS代码

.container{

position: absolute;

left: calc(50%- 150px);

top: calc(50%- 150px);

width: 300px;

height: 300px;

border: 1px solid red;

}缺点:固定高度,高度无法自适应内容。元素脱离文档流。

方法三添加空标签的方式,并且使该元素浮动,脱离文档流,避免影响其他元素的布局。

<div class="space"></div>

<div class="container">

<div class="inner">

hello world!

</div>

</div>

.space{

float: left;

height: 50%;

margin-top:-150px;

}

.container{

clear: both;

height: 300px;

border: 1px solid red;

position: relative;

}缺点:这种方式下的垂直居中需要固定高度,无法实现内容自适应高度。同时,出现多余的空div元素。

高度自适应实现水平垂直居中方法一CSS3中有transform属性,此属性下有一个translate移动函数,此函数接受两个参数。如果两个参数都为百分比值,此时会基于自身宽度和高定进行移动。此函数移动的机制同position:relative相似。

<div class="container">Hello World!</div>

.container{

position: absolute;

top: 50%;

left: 50%;

transform: translate(-50%,-50%);//自身宽度和高度的一半

border: 1px solid red;

}优点:无需定高度。高度随内容自适应。

缺点:元素脱离文档流。如果需要居中的元素已经在高度上超过了视口,那它的顶部会被视口裁切掉。

方法二我们知道,可以使用margin来实现水平居中对齐,而无法使用margin实现垂直居中的原因在于margin的百分比值是基于宽度计算的。

<div class="container">Hello World!</div>

.container{

width: 300px;

margin: 50% auto 0;

border: 1px solid red;

tarnsform: translateY(-50%);

}上面代码中,由于百分比是基于父元素(此时的父元素为body元素)的宽度计算的,所以此时的50%加上translate负值并不能实现垂直居中布局。

不过,CSS中存在一个vh(视口高度),也就相当于DOM中document.body.clientHeight或者document.documentElement.clientHeight的高度,1vh=1%,即1vh等于视口高度的1%。vh单位的浏览器兼容性问题可看vh。因此,以上代码可改为如下,即可实现水平垂直居中效果。

<div class="container">Hello World!</div>

.container{

width: 300px;

margin: 50vh auto 0;

transform: translateY(-50%);

border: 1px solid red;

}方法三CSS3中存在flex布局(伸缩布局盒模型,也叫弹性布局盒模型),对于flex熟悉的朋友来说,使用flex实现水平垂直居中是再简单不过的了。

<div class="container">

<div class="inner">

<p>hello world!</p>

</div>

</div>

.container{

display: flex;

height: 100vh;

}

.inner{

margin: auto;

}当我们使父元素display: flex时,margin: auto不仅可以水平居中,也能够实现垂直居中。这是因为auto外边距会平分水平或垂直方向上的额外空间。

当然,也可以使用justify-content: center来定义弹性项目主轴的对齐方式,align-items: center来定义弹性项目侧轴的对齐方式。

<div class="container">

<div class="inner">

<p>hello world</p>

</div>

</div>

.container{

display: flex;

justify-content: center;

align-items: center;

height: 100vh;

}方法四可以使用display: table来模拟表格,并给子元素设置display: table-cell,让其成为表格的某个单元格,同时设置vertical-align: middle,即可实现垂直居中布局

<div class="container">

<div class="inner">

hello world!

</div>

</div>

.container{

display: table;/*让div以表格的形式渲染*/

width: 100%;

border: 1px solid red;

}

.inner{

display: table-cell;/*让子元素以表格的单元格形式渲染*/

text-align: center;

vertical-align: middle;

}使用此种方式,不需要固定高度。只需要给定任意高度或者不给高度,即可实现水平垂直居中效果。

DIV CSS如何让文字垂直居中

我给div标签加了一个css想让div标签里的文字垂直居中对齐,该怎么做?

<div class="1_div">IT库</div>

.1_div{

font-size: 12px;

font-weight: bold;

color:#FF6600;

text-indent: 10px;

background-color:#DAEEF8;

line-height: 24px;

height: 24px;

}

最佳答案

在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支

持我只需做少许的CSS

Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中的确是有vertical-align属性,但是它只对(X)HTML元素中拥有valign

特性的元素才生效,例如表格元素中的<td>、<th>、<caption>等,而

像<div>、<span>这样的元素是没有valign特性的,因此使用vertical-align对它们不起作用。

CSS网页布局DIV水平居中的各种方法

一、单行垂直居中

如果一个容器中只有一行文字,对它实现居中相对比较简单,我们只需要设置它的实际高度height和所在行的高度line-height相等即可。如:

div{

height:25px;

line-height:25px;

overflow:hidden;

}

这段代码很简单,后面使用overflow:hidden的设置是为了防止内容超出容器或者产生自动换行,这样就达不到垂直居中效果了。

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN"

"">

<html xmlns="">

<head>

<title> IT库--单行文字实现垂直居中</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<style type="text/css">

body{ font-size:12px;font-family:tahoma;}

div{

height:25px;

line-height:25px;

border:1px solid#FF0099;

background-color:#FFCCFF;

}

</style>

</head>

<body>

<div>现在我们要使这段文字垂直居中显示!</div>

</body>

</html>

不过在Internet Explorer 6及以下版本中,这和方法不支持对图片设置垂直居中。

二、多行未知高度文字的垂直居中

如果一段内容,它的高度是可变的那么我们就可以使用上一节讲到的实现水平居中时使用到的最后一种方法,就是设定Padding,使上下的

padding值相同即可。同样的,这也是一种“看起来”的垂直居中方式,它只不过是使文字把<div>完全填充的一种方式而已。可以使用类

似下面的代码:

div{

padding:25px;

}

这种方法的优点就是它可以在任何浏览器上运行,并且代码很简单,只不过这种方法应用的前提就是容器的高度必须是可伸缩的。

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN"

"">

<html xmlns="">

<head>

<title>多行文字实现垂直居中</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<style type="text/css">

body{ font-size:12px;font-family:tahoma;}

div{

padding:25px;

border:1px solid#FF0099;

background-color:#FFCCFF;

width:760px;

}

</style>

</head>

<body>

<div><pre>现在我们要使这段文字垂直居中显示!

div{

padding:25px;

border:1px solid#FF0099;

background-color:#FFCCFF;

}

</pre></div>

</body>

</html>

三、多行文本固定高度的居中

在本文的一开始,我们已经说过CSS中的vertical-align属性只会对拥有valign特性的(X)HTML标签起作用,但是在CSS

中还有一个display属性能够模拟<table>,所以我们可以使用这个属性来让<div>模拟<table>

就可以使用vertical-align了。注意,display:table和display:table-cell的使用方法,前者必须设置在父元素

上,后者必须设置在子元素上,因此我们要为需要定位的文本再增加一个<div>元素:

div#wrap{

height:400px;

display:table;

}

div#content{

vertical-align:middle;

display:table-cell;

border:1px solid#FF0099;

background-color:#FFCCFF;

width:760px;

}

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN"

"">

<html xmlns="">

<head>

<title>多行文字实现垂直居中</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<style type="text/css">

body{ font-size:12px;font-family:tahoma;}

div#wrap{

height:400px;

display:table;

}

div#content{

vertical-align:middle;

display:table-cell;

border:1px solid#FF0099;

background-color:#FFCCFF;

width:760px;

}

</style>

</head>

<body>

<div id="wrap">

<div id="content"><pre>现在我们要使这段文字垂直居中显示!

div#wrap{

height:400px;

display:table;

}

div#content{

vertical-align:middle;

display:table-cell;

border:1px solid#FF0099;

background-color:#FFCCFF;

width:760px;

}

</pre></div>

</div>

</body>

</html>

这个方法应该是很理想了,但是不幸的是Internet Explorer 6

并不能正确地理解display:table和display:table-cell,因此这种方法在Internet Explorer

6及以下的版本中是无效的。嗯,这让人很郁闷!不过我们还其它的办法。

四、在Internet Explorer中的解决方案

在Internet Explorer 6及以下版本中,在高度的计算上存在着缺陷的。在Internet Explorer

6中对父元素进行定位后,如果再对子元素进行百分比计算时,计算的基础似乎是有继承性的(如果定位的数值是绝对数值没有这个问题,但是使用百分比计算的基

础将不再是该元素的高度,而从父元素继承来的定位高度)。例如,我们有下面这样一个(X)HTML代码段:

<div id="wrap">

<div id="subwrap">

<div id="content">

</div>

</div>

</div>

如果我们对subwrap进行了绝对定位,那么content也会继承了这个属性,虽然它不会在页面中马上显示出来,但是如果再对content

进行相对定位的时候,你使用的100%分比将不再是content原有的高度。例如,我们设定了subwrap的position为40%,我们如果想使

content的上边缘和wrap重合的话就必须设置top:-80%;那么,如果我们设定subwrap的top:50%的话,我们必须使用100%才

能使content回到原来的位置上去,但是如果我们把content也设置50%呢?那么它就正好垂直居中了。所以我们可以使用这中方法来实现

Internet Explorer 6中的垂直居中:

div#wrap{

border:1px solid#FF0099;

background-color:#FFCCFF;

width:760px;

height:400px;

position:relative;

}

div#subwrap{

position:absolute;

border:1px solid#000;

top:50%;

}

div#content{

border:1px solid#000;

position:relative;

top:-50%;

}

当然,这段代码只能在Internet Exlporer

6等计算存在问题的浏览器中才会有作用。(不过我不解,我查阅了很多文章,不知道是因为出处相同还是什么原因,似乎很多人都不愿意去解释Internet

Exlporer 6中这个Bug的原理,我也只是了解了一点皮毛,还要再研究)

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN"

"">

<html xmlns="">

<head>

<title>多行文字实现垂直居中</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<style type="text/css">

body{ font-size:12px;font-family:tahoma;}

div#wrap{

border:1px solid#FF0099;

background-color:#FFCCFF;

width:760px;

height:400px;

position:relative;

}

div#subwrap{

position:absolute;

top:50%;

}

div#content{

position:relative;

top:-50%;

}

</style>

</head>

<body>

<div id="wrap">

<div id="subwrap">

<div id="content"><pre>现在我们要使这段文字垂直居中显示!

div#wrap{

border:1px solid#FF0099;

background-color:#FFCCFF;

width:760px;

height:500px;

position:relative;

}

div#subwrap{

position:absolute;

border:1px solid#000;

top:50%;

}

div#content{

border:1px solid#000;

position:relative;

top:-50%;

}

</pre></div>

</div>

</div>

</body>

</html>

五、完美的解决方案

那么我们综合上面两种方法就可以得到一个完美的解决方案,不过这要用到CSS hack的知识。对于如果使用CSS Hack来区分浏览器,你可以参考这篇“简单CSS hack:区分IE6、IE7、IE8、Firefox、Opera”:

div#wrap{

display:table;

border:1px solid#FF0099;

background-color:#FFCCFF;

width:760px;

height:400px;

_position:relative;

overflow:hidden;

}

div#subwrap{

vertical-align:middle;

display:table-cell;

_position:absolute;

_top:50%;

}

div#content{

_position:relative;

_top:-50%;

}

至此,一个完美的居中方案就产生了。

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN"

"">

<html xmlns="">

<head>

<title>多行文字实现垂直居中</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<style type="text/css">

body{ font-size:12px;font-family:tahoma;}

div#wrap{

display:table;

border:1px solid#FF0099;

background-color:#FFCCFF;

width:760px;

height:400px;

_position:relative;

overflow:hidden;

}

div#subwrap{

vertical-align:middle;

display:table-cell;

_position:absolute;

_top:50%;

}

div#content{

_position:relative;

_top:-50%;

}

</style>

</head>

<body>

<div id="wrap">

<div id="subwrap">

<div id="content"><pre>现在我们要使这段文字垂直居中显示!

div#wrap{

border:1px solid#FF0099;

background-color:#FFCCFF;

width:760px;

height:500px;

position:relative;

}

div#subwrap{

position:absolute;

border:1px solid#000;

top:50%;

}

div#content{

border:1px solid#000;

position:relative;

top:-50%;

}

</pre></div>

</div>

</div>

</body>

关于本次css垂直居中对齐和css水平垂直居中的几种方法的问题分享到这里就结束了,如果解决了您的问题,我们非常高兴。

update set语句(update语句用法)css过渡效果(css艺术字效果)