简体   繁体   English

如何在不移动下方图像的情况下将文本居中放置在背景图像上? HTML/CSS

[英]How do I center text over a background image without moving the image underneath? HTML/CSS

I have an image that is under the background image property and I have some lines of text within that div class.我有一个位于背景图像属性下的图像,并且在该 div 类中有一些文本行。 I keep using the margin top property to move the text to center it over the image but it also adds a margin to the image.我一直使用 margin top 属性移动文本使其在图像上居中,但它也为图像添加了一个边距。 I tried to use the padding prop but it also stretches the background I put on.我尝试使用 padding 道具,但它也拉伸了我穿上的背景。 How do I center this text without it adding that margin to the image underneath it?如何在不将边距添加到其下方的图像的情况下将此文本居中? Any help appreciated.任何帮助表示赞赏。

You can center text using text-align: center , margin: auto .您可以使用text-align: center , margin: auto text-align: center Also, using display: flex; justify-content: center; align-items: center;另外,使用display: flex; justify-content: center; align-items: center; display: flex; justify-content: center; align-items: center; . . If element has absolute positioning it entails another solution.如果元素具有绝对定位,则需要另一种解决方案。 Perhaps, you should align your div, but... Need to clarify with code example for better understanding.也许,您应该对齐您的 div,但是...需要通过代码示例进行澄清以便更好地理解。

Try something like this尝试这样的事情

position: absolute;
top: 50%;
left: 50%;

In css file在 css 文件中

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

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