Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

html - How to make all backgrounds have a background-size: cover?

On my body, I have 3 images displayed horizontally.

Each image takes up 33% width.

Together, these 3 images take up the entire webpage.

How can I make these 3 images have a background size of cover, so their aspect ratio's are maintained?

body {
    background-image: url(https://images.pexels.com/photos/1546166/pexels-photo-1546166.jpeg),
                      url(https://images.pexels.com/photos/87223/pexels-photo-87223.jpeg),
                      url(https://images.pexels.com/photos/280221/pexels-photo-280221.jpeg);

    background-repeat: no-repeat, no-repeat, no-repeat;
    background-position: left, center, right;
    background-size: 33% 100%, 34% 100%, 33% 100%;
    width: 100%;
    height: 100vh;
}
question from:https://stackoverflow.com/questions/65854912/how-to-make-all-backgrounds-have-a-background-size-cover

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From what I was able to look up and experiment with, there is no way to have them individually set to cover.
I do however, have a work around!

body {
      width: 100%;
      height: 100vh;
      margin: 0px;
}

#back1,#back2,#back3{
      background-size: 100% 100%;
      background-position: center;
      background-size: cover;
      width: calc(100%/3);
      height: 100vh;
      position: absolute;
      z-index: -1;
}
  
#back1{background-image: url(https://images.pexels.com/photos/1546166/pexels-photo-1546166.jpeg);}
#back2{background-image: url(https://images.pexels.com/photos/87223/pexels-photo-87223.jpeg);left:calc(100%/3);}
#back3{background-image: url(https://images.pexels.com/photos/280221/pexels-photo-280221.jpeg);left:calc(200%/3);}

h1{margin-top: 0px;}
<body>
  <div id="back1"></div>
  <div id="back2"></div>
  <div id="back3"></div>
  <h1>This text will start at the top left like it would otherwise.</h1>
</body>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...