2 回答

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超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貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個贊
只需將此 CSS 添加到您的.transbox
樣式中即可
https://jsfiddle.net/rjaw5nvx/
position: absolute; top: 50%; transform: translateY(-50%);
您還可以使用 Flexbox 使框垂直和水平居中,但這有點(diǎn)復(fù)雜。
- 2 回答
- 0 關(guān)注
- 178 瀏覽
添加回答
舉報(bào)