我已經做了如下佈局:CSS:如何使背景圖像顯示一個div內沒有設置固定的高度
html,
body {
height: 100%;
}
.header {
height: 120px;
width: 100%;
background-image: url(http://lorempixel.com/500/120/);
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.3.0/css/foundation.min.css" rel="stylesheet" />
<div class="row">
<div class="columns small-12 header"></div>
</div>
它的工作原理,因爲我使用了一個高度 - 屬性具有固定的像素值。
如果一個刪除該圖像中消失:
html,
body {
height: 100%;
}
.header {
width: 100%;
background-image: url(http://lorempixel.com/500/120/);
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.3.0/css/foundation.min.css" rel="stylesheet" />
<div class="row">
<div class="columns small-12 header"></div>
</div>
一個與使用固定高度的一個問題是反應性不能被實現。
如何使背景不必使用固定的高度? 或者我應該回到使用img標籤嗎?
那裏的高度是由圖像大小本身決定的。