2016-10-07 723 views
1

我遇到了背景圖像問題。我的背景圖像具有全寬,但沒有響應。在我的代碼中,我使用Bootstrap。我希望這不是問題。響應式背景圖像

圖片在991 px處有或沒有媒體查詢時消失。此外,使圖像全寬的唯一選擇是使用背景大小的封面。

當我使用寬度100%(這會使其響應)不起作用。然後圖像分割。 max-width:100%一樣。我不知道爲什麼它不起作用。

我的代碼:

body, html { 
 
    padding:0px; 
 
    margin:0px; 
 
    font-family: 'TheSans Swisscom' !Important; 
 
} 
 
    
 
@media only screen and (min-width: 991px) { 
 
    .col-md-3{ 
 
     width: calc(25% - 10px) !Important; 
 
     margin-right:5px; 
 
     margin-left:5px; 
 
     margin-top: 10px; 
 
     background-color: white; 
 
     position:relative !Important; 
 
     font-family: TheSans Swisscom; 
 
     display: block; 
 
     padding:0px !Important; 
 
    } 
 
} 
 
    
 
a:link { 
 
    color: black; 
 
}  
 
    
 
a:visited { 
 
    color: Black; 
 
}  
 
    
 
.a { 
 
    padding: 70px; 
 
    position: relative; 
 
    right: -5%; 
 
} 
 

 
a { 
 
    color: black !important; 
 
} 
 

 
.center-block { 
 
    width: 100%; 
 
} 
 

 
h2 { 
 
    font-size:30px; 
 
    margin: 0 0 auto; 
 
    width: 9em; 
 
    text-align: center; 
 
} 
 

 
.ptags { 
 
    line-height: 1.2; 
 
    padding: 5px; 
 
} 
 

 
.button { 
 
    margin-right: 10px; 
 
    height: 45px; 
 
    width: 45px; 
 
    background-color: black; 
 
    font-size: 60px; 
 
    color: white; 
 
    text-align: center; 
 
    line-height: 45px; 
 
    bottom: 10px; 
 
    cursor: pointer; 
 
    z-index: 1; 
 
    font-family: TheSansSwisscom; 
 
    position: relative; 
 
    right: -98%; 
 
    top: -308px; 
 
} 
 

 
.ktm { 
 
    text-align: center; 
 
} 
 

 
.h1{ 
 
    font-size: 36px; 
 
    text-align: center; 
 
} 
 

 
h2 { 
 
    font-size:30px; 
 
    margin: 0 0 auto; 
 
    width: 9em; 
 
    text-align: center; 
 
} 
 

 
.img-center{ 
 
    display: block; 
 
    margin:0 auto; 
 
} 
 

 
a:link { 
 
    color: black; 
 
} 
 

 
.row{ 
 
    display:block; 
 
} 
 

 
@media only screen and (min-width: 991px){ 
 
    .img { 
 
     background-image: url('https://pbs.twimg.com/profile_images/3396462371/53b106cad4de869739517b1ff5d75429.jpeg'); 
 
     background-size: cover; 
 
     background-repeat: no-repeat; 
 
    } 
 
} 
 

 
.container{ 
 
    width: 100% !important; 
 
    padding: 100px; 
 
} 
 

 
.cardContainer { 
 
    width: 1200px; 
 
    position: relative; 
 
    margin-left: calc(50% - 600px); 
 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 

 
    <section class="container img"> 
 
     <h1 class="h1 a"><a name="Details">Details</a></h1> 
 
     <div class="button"> 
 
      <p>+</p> 
 
     </div> 
 
     <div class="cardContainer"> 
 
      <div class="col-md-3 col-xs-12"> 
 
       <img class="center-block"src="http://farm8.static.flickr.com/7411/8725728890_b056a881c5_m.jpg" alt="thirdimage"> 
 
       <h2 class="Details">Details</h2> 
 
       <p class="ptags">Bacon ipsum dolor amet capicola bacon tenderloin, venison landjaeger porchetta ham prosciutto frankfurter. Turkey ball tip jowl t-bone pastrami boudin salami, doner fatback cupim swine chicken cow biltong hamburger. Ham kevin hamburger meatloaf turducken shankle rump. Shankle andouille venison, kielbasa pork belly jerky biltong ham hock.</p> 
 
      </div> 
 
      <div id=cardPrototype class="col-md-3 col-xs-12"> 
 
       <img class="center-block"src="http://farm2.static.flickr.com/1577/26053634152_9a7d5b3580_m.jpg" alt="thirdimage"> 
 
       <h2>Details</h2> 
 
       <p class="ptags">Bacon ipsum dolor amet capicola bacon tenderloin, venison landjaeger porchetta ham prosciutto frankfurter. Turkey ball tip jowl t-bone pastrami boudin salami, doner fatback cupim swine chicken cow biltong hamburger. Ham kevin hamburger meatloaf turducken shankle rump. Shankle andouille venison, kielbasa pork belly jerky biltong ham hock.</p> 
 
      </div> 
 
      <div class="col-md-3 col-xs-12"> 
 
       <img class="center-block"src="http://farm8.static.flickr.com/7250/7445511584_9079770764_m.jpg" alt="thirdimage"> 
 
       <h2>Details</h2> 
 
       <p class="ptags">Bacon ipsum dolor amet capicola bacon tenderloin, venison landjaeger porchetta ham prosciutto frankfurter. Turkey ball tip jowl t-bone pastrami boudin salami, doner fatback cupim swine chicken cow biltong hamburger. Ham kevin hamburger meatloaf turducken shankle rump. Shankle andouille venison, kielbasa pork belly jerky biltong ham hock.</p> 
 
      </div> 
 
      <div class="col-md-3 col-xs-12"> 
 
       <img class="center-block"src="http://farm9.static.flickr.com/8540/8668499106_36a8b6cab8_m.jpg" alt="thirdimage"> 
 
       <h2>Details</h2> 
 
       <p class="ptags">Bacon ipsum dolor amet capicola bacon tenderloin, venison landjaeger porchetta ham prosciutto frankfurter. Turkey ball tip jowl t-bone pastrami boudin salami, doner fatback cupim swine chicken cow biltong hamburger. Ham kevin hamburger meatloaf turducken shankle rump. Shankle andouille venison, kielbasa pork belly jerky biltong ham hock.</p> 
 
      </div> 
 
     </div> 
 
    </section>

+1

分享你的HTML問題。你有沒有嘗試添加html類'img-responsiv''? – ZombieChowder

+0

「在我的代碼中,我也使用bootrap」我認爲他指的是Bootstrap – ZombieChowder

+0

也許你想要[this](https://jsfiddle.net/dofq9kwr/)這樣的東西? – Huelfe

回答

-2

可以通過使響應包含圖像的標籤讓你的背景圖片

.img { 
 
    display: block; 
 
    width: 100% \9; 
 
    height: auto; 
 
    width: 100%; 
 
}
<div class="img"> 
 

 
    
 

 

 

 
</div>

+0

這將使它成爲一個img標籤,作者需要背景 – Raimonds

0

我不知道這是你想要的,但看看。

.img { 
 
    background-image: url('https://pbs.twimg.com/profile_images/3396462371/53b106cad4de869739517b1ff5d75429.jpeg'); 
 
    background-repeat: no-repeat; 
 
    background-size: cover; 
 
} 
 

 
.center-block{ 
 
    width:100%; 
 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 
<section class="container img"> 
 
    <h1 class="h1 a"><a name="Details">Details</a></h1> 
 
    <div class="button"> 
 
    <p>+</p> 
 
    </div> 
 

 
    <div class="cardContainer"> 
 
    <div class="col-md-3 col-xs-12"> 
 
     <img class="center-block" src="http://placehold.it/220x150" alt="thirdimage"> 
 
     <h2 class="Details">Details</h2> 
 
     <p class="ptags">ad adaésdkf awpjd aiwj dwp diw aapdfjap wkd wo dakow dmaow d aodw dja wo lda ow dakldow daowdkwo dkaqodejqd dkaadfa ddfjkladfhi diwj diwdja idjdjwi adijq asdfiw djkafhi alkfwiwj diwdja idjaidfl qod qd adsfadsf (unknown language)</p> 
 
    </div> 
 
    <div id=cardPrototype class="col-md-3 col-xs-12"> 
 
     <img class="center-block" src="http://placehold.it/240x150" alt="thirdimage"> 
 
     <h2>Details</h2> 
 
     <p class="ptags">ad adaésdkf awpjd aiwj dwp diw aapdfjap wkd wo dakow dmaow d aodw dja wo lda ow dakldow daowdkwo dkaqodejqd dkaadfa ddfjkladfhi diwj diwdja idjdjwi adijq asdfiw djkafhi alkfwiwj diwdja idjaidfl qod qd adsfadsf (unknown language)</p> 
 
    </div> 
 
    <div class="col-md-3 col-xs-12"> 
 
     <img class="center-block" src="http://placehold.it/240x150" alt="thirdimage"> 
 
     <h2>Details</h2> 
 
     <p class="ptags">ad adaésdkf awpjd aiwj dwp diw aapdfjap wkd wo dakow dmaow d aodw dja wo lda ow dakldow daowdkwo dkaqodejqd dkaadfa ddfjkladfhi diwj diwdja idjdjwi adijq asdfiw djkafhi alkfwiwj diwdja idjaidfl qod qd adsfadsf (unknown language)</p> 
 
    </div> 
 
    <div class="col-md-3 col-xs-12"> 
 
     <img class="center-block" src="http://placehold.it/240x150" alt="thirdimage"> 
 
     <h2>Details</h2> 
 
     <p class="ptags">ad adaésdkf awpjd aiwj dwp diw aapdfjap wkd wo dakow dmaow d aodw dja wo lda ow dakldow daowdkwo dkaqodejqd dkaadfa ddfjkladfhi diwj diwdja idjdjwi adijq asdfiw djkafhi alkfwiwj diwdja idjaidfl qod qd adsfadsf (unknown language)</p> 
 
    </div> 
 
    </div> 
 
</section>

,不過也許你想使用的背景大小:蓋;背景重複:不重複;這將使它絕對響應

我修改的摘錄

+0

很好的關於我想要的圖像。但文字應該仍然在一張卡片上。我在我的問題中添加了一些CSS。請檢查一下。如果我用你的代碼240x150做的話,我已經將卡片中的圖像設置爲寬度100%,如果將窗口縮小,圖像不會留在卡片中。 – Sandro21

+0

.center-block {width = 100%;寬度:100%; } ? – Raimonds

+0

寬度:100%使圖像100%在卡內。問題是當圖像變小時,我的卡片樣式消失。因此,圖像變得極其龐大 – Sandro21

0

雖然我可以從你的問題理解,希望這將解決你的

background-repeat: no-repeat;