How do I make these media queries work? - css

I am trying to add media queries to my homepage banner image. I want it to be responsive and update to 3 different image sizes depending on the screen used. Currently only the medium sized image shows up. Please advise what is wrong with my code. Thanks.
Here is my code:
/*---------------------------------------------
Section#Slider [Banner Image]
-----------------------------------------------*/
/* NEXUS 5 Size 412px, XS
(less than 786px no media query since this is
default in Bootstrap)
*/
/* IPAD SIZE 768px and up, S*/
#media screen and (min-width: 550px) {
#slider {
background: url("../img/smartphoneweb_640x285.jpeg") no-
repeat;
background-size: cover;
background-attachment: fixed;
background-position: 10% 0%;
padding: 200px 0 280px 0;
position: relative;
}
}
/* MD, desktops, 992px and up */
#media (min-width: 950px) {
#slider {
background: url("../img/smartphoneweb_1280x570.jpeg") no-
repeat;
background-size: cover;
background-attachment: fixed;
background-position: 10% 0%;
padding: 200px 0 280px 0;
position: relative;
}
}
/* LG, large desktops, 1200px and up */
#media (min-width: 1200px) {
#slider {
background: url("../img/smartphoneweb_1920x855.jpeg") no-
repeat;
background-size: cover;
background-attachment: fixed;
background-position: 10% 0%;
padding: 200px 0 280px 0;
position: relative;
}
}

The main issue was you forgot to add screen in your 992px media query. I made the changes might be this is helpful.
/*---------------------------------------------
Section#Slider [Banner Image]
-----------------------------------------------*/
/* NEXUS 5 Size 412px, XS
(less than 786px no media query since this is
default in Bootstrap)
*/
/* IPAD SIZE 768px and up, S*/
#media screen and (min-width:0px) and (max-width:550px) {
#slider {
background: url("../img/smartphoneweb_640x285.jpeg") no-
repeat;
background-size: cover;
background-attachment: fixed;
background-position: 10% 0%;
padding: 200px 0 280px 0;
position: relative;
}
}
/* MD, desktops, 992px and up */
#media screen and (min-width:551px) and (max-width:992px){
#slider {
background: url("../img/smartphoneweb_1280x570.jpeg") no-
repeat;
background-size: cover;
background-attachment: fixed;
background-position: 10% 0%;
padding: 200px 0 280px 0;
position: relative;
}
}
/* LG, large desktops, 1200px and up */

You can handle style of a spicifique screen size using min and max media query
#media screen and (min-width: 550px) and (max-width: 950px){
#slider {
background: url("../img/smartphoneweb_640x285.jpeg") no-
repeat;
background-size: cover;
background-attachment: fixed;
background-position: 10% 0%;
padding: 200px 0 280px 0;
position: relative;
}
}
/* MD, desktops, 992px and up */
#media (min-width: 950px) and (max-width: 1200px){
#slider {
background: url("../img/smartphoneweb_1280x570.jpeg") no-
repeat;
background-size: cover;
background-attachment: fixed;
background-position: 10% 0%;
padding: 200px 0 280px 0;
position: relative;
}
}
#media (min-width: 1200px){
#slider {
background: url("../img/smartphoneweb_1920x855.jpeg") no-
repeat;
background-size: cover;
background-attachment: fixed;
background-position: 10% 0%;
padding: 200px 0 280px 0;
position: relative;
}
}

Related

Optimizing css for a media query

Good morning,
I have created a media query for my website. I have the feeling that it is slowing down my website, because it is not optimized.
On mobile phones, I have a specific image. For tablets and desktops I have another image to be shown.
The media query I have works just fine. But it is not optimized.
Can anyone help me to get it optimized?
this is the query:
body, html {
height : 100%;
width: 100%;
margin:0;
}
#media only screen and (min-width: 320px) {
.Frontpage-image {
background-image: url("https://dit.be/wp-content/uploads/2021/01/mobiel-01.svg");
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
height: 100vh;
}
}
#media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
.Frontpage-image {
background-image:url("https://dit.be/wp-content/uploads/2021/01/desktop-01.svg");
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
height:100vh;
}
}
#media screen and (min-width: 768px) {
.Frontpage-image{
background-image:url("https://dit.be/wp-content/uploads/2021/01/desktop-01.svg");
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
height: 100vh;
}
}
#media screen and (min-width: 1025px) {
.Frontpage-image {
background-image:url("https://dit.be/wp-content/uploads/2021/01/desktop-01.svg");
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
height:100vh;
}
}
Please use this CSS code.
In media query, styles defined outside of query is inherited.
So my code works exactly like yours, but the code has been greatly reduced.
body, html {
height : 100%;
width: 100%;
margin:0;
}
.Frontpage-image {
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
height: 100vh;
}
#media only screen and (min-width: 320px) {
.Frontpage-image {
background-image: url("https://dit.be/wp-content/uploads/2021/01/mobiel-01.svg");
}
}
#media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
.Frontpage-image {
background-image:url("https://dit.be/wp-content/uploads/2021/01/desktop-01.svg");
}
}
#media screen and (min-width: 768px) {
.Frontpage-image{
background-image:url("https://dit.be/wp-content/uploads/2021/01/desktop-01.svg");
}
}
#media screen and (min-width: 1025px) {
.Frontpage-image {
background-image:url("https://dit.be/wp-content/uploads/2021/01/desktop-01.svg");
}
}
You can check this answer.
Here are some tips to optimize this
Use Low size images
Use Low size videos or embed them from YouTube or Vimeo
Compress this CSS code.

