2016-08-20 114 views
3

看到我codepen:http://codepen.io/Chiz/pen/ZOPvvZ大巨大的差距,當瀏覽器窗口大小調整到1100px以下

當調整瀏覽器的寬度窗口1100px或更少,有一個在英雄的旗幟底部的大缺口巨大和它下面的部分。

我相信這是由我在英雄橫幅上設置的700px的固定高度造成的。我嘗試將其設置爲height:auto,以便在瀏覽器窗口調整大小時自動計算高度,但不起作用。

任何想法?我試圖用CSS來做到這一點,而不是依賴bootstrap或grids/flexbox。

* { 
 
    box-sizing: border-box; 
 
    font-family: "Times New Roman", serif; 
 
} 
 

 
body { 
 
    position: relative; 
 
    height: 100%; 
 
} 
 

 
.main-container { 
 
    min-height: 100%; 
 
    margin-bottom: -3rem; 
 
} 
 

 
.main-container:after { 
 
    content: ""; 
 
    display: block; 
 
} 
 

 
section { 
 
    width: 100%; 
 
} 
 

 
.hero-section { 
 
    width: 100%; 
 
    height: 700px; 
 
    background: white url("https://i.imgsafe.org/5c6cf9094f.jpg") no-repeat scroll top center; 
 
    background-size: cover; 
 
} 
 

 
.large-text { 
 
    font-size: 7.5rem; 
 
    font-weight: 500; 
 
} 
 

 
.gracekellytitle { 
 
    margin-top: 0rem; 
 
    position: absolute; 
 
    top: 8rem; 
 
    left: 10rem; 
 
} 
 

 
.gracekellybirthdeath { 
 
    position: absolute; 
 
    top: 15.5rem; 
 
    left: 13rem; 
 
} 
 

 
.medium-text { 
 
    font-size: 2.1rem; 
 
    font-weight: 100; 
 
} 
 

 
.small-text { 
 
    font-size: 1.2rem; 
 
} 
 

 
.generic-section { 
 
    margin-top: 5rem; 
 
} 
 

 
.section-non-first { 
 
    margin-top: 2rem; 
 
} 
 

 
.gracekelly-introtitle { 
 
    font-size: 2.5rem; 
 
    margin-top: 2.5rem; 
 
} 
 

 
.generic-content-text { 
 
    font-size: 1.5rem; 
 
    line-height: 2.2rem; 
 
} 
 

 
.generic-center-text { 
 
    width: 70%; 
 
    margin: 0 auto; 
 
    max-width: 900px; 
 
} 
 

 
.generic-left-image { 
 
    float: left; 
 
    width: 30%; 
 
    padding-left: 8%; 
 
} 
 

 
.generic-right-text { 
 
    float: right; 
 
    width: 70%; 
 
    padding: 0 8% 0 8%; 
 
    margin: 0; 
 
} 
 

 
.section-center { 
 
    text-align: center; 
 
} 
 

 
footer { 
 
    width: 100%; 
 
    background-color: rgb(60, 60, 60); 
 
    text-align: center; 
 
    line-height: 3rem; 
 
    color: white; 
 
    margin-top: 5rem; 
 
} 
 

 
footer, 
 
main-container:after { 
 
    height: 3rem; 
 
} 
 

 

 
/* Media Queries */ 
 

 
@media only screen and (max-width:1600px) { 
 
    .large-text { 
 
    font-size: 6rem; 
 
    font-weight: 500; 
 
    } 
 
    .medium-text { 
 
    font-size: 1.8rem; 
 
    font-weight: 500; 
 
    } 
 
    .gracekellytitle { 
 
    margin-top: 0rem; 
 
    position: absolute; 
 
    top: 12rem; 
 
    left: 10rem; 
 
    } 
 
    .gracekellybirthdeath { 
 
    position: absolute; 
 
    top: 17.7rem; 
 
    left: 12rem; 
 
    } 
 
} 
 

 
@media only screen and (max-width:1430px) { 
 
    .large-text { 
 
    font-size: 5rem; 
 
    font-weight: 500; 
 
    } 
 
    .medium-text { 
 
    font-size: 1.4rem; 
 
    font-weight: 500; 
 
    } 
 
    .gracekellytitle { 
 
    margin-top: 0rem; 
 
    position: absolute; 
 
    top: 12rem; 
 
    left: 10rem; 
 
    } 
 
    .gracekellybirthdeath { 
 
    position: absolute; 
 
    top: 16.8rem; 
 
    left: 12rem; 
 
    } 
 
} 
 

 
@media only screen and (max-width:1100px) 
 
