简体   繁体   English

在图像上将左对齐的文本居中

[英]Centering left-aligned text over image

I want text over an image that's always centered. 我想在始终居中的图像上输入文字。 The following code achieves that. 以下代码实现了这一点。 However, I have a further requirement in that I'd like the text itself to be left-aligned, as opposed to centered, and I'd like to specify a width of 100px for the text so that the words wrap. 但是,我还有一个要求,就是我希望文本本身左对齐,而不是居中,并且我想为文本指定100px的宽度,以使单词自动换行。 How can I achieve this? 我该如何实现?

<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%;
}

You are setting text-align:center; 您正在设置text-align:center; in your CSS, which sets the text to center-aligned. 在CSS中,将文本设置为居中对齐。 Try changing this to text-align: left; 尝试将其更改为text-align: left; .

To set the maximum width of the text area to 100 pixels, you should probably be doing width: 100px; 要将文本区域的最大宽度设置为100像素,您可能应该设置width: 100px; instead of width:100%; 而不是width:100%; .

I made a fiddle to demonstrate the solution: 我做了一个小提琴来演示解决方案:

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

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

This is the CSS that you want. 这是您想要的CSS。 Make sure the parent container has position relative on it as well because you want it to be absolutely positioned relative to the parent not the whole page. 确保父容器也具有相对位置,因为您希望它相对于父而不是整个页面绝对定位。

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

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