2017-04-18 59 views
0

當您調整窗口大小時,第二行中的圖像被向下推並遠離其上方的標題。它應該保持與正上方的標題不變的距離,但也應該保持附着在其行的底部。將背景圖像保持在相對於標題的高度不變

HTML

<div class="row hero-family"> 
     <div class="col-sm-12 col-md-12 col-lg-12"> 
     <div class="container blurb"> 
      <h1 class="header">Nice big title here</h1> 
      <p>More lorem ipsum text. How can I keep the image below at a constant distance from this header, while keeping it attached at the base of this row as well?</p> 
      <button type="button" class="btn btn-red">Big long button here</button> 
     </div> 
     </div> 
    </div> 
    <div class="row hero-family family-image"> 
     </div> 

CSS

.hero-header h2 { 
    margin: 108px 0 30px 0; 
    text-align: left; 
    font-family: Open Sans; 
    font-size: 58px; 
    font-weight: 100; 
    color: #000; 
    width: 43%; 
    line-height: 80px; 
} 
.hero-header p { 
    font-size: 18px; 
    font-weight: 300; 
    width: 40%; 
    color: #000; 
    text-align: left; 
    font-family: 'Open Sans'; 
    padding-bottom: 36px; 

} 
.hero-unit { 
    background-image: url("/../images/bg.png"); 
    width: 100%; 
    /* height: 584px; */ 
    background-position: center center; 
    background-repeat: no-repeat; 
    text-align: center; 
    margin: auto; 
    padding: 0; 
    background-size: cover; 
    -webkit-background-size: cover; 
    -moz-background-size: cover; 
    -o-background-size: cover; 
    background-size: cover; 
} 
@media (max-width: 991px){ 
    .hero-header h2, .hero-header p{ 
    width: 80%; 
    } 
} 
.hero-header { 
    margin-left: -15px; 
} 
@media (max-width: 540px) { 
    .hero-unit { 
    height: auto; 
    } 
} 
@media (max-width: 400px) { 
    .hero-unit .pull-left { 
    display: none; 
    } 
    .hero-unit .call-us { 
    margin-top: 6%; 
    background-color: #fff; 
    color: #000; 
    font-family: 'Open Sans'; 
    font-weight: 600; 
    } 
    .hero-unit .fa-phone { 
    color: #000; 
    } 
} 
@media (min-width: 376px) { 
    .hero-unit .call-us { 
    display: none; 
    } 
    .hero-unit .pull-left { 
    display: block; 
    } 
} 
.hero-unit .hero-header { 
    margin-left: -15px; 
} 

.family-image { 
    background-image: url("https://s3.us-east-2.amazonaws.com/ftp-site-images/family.png"); 
    background-color: #eee; 
    background-repeat: no-repeat; 
    background-size: contain; 
    background-position: center bottom; 
    height: 500px; 
} 
@media (max-width: 400px) { 
    .family-image { 
    height: 300px; 
    } 
} 
@media (max-width: 991px) { 
    .family-image { 
    margin-bottom: 10%; 
    } 
} 

.hero-family { 
    background-color: #eee; 
    background-color: #eee; 
} 
@media (max-width: 400px) { 
    .hero-family { 
    height: 250px; 
    } 
} 
.hero-family .blurb { 
    text-align: center; 
    width: 76%; 
} 
@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation: portrait) and (-webkit-min-device-pixel-ratio: 1) { 
    .hero-family .blurb { 
    margin-left: -1%; 
    } 
} 
.hero-family .header { 
    font-weight: 300; 
    color: #ce2523; 
    margin: 6%; 
    font-size: 53px; 
    font-family: 'Open Sans'; 
} 
@media (max-width: 400px) { 
    .hero-family .header { 
    text-align: center; 
    font-size: 28px; 
    } 
} 
@media (max-width: 1199px) { 
    .hero-family .header { 
    font-size: 36px; 
    } 
} 
.hero-family p { 
    font-family: 'Open Sans'; 
    text-align: center; 
    margin: -3%; 
    font-size: 18px; 
} 
@media (max-width: 400px) { 
    .hero-family p { 
    text-align: center; 
    margin-left: 5px; 
    margin-right: 5px; 
    font-size: 15px; 
    padding: 10px 0 10px; 
    } 
} 
.hero-family .btn-red { 
    margin-top: 6%; 
    font-weight: 400; 
} 
@media (max-width: 400px) { 
    .hero-family .btn-red { 
    margin-left: 0%; 
    } 
} 
@media (max-width: 768px){ 
.hero-header h2, .hero-header p { 
    width: 80%; 
    text-align: center; 
    margin: 0 auto; 
} 
} 

的jsfiddle:LINK

+0

您可以查看vmax或vmin單位https://jsfiddle.net/mtd4rpkd/6/或在wh或vw單位中設置高度請參閱https://web-design-weekly.com/2014/11/18/viewport -units-VW-VH-VMIN-VMAX / –

回答

0

由於與視圖像尺度,它不會是一個固定的高度,但你有一個固定的高度他認爲這是一個背景圖像。您需要使用相對內部填充來匹配圖像的高寬比。

.family-image刪除height: 500px,並添加padding-bottom: 42.6%(二千五百七十零分之一千○九十五* 100)

https://jsfiddle.net/mtd4rpkd/7/

.hero-header h2 { 
 
    margin: 108px 0 30px 0; 
 
    text-align: left; 
 
    font-family: Open Sans; 
 
    font-size: 58px; 
 
    font-weight: 100; 
 
    color: #000; 
 
    width: 43%; 
 
    line-height: 80px; 
 
} 
 