{ 
 
    
 
    .hero-section { 
 
    width: 100%; 
 
    height:700px; 
 
    background: white url("https://i.imgsafe.org/5c6cf9094f.jpg") no-repeat scroll top center; 
 
    background-size: contain; 
 
     border:1px solid red; 
 
} 
 
    
 
    .large-text { 
 
    font-size: 5rem; 
 
    font-weight: 500; 
 
    } 
 
    .medium-text { 
 
    font-size: 1.4rem; 
 
    font-weight: 500; 
 
    } 
 
    .gracekellytitle { 
 
    margin-top: 0rem; 
 
    position: absolute; 
 
    top: 7rem; 
 
    left: 8rem; 
 
    } 
 
    .gracekellybirthdeath { 
 
    position: absolute; 
 
    top: 11.7rem; 
 
    left: 10rem; 
 
    } 
 
    @media only screen and (max-width:900px) { 
 
    .large-text { 
 
     font-size: 5rem; 
 
     font-weight: 500; 
 
    } 
 
    .medium-text { 
 
     font-size: 1.4rem; 
 
     font-weight: 500; 
 
    } 
 
    .gracekellytitle { 
 
     margin-top: 0rem; 
 
     position: absolute; 
 
     top: 7rem; 
 
     left: 8rem; 
 
    } 
 
    .gracekellybirthdeath { 
 
     position: absolute; 
 
     top: 11.7rem; 
 
     left: 10rem; 
 
    } 
 
    .detailssection { 
 
     width: 100%; 
 
     padding: 0; 
 
     margin: 0; 
 
    } 
 
    .generic-left-image { 
 
     padding:0; 
 
     width: 100%; 
 
     float:none; 
 
     margin-top:3rem; 
 
    } 
 
    .generic-right-text { 
 
     width: 80%; 
 
     max-width:850px; 
 
     float:none; 
 
     margin:3rem auto 0 auto; 
 
    } 
 
    } 
 
}
<div class="main-container"> 
 

 
<!--hero banner section--> 
 
<section class="hero-section"> 
 
    <h1 class="large-text gracekellytitle">Grace Kelly</h1> 
 
    <h2 class="medium-text gracekellybirthdeath">12 Nov 1929 – 14 Sep 1982</h2> 
 
</section> 
 

 

 
<!-- intro section --> 
 
<section class="generic-section section-center section-non-first"> 
 
    <img src="https://i.imgsafe.org/5d186cfb14.png" alt="" /> 
 
    <h2 class="gracekelly-introtitle">Grace Kelly - Her Serene Highness Grace of Monaco</h2> 
 
    <p class="generic-center-text generic-content-text">Grace Patricia Kelly (November 12, 1929 – September 14, 1982) was an American actress who, after marrying Prince Rainier III in April 1956, became Princess of Monaco.<br /><br />After embarking on an acting career in 1950, at the age of 20, Kelly appeared in New York City theatrical productions and more than 40 episodes of live drama productions broadcast during the early 1950s Golden Age of Television. In October 1953, she gained stardom from her performance in the film Mogambo. </p> 
 
</section> 
 

 

 
<!-- details section --> 
 
<section class="detailssection section-non-first"> 
 
    <img class="generic-left-image" src="https://i.imgsafe.org/5d79d5ef25.jpg" alt="" /> 
 
    <p class="generic-right-text generic-content-text">Tragedy struck on September 14, 1982, when Princess Grace and her younger daughter was driving along the steep cliffs of the Côte d'Azur region of southern France. She suffered a stroke and lost control of the vehicle, which spun off the cliff's edge and plunged down a 45-foot embankment. Mother and daughter were rushed to a hospital where Princess Grace spent 24 hours in a coma before passing away at the age of 52. Princess Stéphanie survived with minor injuries.<br /><br /> 
 

 
Grace Kelly remained in the public eye for most of her life. Her on-screen beauty, self-confidence and mystery enchanted the world, and her serenity and poise as Princess Consort piqued the media's attention. Of this attention, she remarked with typical humor and grace, "The freedom of the press works in such a way that there is not much freedom from it." Kelly's very public life is set to be portrayed by Nicole Kidman in the film Grace of Monaco (2014).</p> 
 
</section> 
 

 
<br style="clear:both;" /> 
 

 
<!-- footer section --> 
 
<footer>Copyright © 2016 Nikar</footer> 
 

 
</div>

+0

