如果 windows 高度缩小,我有一个图像将主体推离 html 元素
I have an image that is pushing the body away from the html element if the windows height shrinks
我正在建一个网站,主页上 256X480 的图片从 body 中脱离出来,将 body 推离 html 元素,我有点困惑如何处理这个图像及其容器似乎根本不响应最大高度 属性。有人可以解释为什么会这样,或者给我指出一些资源来解释这个问题以及如何解决这个问题,谢谢。
https://jsfiddle.net/9oy5n0he/
html{
height: 100%;
}
body{
margin: 0px;
background-image: linear-gradient( 0, rgba(0,0,0,.8) 30%, rgba(0,150,255,.8) 100%), url("images/mainCover.jpg");
background-repeat: no-repeat;
background-size: cover;
height: 100%;
}
img{
max-width: 100%;
}
/*nav and header*/
header{
background-color: rgba(255,165,0,.8);
display: flex;
flex-flow: row wrap;
justify-content: center;
border-bottom: 8px solid black;
}
nav ul {
list-style: none;
padding: 0;
}
header h1{
text-align: center;
margin: 0;
padding: 15px;
text-shadow: 5px 5px 5px rgba(0,0,0,.3);
}
header h1, nav a{
font-weight: 700;
font-family: arial;
}
header nav{
display: none;
}
.main-nav{
margin: 4px;
}
nav ul li{
text-align: center;
margin: 0 4px;
border: 1px solid white;
border-radius: 15px;
font-size: 1.2rem;
}
a{
text-decoration: none;
}
nav a:visited, nav a,h1{
color: white;
}
.current-page{
background-color: rgba(0,0,0,.5);
}
footer nav{
position: fixed;
width: 100%;
bottom: 0px;
}
footer nav ul{
margin: 0;
}
footer nav li{
background-color: rgba(255,165,0,.8);
}
/*home page*/
.bookWrapper{
width: 25%;
margin: auto;
}
.bookLink {
display: block;
margin: auto;
}
.bookImage{
margin: auto;
display: block;
}
.amazonBookLabel{
background-color: rgba(0,0,0,.5);
color: white;
padding: 10px;
font-size: 1.5em;
font-weight: 600;
width: 50%;
border-radius: 20px;
margin: 30px auto;
text-align: center;
position: relative;
}
.amazonBookLabel:after{
content: "";
width: 30px;
height: 5px;
background-color: rgba(0,0,0,.5);
position: absolute;
bottom: -5px;
left: calc(50% - 17px);
}
.amazonBookLabel:before{
content: "";
width: 0;
height: 0;
border-left: 40px solid rgba(0,0,0,.5);
border-top: 40px solid transparent;
position: absolute;
bottom: -45px;
transform: rotate(-45deg);
transform-origin: top left;
left: calc(50% - 30px);
}
/***responsive***/
@media screen and (min-width: 400px){
header{
height: 130px;
display: flex;
justify-content: center;
}
header h1{
font-size: 2em;
align-self: flex-start;
white-space: nowrap;
border: 8px solid black;
}
header nav{
display: block;
align-self: flex-end;
}
.main-nav{
display: flex;
justify-content: flex-end;
}
.main-nav li{
border-radius: 8px;
padding: 5px;
font-size: 1.3rem;
}
.characters:hover {
position: relative;
border-radius: 8px 8px 0 0;
}
.drop-menu{
position: absolute;
visibility: hidden;
display: block;
top: 33px;
white-space: nowrap;
left: -2px;
background-color: rgba(255,165,0,.8);
border: 1px solid rgba(0,0,0,.02);
box-shadow: 5px 5px 5px 2px rgba(0,0,0,.3);
opacity: 0;
transition: opacity 300ms ease-in-out 0s;
z-index: 1;
}
.characters:hover .drop-menu{
visibility: visible;
opacity:1;
}
.drop-menu li{
margin: 0;
border-radius: 0;
}
footer nav{
display: none;
}
}
@media screen and (min-width:860px){
header{
height: 120px;
justify-content: space-between;
}
header h1{
margin: 0 0 0 20px;
}
.main-nav li{
font-size: 1.5rem;
}
header nav{
margin: 0 40px 0 0;
}
}
@media screen and (min-width: 1109px){
header h1{
font-size: 3em;
margin: 0 0 0 80px;
}
.main-nav > li{
margin: 0 8px;
}
header nav{
margin: 0 80px 0 0;
}
}
这是因为 html
上的 height: 100%
限制了高度,从技术上讲图像会溢出 html/body,所以您不会在溢出中看到背景。
更好的技术是删除 html
上的 height
(不是必需的)并在 body
上使用 min-height: 100vh
body {
margin: 0px;
background-image: linear-gradient( 0, rgba(0, 0, 0, .8) 30%, rgba(0, 150, 255, .8) 100%), url("images/mainCover.jpg");
background-repeat: no-repeat;
background-size: cover;
min-height: 100vh;
}
img {
max-width: 100%;
}
/*nav and header*/
header {
background-color: rgba(255, 165, 0, .8);
display: flex;
flex-flow: row wrap;
justify-content: center;
border-bottom: 8px solid black;
}
nav ul {
list-style: none;
padding: 0;
}
header h1 {
text-align: center;
margin: 0;
padding: 15px;
text-shadow: 5px 5px 5px rgba(0, 0, 0, .3);
}
header h1,
nav a {
font-weight: 700;
font-family: arial;
}
header nav {
display: none;
}
.main-nav {
margin: 4px;
}
nav ul li {
text-align: center;
margin: 0 4px;
border: 1px solid white;
border-radius: 15px;
font-size: 1.2rem;
}
a {
text-decoration: none;
}
nav a:visited,
nav a,
h1 {
color: white;
}
.current-page {
background-color: rgba(0, 0, 0, .5);
}
footer nav {
position: fixed;
width: 100%;
bottom: 0px;
}
footer nav ul {
margin: 0;
}
footer nav li {
background-color: rgba(255, 165, 0, .8);
}
/*home page*/
.bookWrapper {
width: 25%;
margin: auto;
}
.bookLink {
display: block;
margin: auto;
}
.bookImage {
margin: auto;
display: block;
}
.amazonBookLabel {
background-color: rgba(0, 0, 0, .5);
color: white;
padding: 10px;
font-size: 1.5em;
font-weight: 600;
width: 50%;
border-radius: 20px;
margin: 30px auto;
text-align: center;
position: relative;
}
.amazonBookLabel:after {
content: "";
width: 30px;
height: 5px;
background-color: rgba(0, 0, 0, .5);
position: absolute;
bottom: -5px;
left: calc(50% - 17px);
}
.amazonBookLabel:before {
content: "";
width: 0;
height: 0;
border-left: 40px solid rgba(0, 0, 0, .5);
border-top: 40px solid transparent;
position: absolute;
bottom: -45px;
transform: rotate(-45deg);
transform-origin: top left;
left: calc(50% - 30px);
}
/***responsive***/
@media screen and (min-width: 400px) {
header {
height: 130px;
display: flex;
justify-content: center;
}
header h1 {
font-size: 2em;
align-self: flex-start;
white-space: nowrap;
border: 8px solid black;
}
header nav {
display: block;
align-self: flex-end;
}
.main-nav {
display: flex;
justify-content: flex-end;
}
.main-nav li {
border-radius: 8px;
padding: 5px;
font-size: 1.3rem;
}
.characters:hover {
position: relative;
border-radius: 8px 8px 0 0;
}
.drop-menu {
position: absolute;
visibility: hidden;
display: block;
top: 33px;
white-space: nowrap;
left: -2px;
background-color: rgba(255, 165, 0, .8);
border: 1px solid rgba(0, 0, 0, .02);
box-shadow: 5px 5px 5px 2px rgba(0, 0, 0, .3);
opacity: 0;
transition: opacity 300ms ease-in-out 0s;
z-index: 1;
}
.characters:hover .drop-menu {
visibility: visible;
opacity: 1;
}
.drop-menu li {
margin: 0;
border-radius: 0;
}
footer nav {
display: none;
}
}
@media screen and (min-width:860px) {
header {
height: 120px;
justify-content: space-between;
}
header h1 {
margin: 0 0 0 20px;
}
.main-nav li {
font-size: 1.5rem;
}
header nav {
margin: 0 40px 0 0;
}
}
@media screen and (min-width: 1109px) {
header h1 {
font-size: 3em;
margin: 0 0 0 80px;
}
.main-nav > li {
margin: 0 8px;
}
header nav {
margin: 0 80px 0 0;
}
}
<main>
<header>
<h1>Seraph Chronicles</h1>
<nav>
<ul class="main-nav">
<li class="main-nav-item current-page"><a href="index.html">Home</a></li>
<li class="main-nav-item"><a href="about.html">About</a></li>
<li class="main-nav-item characters">
<a href="characters.html">Characters</a>
<ul class="drop-menu">
<li><a href="ethanClarke.html">Ethan Clarke</a></li>
<li><a href="serenaKiriaga.html">Serena Kiriaga</a></li>
<li><a href="MarcusFlynn.html">Marcus Flynn</a></li>
<li><a href="EmilyAshdown.html">Emily Ashdown</a></li>
<li><a href="MilesWest.html">Director Miles West</a></li>
</ul>
</li>
<li class="main-nav-item"><a href="auther.html">Author</a></li>
</ul>
</nav>
</header>
<section>
<p class="amazonBookLabel">Get the first and newest addition of the trilogy here!</p>
<div class="bookWrapper">
<a href="https://www.amazon.com/Seraph-Chronicles-Kyle-James-Feller/dp/1520404999/ref=sr_1_3?ie=UTF8&qid=1496613120&sr=8-3&keywords=seraph+chronicles" class="bookLink">
<img class="bookImage" src="images/bookCover.jpg" alt="image of seraph chronicles: evangelion for purchase">
</a>
</div>
</section>
<footer>
</footer>
<section>
</section>
<footer>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="characters.html">Characters</a></li>
<li><a href="auther.html">Author</a></li>
</ul>
</nav>
</footer>
</main>
我正在建一个网站,主页上 256X480 的图片从 body 中脱离出来,将 body 推离 html 元素,我有点困惑如何处理这个图像及其容器似乎根本不响应最大高度 属性。有人可以解释为什么会这样,或者给我指出一些资源来解释这个问题以及如何解决这个问题,谢谢。
https://jsfiddle.net/9oy5n0he/
html{
height: 100%;
}
body{
margin: 0px;
background-image: linear-gradient( 0, rgba(0,0,0,.8) 30%, rgba(0,150,255,.8) 100%), url("images/mainCover.jpg");
background-repeat: no-repeat;
background-size: cover;
height: 100%;
}
img{
max-width: 100%;
}
/*nav and header*/
header{
background-color: rgba(255,165,0,.8);
display: flex;
flex-flow: row wrap;
justify-content: center;
border-bottom: 8px solid black;
}
nav ul {
list-style: none;
padding: 0;
}
header h1{
text-align: center;
margin: 0;
padding: 15px;
text-shadow: 5px 5px 5px rgba(0,0,0,.3);
}
header h1, nav a{
font-weight: 700;
font-family: arial;
}
header nav{
display: none;
}
.main-nav{
margin: 4px;
}
nav ul li{
text-align: center;
margin: 0 4px;
border: 1px solid white;
border-radius: 15px;
font-size: 1.2rem;
}
a{
text-decoration: none;
}
nav a:visited, nav a,h1{
color: white;
}
.current-page{
background-color: rgba(0,0,0,.5);
}
footer nav{
position: fixed;
width: 100%;
bottom: 0px;
}
footer nav ul{
margin: 0;
}
footer nav li{
background-color: rgba(255,165,0,.8);
}
/*home page*/
.bookWrapper{
width: 25%;
margin: auto;
}
.bookLink {
display: block;
margin: auto;
}
.bookImage{
margin: auto;
display: block;
}
.amazonBookLabel{
background-color: rgba(0,0,0,.5);
color: white;
padding: 10px;
font-size: 1.5em;
font-weight: 600;
width: 50%;
border-radius: 20px;
margin: 30px auto;
text-align: center;
position: relative;
}
.amazonBookLabel:after{
content: "";
width: 30px;
height: 5px;
background-color: rgba(0,0,0,.5);
position: absolute;
bottom: -5px;
left: calc(50% - 17px);
}
.amazonBookLabel:before{
content: "";
width: 0;
height: 0;
border-left: 40px solid rgba(0,0,0,.5);
border-top: 40px solid transparent;
position: absolute;
bottom: -45px;
transform: rotate(-45deg);
transform-origin: top left;
left: calc(50% - 30px);
}
/***responsive***/
@media screen and (min-width: 400px){
header{
height: 130px;
display: flex;
justify-content: center;
}
header h1{
font-size: 2em;
align-self: flex-start;
white-space: nowrap;
border: 8px solid black;
}
header nav{
display: block;
align-self: flex-end;
}
.main-nav{
display: flex;
justify-content: flex-end;
}
.main-nav li{
border-radius: 8px;
padding: 5px;
font-size: 1.3rem;
}
.characters:hover {
position: relative;
border-radius: 8px 8px 0 0;
}
.drop-menu{
position: absolute;
visibility: hidden;
display: block;
top: 33px;
white-space: nowrap;
left: -2px;
background-color: rgba(255,165,0,.8);
border: 1px solid rgba(0,0,0,.02);
box-shadow: 5px 5px 5px 2px rgba(0,0,0,.3);
opacity: 0;
transition: opacity 300ms ease-in-out 0s;
z-index: 1;
}
.characters:hover .drop-menu{
visibility: visible;
opacity:1;
}
.drop-menu li{
margin: 0;
border-radius: 0;
}
footer nav{
display: none;
}
}
@media screen and (min-width:860px){
header{
height: 120px;
justify-content: space-between;
}
header h1{
margin: 0 0 0 20px;
}
.main-nav li{
font-size: 1.5rem;
}
header nav{
margin: 0 40px 0 0;
}
}
@media screen and (min-width: 1109px){
header h1{
font-size: 3em;
margin: 0 0 0 80px;
}
.main-nav > li{
margin: 0 8px;
}
header nav{
margin: 0 80px 0 0;
}
}
这是因为 html
上的 height: 100%
限制了高度,从技术上讲图像会溢出 html/body,所以您不会在溢出中看到背景。
更好的技术是删除 html
上的 height
(不是必需的)并在 body
min-height: 100vh
body {
margin: 0px;
background-image: linear-gradient( 0, rgba(0, 0, 0, .8) 30%, rgba(0, 150, 255, .8) 100%), url("images/mainCover.jpg");
background-repeat: no-repeat;
background-size: cover;
min-height: 100vh;
}
img {
max-width: 100%;
}
/*nav and header*/
header {
background-color: rgba(255, 165, 0, .8);
display: flex;
flex-flow: row wrap;
justify-content: center;
border-bottom: 8px solid black;
}
nav ul {
list-style: none;
padding: 0;
}
header h1 {
text-align: center;
margin: 0;
padding: 15px;
text-shadow: 5px 5px 5px rgba(0, 0, 0, .3);
}
header h1,
nav a {
font-weight: 700;
font-family: arial;
}
header nav {
display: none;
}
.main-nav {
margin: 4px;
}
nav ul li {
text-align: center;
margin: 0 4px;
border: 1px solid white;
border-radius: 15px;
font-size: 1.2rem;
}
a {
text-decoration: none;
}
nav a:visited,
nav a,
h1 {
color: white;
}
.current-page {
background-color: rgba(0, 0, 0, .5);
}
footer nav {
position: fixed;
width: 100%;
bottom: 0px;
}
footer nav ul {
margin: 0;
}
footer nav li {
background-color: rgba(255, 165, 0, .8);
}
/*home page*/
.bookWrapper {
width: 25%;
margin: auto;
}
.bookLink {
display: block;
margin: auto;
}
.bookImage {
margin: auto;
display: block;
}
.amazonBookLabel {
background-color: rgba(0, 0, 0, .5);
color: white;
padding: 10px;
font-size: 1.5em;
font-weight: 600;
width: 50%;
border-radius: 20px;
margin: 30px auto;
text-align: center;
position: relative;
}
.amazonBookLabel:after {
content: "";
width: 30px;
height: 5px;
background-color: rgba(0, 0, 0, .5);
position: absolute;
bottom: -5px;
left: calc(50% - 17px);
}
.amazonBookLabel:before {
content: "";
width: 0;
height: 0;
border-left: 40px solid rgba(0, 0, 0, .5);
border-top: 40px solid transparent;
position: absolute;
bottom: -45px;
transform: rotate(-45deg);
transform-origin: top left;
left: calc(50% - 30px);
}
/***responsive***/
@media screen and (min-width: 400px) {
header {
height: 130px;
display: flex;
justify-content: center;
}
header h1 {
font-size: 2em;
align-self: flex-start;
white-space: nowrap;
border: 8px solid black;
}
header nav {
display: block;
align-self: flex-end;
}
.main-nav {
display: flex;
justify-content: flex-end;
}
.main-nav li {
border-radius: 8px;
padding: 5px;
font-size: 1.3rem;
}
.characters:hover {
position: relative;
border-radius: 8px 8px 0 0;
}
.drop-menu {
position: absolute;
visibility: hidden;
display: block;
top: 33px;
white-space: nowrap;
left: -2px;
background-color: rgba(255, 165, 0, .8);
border: 1px solid rgba(0, 0, 0, .02);
box-shadow: 5px 5px 5px 2px rgba(0, 0, 0, .3);
opacity: 0;
transition: opacity 300ms ease-in-out 0s;
z-index: 1;
}
.characters:hover .drop-menu {
visibility: visible;
opacity: 1;
}
.drop-menu li {
margin: 0;
border-radius: 0;
}
footer nav {
display: none;
}
}
@media screen and (min-width:860px) {
header {
height: 120px;
justify-content: space-between;
}
header h1 {
margin: 0 0 0 20px;
}
.main-nav li {
font-size: 1.5rem;
}
header nav {
margin: 0 40px 0 0;
}
}
@media screen and (min-width: 1109px) {
header h1 {
font-size: 3em;
margin: 0 0 0 80px;
}
.main-nav > li {
margin: 0 8px;
}
header nav {
margin: 0 80px 0 0;
}
}
<main>
<header>
<h1>Seraph Chronicles</h1>
<nav>
<ul class="main-nav">
<li class="main-nav-item current-page"><a href="index.html">Home</a></li>
<li class="main-nav-item"><a href="about.html">About</a></li>
<li class="main-nav-item characters">
<a href="characters.html">Characters</a>
<ul class="drop-menu">
<li><a href="ethanClarke.html">Ethan Clarke</a></li>
<li><a href="serenaKiriaga.html">Serena Kiriaga</a></li>
<li><a href="MarcusFlynn.html">Marcus Flynn</a></li>
<li><a href="EmilyAshdown.html">Emily Ashdown</a></li>
<li><a href="MilesWest.html">Director Miles West</a></li>
</ul>
</li>
<li class="main-nav-item"><a href="auther.html">Author</a></li>
</ul>
</nav>
</header>
<section>
<p class="amazonBookLabel">Get the first and newest addition of the trilogy here!</p>
<div class="bookWrapper">
<a href="https://www.amazon.com/Seraph-Chronicles-Kyle-James-Feller/dp/1520404999/ref=sr_1_3?ie=UTF8&qid=1496613120&sr=8-3&keywords=seraph+chronicles" class="bookLink">
<img class="bookImage" src="images/bookCover.jpg" alt="image of seraph chronicles: evangelion for purchase">
</a>
</div>
</section>
<footer>
</footer>
<section>
</section>
<footer>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="characters.html">Characters</a></li>
<li><a href="auther.html">Author</a></li>
</ul>
</nav>
</footer>
</main>