I'm trying to have a background-image cover the entire viewport for mobile devices for my React project.
- model: iPhone 11
- os: 16.5.1
I've noticed 2 issues:
1)
When I set the background-color
of a <body>
tag => it covers the entire screen
@media screen and (max-width: 450px) {
body {
background-color: blue;
background-size: cover;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
height: 100%;
overflow: hidden;
}
}
BUT, when I use a background-image
to set the background it does not
@media screen and (max-width: 450px) {
body {
background-color: blue;
background-size: cover;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
height: 100%;
overflow: hidden;
}
}
When I try to set any background
property, for any sort of child element of the <body>
tag to cover the entire screen => it doesn't work
- child div with a
background-color
@media screen and (max-width: 450px) {
body {
background-color: blue;
}
}
#app {
background-color: red;
height: 100vh;
width: 100vw;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
overflow: hidden;
}
-child div with abackground-image
@media screen and (max-width: 450px) {
body {
background-color: blue;
}
}
#app {
background-image: url("https://res.cloudinary.com/ducqdbpaw/image/upload/v1685200227/FABIO/2017/Sanzogni_Significance_14_36_x_48_silver_leaf_oil_on_canvas_mouygv.jpg")
height: 100vh;
width: 100vw;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
overflow: hidden;
}
I've re-adjusted my css rules many times according to some sources I've found:
=>
@media screen and (max-width: 450px) {
body {
background-attachment: scroll;
background-image: url("image url");
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
height: 100%;
overflow: hidden;
}
}
... same issue
=>
@media screen and (max-width: 450px) {
body {
background-image: url("https://res.cloudinary.com/ducqdbpaw/image/upload/v1685200227/FABIO/2017/Sanzogni_Significance_14_36_x_48_silver_leaf_oil_on_canvas_mouygv.jpg") no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
height: 100%;
overflow: hidden;
}
}
..shows no image
Ultimately, I want this image to cover the entire background. Either through the <body>
tag or the <App/>
tag for my React project. Any ideas?
the html of my app
- Reproducible example: