【发布时间】:2023-04-09 12:39:01
【问题描述】:
在我的代码中,我有一个带有背景图像的部分元素,我想在上面放置一个 div 元素,例如 this
但我不知道如何将该文本放在图像上。它只是粘在上面: 这是我的html和css:
* {
background-color: black;
font-family: Helvetica;
color: seashell;
opacity: 0.9;
font-size: 22px;
}
#mission {
margin: 0 auto;
width: 1200px;
height: 700px;
background-image: url(https://content.codecademy.com/courses/freelance-1/unit-4/img-mission-background.jpg);
background-repeat: no-repeat;
}
#mission div {
background-color: black;
text-align: center;
}
<section id="mission">
<div>
<h2>Our Mission</h2>
<h4>Handpicked, Artisanally Curated, Free Range, Sustainable, Small Batch, Fair Trade, Organic Tea</h4>
</div>
</section>
【问题讨论】:
-
唯一的方法是使用flexbox。当您不知道元素的高度时,这也有效。它是supported by all modern browsers,甚至是 Internet Explorer。
-
@Pieterjan 我不知道你是什么意思。我应该将
display: flex;分配给<section>吗? -
通常是的。
display:flex;flex-direction:column;justify-content:center; -
2021,我们很多都没有头绪,不用担心。