嗯,這是因爲% percentage
這是真的。默認div width
值爲auto
,所以現在如果您要在% percentage
中定義您的子元素,則在該情況下,您的父母根據子元素的寬度在% percentage
中指定子元素的寬度,即子元素的100%
。
此處我們將50% width
分配到child element
和parent
沒有任何寬度。但它會自動分配,因爲孩子需要是家長的50%。而在px
都是一樣的。
/*Using Percentage*/
.navigation {
\t \t \t \t position: absolute;
background:#111;
\t \t \t }
\t \t \t .navigation > img {
\t \t \t \t height: auto;
\t \t \t \t max-width: 50%;
\t \t \t }
/*using pixels*/
\t .previous {
\t \t \t \t position: absolute;
background:#111;
\t \t \t }
\t \t \t .previous > img {
\t \t \t \t height: auto;
\t \t \t \t width: 100px;
\t \t \t }
<div class="navigation"><img src="http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/MinorCatImages/033256AM.png">
</div>
<br><br><br><br><br>
<div class="previous">
<img src="http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/MinorCatImages/033256AM.png">
</div>
給圖像顯示':block'可能? – vsync
css規則不會上流。使您的img 50%寬度不會神奇地使父母也小50%。 –
當你設置'position:absolute;'你也必須設置'width:100%',否則div會摺疊以填充最小寬度。 – Toby