【问题标题】:Centering left-aligned text over image在图像上居中左对齐文本
【发布时间】:2015-03-13 22:50:27
【问题描述】:

我希望在始终居中的图像上显示文字。下面的代码实现了这一点。但是,我还有一个进一步的要求,即我希望文本本身左对齐,而不是居中,并且我想为文本指定 100px 的宽度,以便文字换行。我怎样才能做到这一点?

<div id="typewritter-text">
    <span id='typewritter'>Lorem Ipsum</span>
</div>

#typewritter-text {
    color: white;
    position: absolute;
    left: -65px;
    position:absolute;
    text-align:center;
    top: 400px;
    width: 100%;
}

【问题讨论】:

  • 尝试设置text-align: left;而不是text-align: center;width: 100px;而不是width: 100%;。如果可行,我会将其发布为答案。

标签: html css


【解决方案1】:

您在 CSS 中设置 text-align:center;,这会将文本设置为居中对齐。尝试将其更改为 text-align: left;

要将文本区域的最大宽度设置为 100 像素,您可能应该使用 width: 100px; 而不是 width:100%;

【讨论】:

  • 这个解决方案的问题是它不会将 span 本身居中,尽管文本居中...
  • 好的,也许你也可以尝试设置left: 0px;
【解决方案2】:

我做了一个小提琴来演示解决方案:

http://jsfiddle.net/Ar14/gg076ew0/1/

#typewritter-text {
    top: 0;
    left: 0;
    bottom: 0;
    margin: auto;
    width: 100px;
    height: 100px;
    position: absolute;
}

这是您想要的 CSS。确保父容器也具有相对位置,因为您希望它相对于父容器而不是整个页面绝对定位。

【讨论】:

  • 这不起作用,因为它没有与图像重叠,因为这里的父级位置是相对的。
  • 如果您希望它重叠,请尝试定义 z-index:z-index: 1
猜你喜欢
  • 2012-06-12
  • 1970-01-01
  • 2019-10-01
  • 2017-10-20
  • 2023-04-04
  • 1970-01-01
  • 2016-12-13
  • 2018-03-16
  • 2014-03-29
相关资源
最近更新 更多