2017-08-12 36 views
3

我希望我的背景圖像能夠在div內響應。滾動時它具有視差效果。我已經嘗試在media queries中自定義背景,但沒有希望它只調整高度,並且在移動設備寬度中不包含整個頁面。有人能給我一個線索來解決這個問題嗎?我新的HTML和CSS。對於DIV在DIV響應中製作背景圖像

的html代碼:

<div class="parallax"></div> 

CSS代碼:

.parallax { 
    height: 502px; 
    background-image: url(../img/back2.png); 
    background-attachment: fixed; 
    background-position: -70px 80px; 
    background-repeat: no-repeat; 
    background-size: cover; 
} 

@media screen and (min-width: 270px) and (max-width: 320px){ 
.parallax { 
    max-height: 300px; 
    background-image: url(../img/back2.png); 
    background-attachment: fixed; 
    background-position: -70px 40px; 
    background-repeat: no-repeat; 
    background-size: cover; 
} 
} 

回答

1

您應該使用屬性:如果寬度屬性設置爲100%background-size:100% auto;

,圖像會響應和擴大和縮小。

請訪問以下鏈接瞭解更多信息:w3schools

+0

三江源兄弟。有效! – nethkennnnn