【问题标题】:Centering span near an image with the respect to the wrapper div相对于包装器 div 在图像附近居中跨度
【发布时间】:2015-01-05 10:35:09
【问题描述】:

如何在不使用表格的情况下将 span 元素垂直和水平居中?

我需要左边的图片,然后在包装器div 的中心对齐跨度。 span 元素不应在img 和右侧之间对齐,而是整个div 本身。

Fiddle

<div style="height: 100px; background-color:black;color:white;">
  <img style="height:100px;" src="http://www.kiplinger.com/quiz/business/T049-S001-test-your-start-up-know-how/images/all-small-businesses-have-to-be-incorporated1.jpg">
  <span class="hejsa">hej du</span>
</div>

【问题讨论】:

  • 小提琴中没有跨度元素??
  • 这个问题已经在 SO 上被问过好几次了。你试过了吗?
  • 尝试了一堆,似乎无法让它工作,只是寻找一个直接的工作代码 sn-p 可以让我这样做,而不需要大的位置:变化。
  • 你是这个意思吗? jsfiddle.net/hashem/e8ESb/627
  • jsfiddle.net/e8ESb/628@RasmusHjorthLudeking

标签: html css vertical-alignment


【解决方案1】:

试试这个

css

div{text-align:center;line-height:100px;position:relative;}
img{position:absolute;left:0}

html

<div style="height: 100px; background-color:black;color:white;"><img style="height:100px;" src="http://www.kiplinger.com/quiz/business/T049-S001-test-your-start-up-know-how/images/all-small-businesses-have-to-be-incorporated1.jpg"><span class="hejsa">hej du</span></div>

fiddle

【讨论】:

  • 请注意,align 属性自 HTML4.01 起已弃用,自 HTML5 起已过时。
  • @AbdulBasit 我把你的代码放在一个代码 sn-p 中,它可以在 Stack Overflow 上运行——你为什么把它移到一个 jsfiddle 中?
  • 但是.. 这使得 span 元素相对于左边缘和图片之间的空间对齐。
  • @nathan 不小心,我们都在同时编辑,我怎么能再去呢?
  • @AbdulBasit 哦,好的,那很好:) 只需回滚编辑(单击“已编辑 x 分钟前”,然后使用代码 sn-p 在我的编辑上单击“回滚”)。有关代码 sn-ps 的更多信息,请查看this blog post
【解决方案2】:

正如 OP 所说:

我需要以整个站点为中心的跨度。

一种选择可能是定位&lt;span&gt; 元素absolutely 并将文本水平和垂直对齐到中心,如下所示:

方法#1:

使用top/lefttransform 属性:

.wrapper {
    position: relative;
    height: 100px; background-color:black; color:white;
}

.wrapper > span.hejsa {
    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%);
}
<div class="wrapper">
    <img style="height:100px;" src="http://www.kiplinger.com/quiz/business/T049-S001-test-your-start-up-know-how/images/all-small-businesses-have-to-be-incorporated1.jpg" />
    <span class="hejsa">
        Lorem ipsum dolor sit amet...
    </span>
</div>

值得注意的是CSS transformnot supported in IE8 and olders (这不是他/她在comments 中提到的OP 的关注点)

方法#2:

通过top/right/bottom/left属性扩展绝对定位的&lt;span&gt;元素:

.wrapper {
    position: relative;
    height: 100px; background-color:black; color:white;
}

/* .wrapper > img { position: relative; z-index: 1; } */ /* optional */

.wrapper > span.hejsa {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    line-height: 100px;
    text-align: center;
}
<div class="wrapper">
    <img style="height:100px;" src="http://www.kiplinger.com/quiz/business/T049-S001-test-your-start-up-know-how/images/all-small-businesses-have-to-be-incorporated1.jpg" />
    <span class="hejsa">
        Lorem ipsum dolor sit amet...
    </span>
</div>

这种方法有更广泛的浏览器支持,但是它的一个缺点是文本应该是多行的。

【讨论】:

  • 谢谢你,我的人,你明白了!赞成并接受。祝你有美好的一天!
  • 只是为了知识,@hashem 既然可以用 css 完成,为什么还要使用 transform 和 css3 属性?也可以在旧浏览器上运行?
  • @AbdulBasit 刚刚注意到你的回答 +1 它比我的好。但是我不确定从正常流程中删除图像是否适合 OP,因为它可能会影响其他元素(如果有)。如果 OP 分别接受你的答案,我会删除我的答案。
  • 不,没关系,我只是从你的代码中学习,问题解决了,很好:)
猜你喜欢
  • 2019-09-25
  • 1970-01-01
  • 2021-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多