如何让我的 CSS 背景图像与窗口大小一起缩放?



我正在构建我的第一个实时网站,我注意到背景图像没有随着窗口而缩小,然后它调整了大小。网站的其余部分会缩放,但背景图像看起来就像是全屏显示的。我只能假设这意味着它不会完全可见,但在较小的设备中被切断。是什么原因导致这种情况发生,我该如何解决? 对此的帮助将不胜感激,谢谢

我刚刚发布了我的css的一般部分,在整个站点中共享的非响应部分,但其余部分在jsfiddle中 https://jsfiddle.net/zd00nmce/5/

html,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;
}
img{
max-width: 100%;
}
h1,h2,h3{
font-family: arial;
text-align: center;
margin: 20px auto;
}
ul{
list-style: none;
text-align: center;
padding: 0;
}
a:visited,a{
color: black;
}
section{
margin: 5%;
color: white;
}
section > article{
padding: 1em;
background-color: rgba(0,0,0,.5);
border-radius: 15px;
margin: 10px;
}
section h2, section h3{
text-align: center;
}
section h2{
font-size: 1.3em;
text-shadow: 3px 3px 3px rgba(0,0,0,.5);
}
section h3{
font-size: 1.1em;
}
/*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;
}
header h1{
text-align: center;
margin: 0;
padding: 15px;
font-size: 1.5em;
text-shadow: 5px 5px 5px rgba(0,0,0,.3);
}
header h1, nav a{
font-weight: 700;
font-family: arial;
}
header nav{
position: fixed;
bottom: 0;
width: 100%;
z-index: 1;
// background-color: rgba(255,165,0,.8);
}
.main-nav{
margin: 0;
width: 100%;
color: white;
font-family: arial;
font-weight: 600;
}
nav ul li{ 
text-align: center;
border: 1px solid white;
border-radius: 15px;
font-size: 1.2rem;
background-color:rgba(255,165,0,.8);
width: 100%;
}
.drop-menu-back{
display: none;
width: 100%;
}
a{
text-decoration: none;
}
nav a:visited, nav a,h1{
color: white; 
}
.main-nav .current-page { 
background-color: rgba(0,0,0,.5);
}
/****drop down menu****/
.characters:hover {
position: relative;
border-radius: 8px 8px 0 0;
}
.drop-menu{
position: absolute;
visibility: hidden;
display: block;
top: 38px;
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;
}

CSS Tricks对此有一个有用的解决方案。

https://css-tricks.com/perfect-full-page-background-image/

html { 
background: url(images/bg.jpg) no-repeat center center fixed; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

我相信这应该是你要找的。

尝试将其添加到 css 中的正文中:

background-position: center center;

这将使您的图像居中并在调整大小时保持纵横比。

//拉斯

最新更新