简体   繁体   English

text-align:center在绝对定位跨度上不能正常工作

[英]text-align:center Not working properly on absolute positioned spans

I need to place 2 <span> inside a <div> , the first span must be placed on top, the second one on bottom, like North-South. 我需要在<div>放置2 <span> ,第一个跨度必须放在顶部,第二个跨度放在底部,如南北。

在此输入图像描述

<div>
    <span class="north">N</span>
    <span class="south">S</span>
</div>

To do this, I thought about using position:absolute; 为此,我考虑使用position:absolute; on the 2 <span> . 在2 <span>

div
{
    display:inline-block;
    width: 20px;
    position:relative;
    height:100px;
}
.north
{
    position:absolute;
    top:0;
}
.south
{
    position:absolute;
    bottom:0;
}

Now, the spans should be positioned to the left (default), to center them, I used: 现在,跨度应该位于左侧(默认),以使它们居中,我使用:

div
{
    text-align:center;
}

But I got this: 但我得到了这个:

在此输入图像描述

Check it out : http://jsfiddle.net/Zn4vB/ 看看: http//jsfiddle.net/Zn4vB/

Why is this Happening? 为什么会发生这种情况?

Note: I cannot use margins, left, right, because the contents of those spans are different, I just need to align them properly in the center. 注意:我不能使用左,右边距,因为这些跨距的内容不同,我只需要在中心正确对齐它们。

http://jsfiddle.net/Zn4vB/1/ http://jsfiddle.net/Zn4vB/1/

The issue is that once absolutely positioned, it no longer follows the document flow. 问题是,一旦绝对定位,它就不再遵循文档流程。 So the text is centered, but only inside the pink span. 所以文本居中,但只在粉红色范围内。 And since it's absolutely positioned, even if it were a div, the width would collapse. 而且由于它绝对定位,即使它是一个div,宽度也会崩溃。

The solution is to give the positioned spans a 100% width and then centering works. 解决方案是使定位的跨度为100%宽度,然后对中工作。

span
{
    background-color:pink;
    left: 0;
    width: 100%;
}

If you don't want the pink to extend the full width, then you must nest an element (eg span) inside the positioned spans and give that element the background color, as seen here: http://jsfiddle.net/Zn4vB/6/ 如果你不希望粉红色扩展整个宽度,那么你必须在定位的跨度内嵌套一个元素(例如span)并给该元素提供背景颜色,如下所示: http//jsfiddle.net/Zn4vB/ 6 /

please check if this one is the idea you want.. 请检查这个是否是您想要的想法..

div
{
    display:inline-block;
    width: 20px;
    position:relative;
    height:100px;
    border-style:solid;
}
span
{
    background-color:pink;
    width:100%;
    text-align:center;
}
.north
{
    position:absolute;
    top:0;
}
.south
{
    position:absolute;
    bottom:0;
}

You've got the positioning right. 你有正确的定位。 But <span> tags are inline elements, so you need to make them display as block-level elements with display: block; <span>标签是内联元素,因此您需要将它们显示为块级元素,并使用display: block; and then explicitly declare their width with width: 100%; 然后用width: 100%;明确声明它们的width: 100%; .

They will inherit the text-align property from your style rules on the <div> so the text will be in the center. 它们将从<div>上的样式规则继承text-align属性,因此文本将位于中心。

I've updated your code here: http://jsfiddle.net/robknight/Zn4vB/5/ 我在这里更新了你的代码: http//jsfiddle.net/robknight/Zn4vB/5/

you can use transform to solve this problem 你可以使用transform来解决这个问题

 div
{
    display:inline-block;
    width: 20px;
    position:relative;
    height:100px;
    border-style:solid;
    text-align:center;
}
span
{
    background-color:pink;
}
.north
{
    position:absolute;
    top:0;
  transform: translateX(-50%);
  -webkit-transform: translateX(-50%);
}
.south
{
    position:absolute;
    bottom:0;
  transform: translateX(-50%);
  -webkit-transform: translateX(-50%);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM