1
我有一個結構,在這兩個視圖中,只有桌面視圖和移動視圖分別具有固定高度的圖像。爲不同的桌面和移動視圖調整圖像大小
在移動視點圖像出來拉伸。 我已經應用了一些解決方案,從上,下,左,右裁剪圖像,在我的情況下,我不想要。 我應該如何繼續,以便圖像不會在任何視圖中伸展?
我有一個結構,在這兩個視圖中,只有桌面視圖和移動視圖分別具有固定高度的圖像。爲不同的桌面和移動視圖調整圖像大小
在移動視點圖像出來拉伸。 我已經應用了一些解決方案,從上,下,左,右裁剪圖像,在我的情況下,我不想要。 我應該如何繼續,以便圖像不會在任何視圖中伸展?
假設你想有一個固定的佈局(如你的圖像中),你可以使用background-image
/background-size
,並設置它像這樣
background: url(...) no-repeat; /* the shorthand property */
background-size: cover;
background-position: center center;
的cover
值使圖像總是充滿元件,而不將其拉伸,並與background-position
可以控制圖像是否應居中,左,右,底部,頂部對齊
div {
height: 200px;
width: 500px;
background: url(http://lorempixel.com/300/400/animals/4) no-repeat;
background-size: cover;
background-position: center center;
border: 1px solid black;
}
div ~ div {
height: 200px;
width: 200px;
}
div ~ div ~ div {
height: 500px;
width: 200px;
background-position: left center;
}
<div></div>
<div></div>
<div></div>
如果您不想裁剪,則可以使用background-size: contain
代替。
div {
height: 200px;
width: 500px;
background: url(http://lorempixel.com/300/400/animals/4) no-repeat;
background-size: contain;
background-position: left bottom;
border: 1px solid black;
}
div ~ div {
height: 200px;
width: 200px;
}
div ~ div ~ div {
height: 500px;
width: 200px;
}
<div></div>
<div></div>
<div></div>
更新基於評論,在與背景尺寸的img
html, body {
margin: 0;
height: 100%;
}
img {
height: 40%;
width: auto;
}
<img src="http://lorempixel.com/200/400/animals/4" alt="">
<img src="http://lorempixel.com/300/300/animals/4" alt="">
<img src="http://lorempixel.com/400/200/animals/4" alt="">
第一溶液中使用百分比高度(相對於主體):封面是相當不錯的 - 唯一的問題是它收穫的形象。 不能使用包含,因爲會顯示一些額外的空間。 感謝您的這一點,你會有任何其他的解決方案。 – pdjinn
@pdjinn然後我不確定你想要如何,作爲你的作物,縮放或拉伸圖像以適應某種佈局? ..如果沒有,那麼你只需給'img'元素設置一個固定的高度並將寬度設置爲auto。 – LGSon
這就是我一直以固定的高度和寬度進行拍攝的方式:100%,因爲我的佈局非常靈敏,所以可以延伸圖像。有沒有我可以使用多個圖像申請的解決方案。寬度:自動將再次顯示額外的空間 – pdjinn