2 回答
TA贡献1876条经验 获得超5个赞
您可以使用display: flexon.container将元素放置.container在顶部。我添加了一些注释来最小化您的代码。一探究竟!
.container {
background-image: url("https://cdn.pixabay.com/photo/2015/08/25/03/51/toner-906142__480.jpg");
/* Add the background image like this */
height: 500px;
/* Give the background image the correct height */
background-repeat: no-repeat;
background-position: center;
background-size: cover;
display: flex;
/* Places content inside on top */
justify-content: center;
/* Center horizontally */
align-items: center;
/* Center vertically */
}
.transbox {
padding: 30px;
background-color: rgba(255, 255, 255, 0.5);
/* Opacity can be given to background with rgba*/
border: 1px solid black;
height: 100px;
}
<div class="container" href="service">
<div class="transbox">
<h2>
Food is essential part of our well-being
</h2>
<p>Our expertise </p>
</div>
</div>
TA贡献1818条经验 获得超3个赞
只需将此 CSS 添加到您的.transbox
样式中即可
https://jsfiddle.net/rjaw5nvx/
position: absolute; top: 50%; transform: translateY(-50%);
您还可以使用 Flexbox 使框垂直和水平居中,但这有点复杂。
- 2 回答
- 0 关注
- 86 浏览
添加回答
举报