.hero-header p { 
 
    font-size: 18px; 
 
    font-weight: 300; 
 
    width: 40%; 
 
    color: #000; 
 
    text-align: left; 
 
    font-family: 'Open Sans'; 
 
    padding-bottom: 36px; 
 
    
 
} 
 
.hero-unit { 
 
    background-image: url("/../images/bg.png"); 
 
    width: 100%; 
 
    /* height: 584px; */ 
 
    background-position: center center; 
 
    background-repeat: no-repeat; 
 
    text-align: center; 
 
    margin: auto; 
 
    padding: 0; 
 
    background-size: cover; 
 
    -webkit-background-size: cover; 
 
    -moz-background-size: cover; 
 
    -o-background-size: cover; 
 
    background-size: cover; 
 
} 
 
@media (max-width: 991px){ 
 
    .hero-header h2, .hero-header p{ 
 
    width: 80%; 
 
    } 
 
} 
 
.hero-header { 
 
    margin-left: -15px; 
 
} 
 
@media (max-width: 540px) { 
 
    .hero-unit { 
 
    height: auto; 
 
    } 
 
} 
 
@media (max-width: 400px) { 
 
    .hero-unit .pull-left { 
 
    display: none; 
 
    } 
 
    .hero-unit .call-us { 
 
    margin-top: 6%; 
 
    background-color: #fff; 
 
    color: #000; 
 
    font-family: 'Open Sans'; 
 
    font-weight: 600; 
 
    } 
 
    .hero-unit .fa-phone { 
 
    color: #000; 
 
    } 
 
} 
 
@media (min-width: 376px) { 
 
    .hero-unit .call-us { 
 
    display: none; 
 
    } 
 
    .hero-unit .pull-left { 
 
    display: block; 
 
    } 
 
} 
 
.hero-unit .hero-header { 
 
    margin-left: -15px; 
 
} 
 

 
.family-image { 
 
    background-image: url("https://s3.us-east-2.amazonaws.com/ftp-site-images/family.png"); 
 
    background-color: #eee; 
 
    background-repeat: no-repeat; 
 
    background-size: contain; 
 
    background-position: center bottom; 
 
    /*height: 500px;*/ 
 
    padding-bottom: 42.6%; 
 
} 
 
@media (max-width: 400px) { 
 
    .family-image { 
 
    height: 300px; 
 
    } 
 
} 
 
@media (max-width: 991px) { 
 
    .family-image { 
 
    margin-bottom: 10%; 
 
    } 
 
} 
 

 
.hero-family { 
 
    background-color: #eee; 
 
    background-color: #eee; 
 
} 
 
@media (max-width: 400px) { 
 
    .hero-family { 
 
    height: 250px; 
 
    } 
 
} 
 
.hero-family .blurb { 
 
    text-align: center; 
 
    width: 76%; 
 
} 
 
@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation: portrait) and (-webkit-min-device-pixel-ratio: 1) { 
 
    .hero-family .blurb { 
 
    margin-left: -1%; 
 
    } 
 
} 
 
.hero-family .header { 
 
    font-weight: 300; 
 
    color: #ce2523; 
 
    margin: 6%; 
 
    font-size: 53px; 
 
    font-family: 'Open Sans'; 
 
} 
 
@media (max-width: 400px) { 
 
    .hero-family .header { 
 
    text-align: center; 
 
    font-size: 28px; 
 
    } 
 
} 
 
@media (max-width: 1199px) { 
 
    .hero-family .header { 
 
    font-size: 36px; 
 
    } 
 
} 
 
.hero-family p { 
 
    font-family: 'Open Sans'; 
 
    text-align: center; 
 
    margin: -3%; 
 
    font-size: 18px; 
 
} 
 
@media (max-width: 400px) { 
 
    .hero-family p { 
 
    text-align: center; 
 
    margin-left: 5px; 
 
    margin-right: 5px; 
 
    font-size: 15px; 
 
    padding: 10px 0 10px; 
 
    } 
 
} 
 
.hero-family .btn-red { 
 
    margin-top: 6%; 
 
    font-weight: 400; 
 
} 
 
@media (max-width: 400px) { 
 
    .hero-family .btn-red { 
 
    margin-left: 0%; 
 
    } 
 
} 
 
@media (max-width: 768px){ 
 
.hero-header h2, .hero-header p { 
 
    width: 80%; 
 
    text-align: center; 
 
    margin: 0 auto; 
 
} 
 
}
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/> 
 
<div class="hero-unit"> 
 
    <div class="container"> 
 
    <div class="container hero-header"> 
 
     <h2>Title text goes here, for you to read</h2> 
 
     <p>This is a bunch of lorem ipsum text to fill up space that will eventually be taken over by real content. Just filler content here, but it seeps into the row below is when resizing.</p> 
 
     <button type="button" class="btn btn-ghost pull-left">contact us</button> 
 
     <button type="button" class="btn btn-ghost call-us">call us 24/7 <i class="fa fa-phone"></i></button> 
 
    </div> 
 
    </div> 
 
    <div class="col-lg-6"> 
 
    </div> 
 
</div> 
 

 
<div class="row hero-family"> 
 
    <div class="col-sm-12 col-md-12 col-lg-12"> 
 
    <div class="container blurb"> 
 
     <h1 class="header">Nice big title here</h1> 
 
     <p>More lorem ipsum text. How can I keep the image below at a constant distance from this header, while keeping it attached at the base of this row as well?</p> 
 
     <button type="button" class="btn btn-red">Big long button here</button> 
 
    </div> 
 
    </div> 
 
</div> 
 
<div class="row hero-family family-image"> 
 
    </div>

0

這裏是一個修復: 你只刪除背景位置從類.family-image還設置高度257px到類將它修復您的問題

相關問題