[響應CSS背景圖像]的可能的複製(http://stackoverflow.com/questions/12609110/responsive-css-background-images) –

+0

是上述真的** *您需要複製或顯示您遇到的問題的最少量***代碼? – Martin

+0

@IlloYonexIllo你能回答這個問題的答案嗎? – kukkuz

回答

1

與它搞亂,我改變高度:450像素和背景大小:覆蓋

@media only screen and (max-width:1100px) { 
    .hero-section { 
    width: 100%; 
    height: 450px; 
    background: white url("https://i.imgsafe.org/5c6cf9094f.jpg") no-repeat scroll top center; 
    background-size: cover; 
    border: 1px solid red; 
    } 
0

我知道你想contain你的英雄形象,而不是使用它像背景cover。那麼會發生什麼是

a。當你包含背景時,它將調整到屏幕寬度。

b。這也意味着你必須爲英雄section設置一個高度,並且繁瑣地控制空白。

替代

所以你的情況最好是把它作爲一個img,然後調整響應式設計。我得到了它開始爲你下面:

的英雄section註釋掉heightbackground並添加這個CSS:

.hero-section img{ 
    max-width: 100%; 
    vertical-align: top; 
} 

和這個網站太:

<img src="https://i.imgsafe.org/5c6cf9094f.jpg"/> 

檢查了這一點,讓我知道你對此的反饋。謝謝!

* { 
 
    box-sizing: border-box; 
 
    font-family: "Times New Roman", serif; 
 
} 
 

 
body { 
 
    position: relative; 
 
    height: 100%; 
 
} 
 

 
.main-container { 
 
    min-height: 100%; 
 
    margin-bottom: -3rem; 
 
} 
 

 
.main-container:after { 
 
    content: ""; 
 
    display: block; 
 
} 
 

 
section { 
 
    width: 100%; 
 
} 
 

 
.hero-section { 
 
    width: 100%; 
 
    /*height: 700px; 
 
    background: white url("https://i.imgsafe.org/5c6cf9094f.jpg") no-repeat scroll top center; 
 
    background-size: cover;*/ 
 
} 
 
.hero-section img{ 
 
    max-width: 100%; 
 
    vertical-align: top; 
 
} 
 

 
.large-text { 
 
    font-size: 7.5rem; 
 
    font-weight: 500; 
 
} 
 

 
.gracekellytitle { 
 
    margin-top: 0rem; 
 
    position: absolute; 
 
    top: 8rem; 
 
    left: 10rem; 
 
} 
 

 
.gracekellybirthdeath { 
 
    position: absolute; 
 
    top: 15.5rem; 
 
    left: 13rem; 
 
} 
 

 
.medium-text { 
 
    font-size: 2.1rem; 
 
    font-weight: 100; 
 
} 
 

 
.small-text { 
 
    font-size: 1.2rem; 
 
} 
 

 
.generic-section { 
 
    margin-top: 5rem; 
 
} 
 

 
.section-non-first { 
 
    margin-top: 2rem; 
 
} 
 

 
.gracekelly-introtitle { 
 
    font-size: 2.5rem; 
 
    margin-top: 2.5rem; 
 
} 
 

 
.generic-content-text { 
 
    font-size: 1.5rem; 
 
    line-height: 2.2rem; 
 
} 
 

 
.generic-center-text { 
 
    width: 70%; 
 
    margin: 0 auto; 
 
    max-width: 900px; 
 
} 
 

 
.generic-left-image { 
 
    float: left; 
 
    width: 30%; 
 
    padding-left: 8%; 
 
} 
 

 
.generic-right-text { 
 
    float: right; 
 
    width: 70%; 
 
    padding: 0 8% 0 8%; 
 
    margin: 0; 
 
} 
 

 
.section-center { 
 
    text-align: center; 
 
} 
 

 
footer { 
 
    width: 100%; 
 
    background-color: rgb(60, 60, 60); 
 
    text-align: center; 
 
    line-height: 3rem; 
 
    color: white; 
 
    margin-top: 5rem; 
 
} 
 

 
footer, 
 
main-container:after { 
 
    height: 3rem; 
 
} 
 

 

 
/* Media Queries */ 
 

 
@media only screen and (max-width:1600px) { 
 
    .large-text { 
 
    font-size: 6rem; 
 
    font-weight: 500; 
 
    } 
 
    .medium-text { 
 
    font-size: 1.8rem; 
 
    font-weight: 500; 
 
    } 
 
    .gracekellytitle { 
 
    margin-top: 0rem; 
 
    position: absolute; 
 
    top: 12rem; 
 
    left: 10rem; 
 
    } 
 
    .gracekellybirthdeath { 
 
    position: absolute; 
 
    top: 17.7rem; 
 
    left: 12rem; 
 
    } 
 
} 
 

 
@media only screen and (max-width:1430px) { 
 
    .large-text { 
 
    font-size: 5rem; 
 
    font-weight: 500; 
 
    } 
 
    .medium-text { 
 
    font-size: 1.4rem; 
 
    font-weight: 500; 
 
    } 
 
    .gracekellytitle { 
 
    margin-top: 0rem; 
 
    position: absolute; 
 
    top: 12rem; 
 
    left: 10rem; 
 
    } 
 
    .gracekellybirthdeath { 
 
    position: absolute; 
 
    top: 16.8rem; 
 
    left: 12rem; 
 
    } 
 
} 
 

 
@media only screen and (max-width:1100px) 
 
{ 
 
    
 
    .hero-section { 
 
    width: 100%; 
 
    /*height:700px; 
 
    background: white url("https://i.imgsafe.org/5c6cf9094f.jpg") no-repeat scroll top center; 
 
    background-size: contain;*/ 
 
     border:1px solid red; 
 
} 
 
    
 
    .large-text { 
 
    font-size: 5rem; 
 
    font-weight: 500; 
 
    } 
 
    .medium-text { 
 
    font-size: 1.4rem; 
 
    font-weight: 500; 
 
    } 
 
    .gracekellytitle { 
 
    margin-top: 0rem; 
 
    position: absolute; 
 
    top: 7rem; 
 
    left: 8rem; 
 
    } 
 
    .gracekellybirthdeath { 
 
    position: absolute; 
 
    top: 11.7rem; 
 
    left: 10rem; 
 
    } 
 
    @media only screen and (max-width:900px) { 
 
    .large-text { 
 
     font-size: 5rem; 
 
     font-weight: 500; 
 
    } 
 
    .medium-text { 
 
     font-size: 1.4rem; 
 
     font-weight: 500; 
 
    } 
 
    .gracekellytitle { 
 
     margin-top: 0rem; 
 
     position: absolute; 
 
     top: 7rem; 
 
     left: 8rem; 
 
    } 
 
    .gracekellybirthdeath { 
 
     position: absolute; 
 
     top: 11.7rem; 
 
     left: 10rem; 
 
    } 
 
    .detailssection { 
 
     width: 100%; 
 
     padding: 0; 
 
     margin: 0; 
 
    } 
 
    .generic-left-image { 
 
     padding:0; 
 
     width: 100%; 
 
     float:none; 
 
     margin-top:3rem; 
 
    } 
 
    .generic-right-text { 
 
     width: 80%; 
 
     max-width:850px; 
 
     float:none; 
 
     margin:3rem auto 0 auto; 
 
    } 
 
    } 
 
}
<div class="main-container"> 
 

 
<!--hero banner section--> 
 
<section class="hero-section"> 
 
    <img src="https://i.imgsafe.org/5c6cf9094f.jpg"/> 
 
    <h1 class="large-text gracekellytitle">Grace Kelly</h1> 
 
    <h2 class="medium-text gracekellybirthdeath">12 Nov 1929 – 14 Sep 1982</h2> 
 
</section> 
 

 

 
<!-- intro section --> 
 
<section class="generic-section section-center section-non-first"> 
 
    <img src="https://i.imgsafe.org/5d186cfb14.png" alt="" /> 
 
    <h2 class="gracekelly-introtitle">Grace Kelly - Her Serene Highness Grace of Monaco</h2> 
 
    <p class="generic-center-text generic-content-text">Grace Patricia Kelly (November 12, 1929 – September 14, 1982) was an American actress who, after marrying Prince Rainier III in April 1956, became Princess of Monaco.<br /><br />After embarking on an acting career in 1950, at the age of 20, Kelly appeared in New York City theatrical productions and more than 40 episodes of live drama productions broadcast during the early 1950s Golden Age of Television. In October 1953, she gained stardom from her performance in the film Mogambo. </p> 
 
</section> 
 

 

 
<!-- details section --> 
 
<section class="detailssection section-non-first"> 
 
    <img class="generic-left-image" src="https://i.imgsafe.org/5d79d5ef25.jpg" alt="" /> 
 
    <p class="generic-right-text generic-content-text">Tragedy struck on September 14, 1982, when Princess Grace and her younger daughter was driving along the steep cliffs of the Côte d'Azur region of southern France. She suffered a stroke and lost control of the vehicle, which spun off the cliff's edge and plunged down a 45-foot embankment. Mother and daughter were rushed to a hospital where Princess Grace spent 24 hours in a coma before passing away at the age of 52. Princess Stéphanie survived with minor injuries.<br /><br /> 
 

 
Grace Kelly remained in the public eye for most of her life. Her on-screen beauty, self-confidence and mystery enchanted the world, and her serenity and poise as Princess Consort piqued the media's attention. Of this attention, she remarked with typical humor and grace, "The freedom of the press works in such a way that there is not much freedom from it." Kelly's very public life is set to be portrayed by Nicole Kidman in the film Grace of Monaco (2014).</p> 
 
</section> 
 

 
<br style="clear:both;" /> 
 

 
<!-- footer section --> 
 
<footer>Copyright © 2016 Nikar</footer> 
 

 
</div>

+0

@IlloYonexIllo想知道你的想法嗎?謝謝! – kukkuz

相關問題