在本文中,我们将为您详细介绍纯css使div垂直居中,div垂直,div居中的方法的相关知识,并且为您解答关于css让div垂直居中的疑问,此外,我们还会提供一些关于7种div垂直居中的方法总结、cs
在本文中,我们将为您详细介绍纯css使div垂直居中,div垂直,div居中的方法的相关知识,并且为您解答关于css让div垂直居中的疑问,此外,我们还会提供一些关于7种div垂直居中的方法总结、css div垂直居中解决方案、css 文本和div垂直居中方法汇总、css:div垂直居中的有用信息。
本文目录一览:纯css使div垂直居中,div垂直,div居中的方法(css让div垂直居中)
首先编写一个简单的HTML代码,设置一个父div类名为BoxFather,再设置一个子div类名为Box1。HTML代码如下:
<div class="BoxFather"> <div class="Box1"></div> </div>
下面使用div盒子里面要有内容撑开,HTML代码如下:
<divhttps://www.jb51.cc/tag/Box/" target="_blank">BoxFather">
<divhttps://www.jb51.cc/tag/Box/" target="_blank">Box1">
测试内容
</div>
</div>
1.div居中的方法
(1)父div不定宽高,子div定宽高
第一种方式:
<style> .Box1{ background-color: red; width: 300px; height: 200px; margin: 0 auto; } </style>
第二种方式:
<style> .BoxFather{ position: relative; } .Box1{ width: 300px; height: 200px; background-color: red; position: absolute; left: 50%; -webkit-transform: translate(-50%); -moz-transform: translate(-50%); -ms-transform: translate(-50%); -o-transform:translate(-50%) ; transform:translate(-50%); } </style>
第三种方式:
<style> .BoxFather{ display: flex; justify-content: center; } .Box1{ background-color: red; width: 300px; height: 200px; } </style>
第四种方法:
<style> .BoxFather{ text-align: center; } .Box1{ display: inline-block; background-color: red; width: 300px; height: 200px; } </style>
第五种方法:
<style> .BoxFather{ position: relative; } .Box1{ position: absolute; left:0; top:0; right:0; bottom:0; margin:auto; background-color: red; width: 300px; height: 200px; } </style>
第六种:
<style> .BoxFather{ position: relative; } .Box1{ position: absolute; left:50%; top:50%; width: 300px; height: 200px; margin-left: -150px; margin-top: -100px; background-color: red; } </style>
第七种:
<style> .BoxFather{ display: flex; justify-content:center; } .Box1{ width: 300px; height: 200px; background-color: red; } </style>
(2)父div不定宽高,子div不定宽高 。(注意div盒子里面要有内容撑开)
第一种方法:
<style> .BoxFather{ position: relative; } .Box1{ background-color: red; position: absolute; left: 50%; -webkit-transform: translate(-50%); -moz-transform: translate(-50%); -ms-transform: translate(-50%); -o-transform:translate(-50%) ; transform:translate(-50%); } </style>
第二种方法:
<style> .BoxFather{ display: flex; justify-content:center; } .Box1{ background-color: red; } </style>
2.div垂直居中
(1)父div不定宽高,子div不定宽高
第一种方法:
<style> .BoxFather{ display: flex; justify-content:center; align-items: center; height: 200px; border:1px solid #ccc; } .Box1{ background-color: red; } </style>
(2)父div定高,子div定宽高
第一种方法:
<style> .BoxFather{ position: relative; height: 500px; border:1px solid #ccc; } .Box1{ width: 300px; height: 200px; background-color: red; position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%,-50%); -moz-transform: translate(-50%,-50%); -ms-transform: translate(-50%,-50%); -o-transform:translate(-50%,-50%) ; transform:translate(-50%,-50%); } </style>
第二种方法:
<style> .BoxFather{ } .Box1{ position: fixed; left:0; top:0; right:0; bottom:0; margin:auto; background-color: red; width: 300px; height: 200px; } </style>
(3)父div定宽高,子div不定宽高(需要内容撑开)
第一种方法:
<style> .BoxFather{ width: 300px; height: 200px; background-color: red; display: table-cell; vertical-align: middle; text-align: center; } .Box1{ display: inline-block; } </style>
第二种方法:
<style> .BoxFather{ display: flex; justify-content:center; align-items: center; height: 200px; border:1px solid #ccc; } .Box1{ background-color: red; } </style>
3.div垂直
文字垂直,HTML代码如下:
<divhttps://www.jb51.cc/tag/Box/" target="_blank">BoxFather">
测试内容
</div>
第一种方法:
.BoxFather{ height: 100px; line-height: 100px; background-color: red; border: 1px solid #ccc; }
第二种方法:
.BoxFather{ height: 100px; background-color: red; display:table-cell; vertical-align:middle; }
div垂直的内容可复用‘div垂直居中’的内容是内容垂直
记录一下,以后还会陆续添加。
7种div垂直居中的方法总结
在我们日常web开发中,会经常使用到css和div进行布局,使其页面更为的美观,以及体验效果更佳,那么怎么使div以各种居中方式展现出来呢,下面我们就来详细总结div垂直居中的方法。
CSS实现div垂直居中的方法:
1.CSS教程之div垂直居中的多种方法
在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中的确是有vertical-align属性,但是它只对(X)HTML元素中拥有valign特性的元素才生效,例如表格元素中的
、 、等,而像、这样的元素是没有valign特性的,因此使用vertical-align对它们不起作用。
2.CSS文本和div垂直居中方法示例分析
在样式布局中,我们经常碰到需要将元素居中。通过css实现元素的水平居中较为简单:对文本,只需要对其父级元素设置text-align: center;,而对p等块级元素,只需要设置其left和right的margin值为auto。要实现元素的垂直居中,有人会想到css中的vertical-align属性,但是它只对拥有valign特性的元素才生效,例如表格元素中的
、 、等,而像、这样的元素是没有valign特性的,因此使用vertical-align对它们不起作用。因此我们需要通过别的方法去实现元素的垂直居中,下面我总结了几种了常用垂直居中方法。
3.【前台】css控制DIV垂直居中
由于这个时候浏览器会以图中绿点所示的位置为默认其实位置。要想让内容居中就要将margin-left和margin-top分别设置为负的宽度一半和搞度一半。
二、div水平垂直居中:
1.利用CSS实现div水平垂直居中
实现居中的方案有很多,这篇文章主要介绍了纯CSS使用absolute配合margin的方案,margin-top为-(height / 2),margin-left为-(width / 2)。当然也可以不用margin,即top为calc(100% - height) / 2,left为calc(100% - width) / 2,但是建议可以不用calc()就不要用。
2.css 实现DIV水平垂直居中于屏幕
css如何将p实现全屏水平垂直居中,本章节介绍一下如何将一个p元素在整个网页内实现水平垂直居中效果,代码是最有说服力的,直接看代码。
div垂直居中相关问答:
1.css3 - 如何让div中的内容水平居中,垂直居中?
2.css - UC手机浏览器下如何让div水平垂直居中?
【div垂直居中相关文章】
1.css图片居中:css图片上下左右居中(水平和垂直居中)
2.div居中:最全的div居中方法总结
3.CSS居中:最全面的CSS居中方法大全
以上就是7种div垂直居中的方法总结的详细内容,更多请关注php中文网其它相关文章!
css div垂直居中解决方案
总结
以上是小编为你收集整理的css div垂直居中解决方案全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
css 文本和div垂直居中方法汇总
总结
以上是小编为你收集整理的css 文本和div垂直居中方法汇总全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
css:div垂直居中
在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS
Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中的确是有vertical-align属性,但是它只对(X)HTML元素中拥有valign特性的元素才生
效,例如表格元素中的<td>、<th>、<caption>等,而像<div>、<span>这样的元素是没有valign特性的,因此使用vertical-align对它们不起
作用。
相关教程:div水平居中的N种方法
一、单行垂直居中
如果一个容器中只有一行文字,对它实现居中相对比较简单,我们只需要设置它的实际高度height和所在行的高度line-height相等即可。
如:
div {
height:25px;
line-height:25px;
overflow:hidden;
}
这段代码很简,后面使用overflow:hidden的设置是为了防止内容超出容器或者产生自动换行,这样就达不到垂直居中效果了。更多CSS教
程。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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 {
height:25px;
line-height:25px;
border:1px solid #FF0099;
background-color:#FFCCFF;
}
</style>
</head>
<body>
<div>现在我们要使这段文字垂直居中显示!</div>
</body>
</html>
二、多行未知高度文字的垂直居中
如果一段内容,它的高度是可变的那么我们就可以使用上一节讲到的实现水平居中时使用到的最后一种方法,就是设定Padding,使上下的
padding值相同即可。同样的,这也是一种“看起来”的垂直居中方式,它只不过是使文字把<div>完全填充的一种访求而已。可以使用类似下
面的代码:
div {
padding:25px;
}
这种方法的优点就是它可以在任何浏览器上运行,并且代码很简单,只不过这种方法应用的前提就是容器的高度必须是可伸缩的。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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>现在我们要使这段文字垂直居中显示! Webjx.Com
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
</html>
p.s. 垂直居中vertical-align的值是middle,而水平居中align的值是center,虽然同是居中但关键字不同
总结
以上是小编为你收集整理的css:div垂直居中全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
今天关于纯css使div垂直居中,div垂直,div居中的方法和css让div垂直居中的讲解已经结束,谢谢您的阅读,如果想了解更多关于7种div垂直居中的方法总结、css div垂直居中解决方案、css 文本和div垂直居中方法汇总、css:div垂直居中的相关知识,请在本站搜索。
本文标签: