我有一個HTML標記,其中的印刷內容是<p>
-tags。在這些標籤之間,我想放置一些圖像。這些圖像的大小始終相同:100%寬,50%高。
爲了避免一些扭曲,我設置了一個<div>
-tag這個大小,並將圖像設置爲封面大小的背景圖像。如何確定沒有內容的相對div?
此<div>
不包含任何內容,但背景圖像除外。所以我的尺碼無法使用,因爲我無法將其設置爲position: absolute/fixed;
,因此它不適合<p>
標籤。
那麼我怎麼能夠在不損失合適的情況下確定空的 div?
的HTML標記:
<div class="container">
<section class="about">
<div class="content">
<p>Lorem ipsum dolor</p>
<div class="img"></div>
<p>Lorem ipsum dolor</p>
</div>
</section>
</div>
而CSS樣式
.container,
.container > section{
position: fixed;
height: 100%;
width: 100%;
}
.container > section{
overflow:auto;
}
.container > section > .content > p{
padding: 5% 15% 5% 15%;
font-family: Arial, sans-serif;
font-size: 1.8em;
line-height: 1.5;
}
.container > section > .content > .img{
width:100%;
height:50%;
background: url(http://www.hdcarwallpapers.com/walls/widescreen_lamborghini_lp710-wide.jpg) no-repeat center center;
background-size:cover;
}
而一個CODEPEN DEMO
嗯...... IMO這不起作用,因爲.img的高度是一個百分比。所有父標籤必須具有百分比的高度。 – 2014-03-03 15:54:12
似乎您正在尋找一個響應式容器,以根據* width *設置* height *。如果是這樣,你可以參考[我的答案](http://stackoverflow.com/questions/18516317/vertically-align-an-image-inside-a-div-with-responsive-height/18516474#18516474)。看看第二部分:* Responsive Container * –