[英]how can I vertically center a span in a div?
I really would like this and have been trying for hours, I have it centered horizontally in the div but just need to get it vertically centered now.. 我真的很想要这样做,并且已经尝试了几个小时,我将它在div中水平居中,但现在只需要使其垂直居中即可。
This is my css code for the span thats horizontally centred in a div. 这是我在div中水平居中的跨度的CSS代码。
.titl {
font-family:serif;
text-align:center;
font-size:35px;
font-weight: bolder;
color:#FFFFFF;
padding-top:1cm;
text-shadow: 0 0 2px #000; /* horizontal-offset vertical-offset 'blur' colour */
-moz-text-shadow: 0 0 2px #000;
-webkit-text-shadow: 0 0 2px #000;
}
This is the html of div code: 这是div代码的html:
<div class="wrapper">
<div id="Production" class="orange" style="cursor:wait">
<div id="incidentsList">
<span class="titl">Production</span>
</div>
</div>
<div id="Infrastructure" class="red" style="cursor:wait">
<div id="requestsList">
<span class="titl">Infrastructure</span>
</div>
</div>
<div id="QA" class="green" style="cursor:wait">
<div id="approvalsList">
<span class="titl">QA</span>
</div>
</div>
</div>
The css code for the orange and red etc is below: 橙色和红色等的css代码如下:
.orange {
background-image: url('../images/nFinal.jpg');
background-position: bottom;
background-repeat: no-repeat;
height: 120px;
width: 224px;
position: relative;
float: left;
margin-top: 2px;
TEXT-ALIGN: center;
}
EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT* * ** * ** * ** * ** * ** * @Dan I tried what you said but it didnt work. * * ** * ** * ** * ** ** * ** * @Dan我尝试了您所说的内容,但没有成功。 HTML: HTML:
<div id="Productiond">
<span class="titl">text</span>
</div>
CSS: CSS:
#Productiond {height: 50px}
.titl {
font-family:serif;
text-align:center;
font-size:35px;
font-weight: bolder;
color:#FFFFFF;
padding-top:1cm;
text-shadow: 0 0 2px #000; /* horizontal-offset vertical-offset 'blur' colour */
-moz-text-shadow: 0 0 2px #000;
-webkit-text-shadow: 0 0 2px #000;
height: 50px;
line-height: 50px;
}
You could try setting the height of the .titl
span to match the height of the enclosing div
tag. 您可以尝试设置.titl
跨度的高度以匹配封闭的div
标签的高度。 then setting the line-height
of the .titl
span to match that as well. 然后设置.titl
跨度的line-height
以.titl
也匹配。 this would work depending on the context you are using it in. 这将根据您在其中使用的上下文而起作用。
Try this for an example: 尝试以下示例:
HTML: HTML:
<div id='Request'>
<span class='titl'>Test Message</span>
</div>
CSS: CSS:
#Request {height: 50px}
.titl {height: 50px; line-height: 50px}
This would then display the title "Test Message" centered vertically inside the containing Div. 然后,这将显示标题“ Test Message”,垂直位于所包含的Div内部。
First, I would recommend removing the padding
, height
and line-height
declarations from .titl
. 首先,我建议从.titl
删除padding
, height
和line-height
声明。 Then, I would specify vertical-align: middle
on .titl
. 然后,我将在.titl
上指定vertical-align: middle
。 vertical-align
is meant for inline elements, and span
s are inline elements. vertical-align
用于内联元素,而span
则是内联元素。 Thus, you can use vertical-align
. 因此,您可以使用vertical-align
。
That said, I was unable to duplicate your issue when attempting it on JSFiddle and JSBin. 就是说,在JSFiddle和JSBin上尝试时,我无法复制您的问题。 If this does not solve your issue, I would recommend posting a link to one or the other. 如果这样做不能解决您的问题,建议您发布指向其中一个的链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.