Why media queries don't work for mobile corectly?

У меня есть следующий код
.bg-if-premium {
background-image: url(photo_2021-05-22_20-40-40.jpg);
background-size: 100% 100% !important;
background-position: fixed;
background-attachment: fixed;
}
.bg-if-free {
background-image: url(photo_2021-04-16_16-43-00.jpg);
background-size: 100% 100%;
background-position: fixed;
background-attachment: fixed;
}
#media (max-width: 767px) {
.bg-if-premium .bg-if-free {
background-image: none;
}
}
#media (max-width: 480px) {
.bg-if-premium .bg-if-free {
background-image: none;
}
}
But on a mobile device, the background is still present. How to solve the problem?
#media screen and (max-width: 767px) {
.bg-if-premium,
.bg-if-free {
background-image: none;
}
}
#media screen and (max-width: 480px) {
.bg-if-premium,
.bg-if-free {
background-image: none;
}
}
Include the #media screen and (desired width) and like Mordred stated, separate the class identifiers by a comma.
Also since it looks like your working from a website first approach, you shouldn't have to include both of these screen queries. The first one will do the trick.

css Background images not display on iphone or properly on any device but desktop

I have a background image setup in my css and everything seems to display fine on the desktop but when I try viewing on my ipad or iphone 6 plus I don't get the image at all (not even an off-center image).
How can I be sure my images resize, center and appear on mobile?
.landpage-img {
background: url('../img/bg-neon-01-2000x1333.jpg') center center no-repeat fixed !important;
height: 1333px;
background-size: cover;
}
#media only screen and (max-width: 1024px) {
.landpage-img {
background: url('../img/bg-neon-01-1024x671.jpg') 50% 0 no-repeat center center fixed !important;
height: 671px;
background-size: cover;
}
}
#media only screen and (max-width: 991px) {
.landpage-img {
background: url('../img/bg-neon-01--991x649.jpg') 50% 80% center center no-repeat fixed;
height: 649px;
background-size: cover;
}
}
#media only screen and (min-width: 0px) and (max-width: 767px) {
.landpage-img {
background: url('../img/bg-neon-01--767x502.jpg') 50% 80% no-repeat fixed !important;
height: 767px;
background-size: cover;
}
}
I am not sure what image you are working with but this should fix the problem (no media queries needed).
body {
background-image: url(/example/example.jpg);
background-repeat: no-repeat;
background-size: cover;
}
Also, see https://www.w3schools.com/css/css_background.asp

Parallax Background Image + Browser Height Issues + Responsive Design

I've been trying to add a parallax scrolling effect to a background image on my website.
I'm having problems when the browser height changes. (It will push the image up over the text.) Adding queries using min-height or max-height doesn't seem like a very elegant solution.
Do you suggest I include different sizes of the image for different media queries, or just simply alter the background size and background position?
#headerwrap {
background-color: #3F8BE0;
min-height: 1020px;
padding-top: 85px;
padding-bottom: 0;
text-align: center;
/* Parallax scrolling effect */
background-image: url("../img/globe345.png");
background-size: 800px 800px;
background-position: 50% 155%;
background-attachment: fixed;
background-repeat: no-repeat;
}
And here are the media queries I'm using:
#media (min-width: 590px) and (max-width: 767px) {
#headerwrap {
background-size: 450px 450px;
background-position: 50% 55%;
min-height: 720px;
}
}
#media (min-width: 320px) and (max-width: 589px) {
#headerwrap {
background-size: 450px 450px;
background-position: 50% 40%;
min-height: 625px;
}
}
#media (max-height: 950px) {
#headerwrap {
background-size: 450px 450px;
background-position: 50% 75%;
min-height: 710px;
}
}

Bootstrap fullwidth container background-image redcution based on screen size via media queries

I am trying to display a smaller background-image version for mobile devices using media queries. However, the original background image is displayed regular size behind the reduced image.
Here is the css for the image in regular form.
#signup{
background-image:linear-gradient(rgba(0,0,0,0.4), rgba(0,0,0,0.7)),
image-url('zoom1028.jpg');
background-position: center center;
background-repeat: no-repeat;
background-image: fixed;
background-size: cover;
background-color: #464646;
height: 800px;
}
Here are the bootstrap media queries in same css.scss file that I am using.
/* Extra small devices (phones, less than 768px) */
/* No media query since this is the default in Bootstrap */
/* Custom, iPhone Retina */
#media only screen and (min-width : 320px) {
#signup{
background-image:linear-gradient(rgba(0,0,0,0.4), rgba(0,0,0,0.7)),
image-url('zoom1028.jpg');
background-position: center center;
background-repeat: no-repeat;
background-image: fixed;
background-size: cover;
}
}
/* Extra Small Devices, Phones */
#media only screen and (min-width : 480px) {
#signup{
background-image:linear-gradient(rgba(0,0,0,0.4), rgba(0,0,0,0.7)),
image-url('zoom1028.jpg');
background-position: center center;
background-repeat: no-repeat;
background-image: fixed;
background-size: cover;
}
}
/* Small devices (tablets, 768px and up) */
#media only screen and (min-width : 768px) {
#signup{
background-image:linear-gradient(rgba(0,0,0,0.4), rgba(0,0,0,0.7)),
image-url('zoom1028.jpg');
background-position: center center;
background-repeat: no-repeat;
background-image: fixed;
background-size: cover;
}
}

Resources