Improving CSS performance for a specific media query

Hey there,

I recently implemented a media query on my website to display different images based on the device's screen size. However, I'm noticing that it may be causing some performance issues due to lack of optimization. On mobile devices, a specific image is shown, while tablets and desktops display another image. Although the media query is functional, I believe it can be optimized for better efficiency. Would appreciate any assistance in optimizing it effectively.

This is the current 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;
    }
}

Answer №1

Feel free to utilize the following CSS code for your project.
It's worth noting that styles defined outside of a media query are inherited within the query, resulting in a reduction in code length while maintaining functionality.

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");
    }
}

To learn more about inheriting values in media queries, you may find this answer helpful.

Answer №2

Below are a few suggestions for enhancing this

  1. Opt for smaller images.
  2. Opt for smaller videos or utilize YouTube or Vimeo embeds instead.
  3. Condense the CSS code.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to ensure that the background of my sticky table th stays in place and does not scroll away

I have a dynamic table where the table body rows are loaded dynamically. The wrapper uses Bootstrap5's overflow-scroll feature to keep the table at a fixed height and scroll if there are too many rows. I managed to make the table head sticky, but had ...

What is the best way to conceal an element so that it only becomes visible when a user begins to input text

Hey there! I'm in the process of adding a search feature to my Jekyll site, and I've opted to use Simple-Jekyll-Search, which you can check out here: Link. Take a peek at what's inside my search.html: <input type="text" id="my-search-in ...

Change the visibility of a div's content when a radio button is selected with only CSS

When the radio buttons are on the same div level as "content1" and "content2", it works properly. But how can I make it work if I move the radio buttons to another div outside of the "second" div? For example, if toggle1 is checked then content1 should be ...

Building CSS columns

Similar Inquiry: Creating dual columns in HTML/CSS layout I'm exploring ways to achieve a two-column layout within a wrapping div element. Currently, I have the following CSS code snippet which centers a div inside the browser: .wrapper {width: ...

Preventing flexbox containers from being affected by overflow elements

I am looking to implement overflow: scroll within a flexbox layout. I am unsure of how to link an image directly from my Google Drive, so I have provided the URL instead: https://drive.google.com/open?id=17uM5twoprxmbo5xQ37kgeTaMwn7FGSu_ When using the o ...

Use the href attribute along with an id to redirect to a different page and simultaneously update the class to

On my website, I have two main pages: the home page and the contact us page. The contact us page features 4 menu options, with the first option currently active. I would like to set it up so that when a user clicks on the home page button, it will navigate ...

Border dividing vertically of equal length

My goal is to create a table-like appearance in a simple bootstrap grid using the row and col- classes, with vertical dividers that extend the full length of the tallest column. I have created a demo which shows that traditional methods only allow the div ...

Is it possible to eliminate units from a calculation function within CSS code?

I've been working with the rule below: margin-left: calc(((100vw - 624px) / 144) * 5); At a viewport width of 1200px, this translates to 20px. However, I would prefer to have a result of 20%. To achieve this, it would be ideal to change the integer ...

The color of text is being altered by IE 7's gradient filters

I'm facing an issue with the CSS code applied to a button (along with other styles): border: 1px solid #86A74B; border-top: 1px solid #B0C5B0; background-color: #91B248; background-image: -moz-linear-gradient(top, #9BBC52, #84A440); background-image: ...

Customizing table header sort icons in PrimeNG 15.4+: A step-by-step guide

The most recent update in PrimeNG brought about a change in the way sort icons are implemented. Previously, they used an i tag with CSS classes which could be customized easily using my company's icons: https://i.sstatic.net/f0Nrq.png However, the n ...

Reducing Image Size in JavaScript Made Easy

I need help with a project where I want the image to shrink every time it's clicked until it disappears completely. I'm struggling to achieve this, can someone assist me? Here is the HTML code I have: <html lang="en" dir="l ...

Shift the sleek navigation arrows to the interior of the slides

Is there a way to relocate the navigation arrows on the slider images? I have tried various methods found online with no success. Currently using ASP.NET, but doubt it matters. Employing the latest version of SLICK. Here is the HTML code snippet: <%@ ...

Is there a way to navigate to a specific element inside a div with overflow-y enabled?

I have a div with a max-height of 300px, causing a scrollbar to appear when the content exceeds this limit. I am seeking a way to click a button and automatically scroll to a specific element within that div. While I know how to manipulate the main browser ...

The div element is not resizing properly when the phone is in landscape mode

I have set a background photo to take up 100% of the height. However, when I view the site on a mobile phone in landscape mode using Chrome or Firefox, the background image does not fill the entire space. In desktop and portrait mobile modes, everything ...

Customizing your website's font styles for various browsers

I encountered an issue when using fonts with @font-face: Chrome was not applying the font-weight rule correctly, resulting in poor looking captions. Despite my searches, I could not find a solution specific to my case. As a workaround, I decided to use a d ...

When the browser size shrinks, turn off the drop-down navigation menu

I'm currently working on a responsive website, and I have encountered an issue with a dropdown menu. When the browser size is reduced to 900px, the menu shifts all the way to the left side. To address this, I added some left padding to keep it off the ...

Adjusting the parent div width to match the child div width

Here is the code I am working with: <div class="head_title"> <img src="/path/to/image.png"> <h1 id="entryTitle"> <!--Cufon Font--> </h1> </div> I am trying to make the img element stretch to the full width of ...

Bootstrap modals are refusing to open

I have encountered an issue where the images are not displaying on my modal. I have tried changing both the images and the code itself, but being new to this, I am unsure of what exactly is causing the problem. Could it be a fault in the Javascript code or ...

Accordion menu with smooth CSS3 transitions

I created a unique accordion menu to replace the select form control, and I am interested in using CSS3 transitions to give it a smooth expand and contract effect. Feel free to check out my work on jsfiddle: http://jsfiddle.net/hKsCD/4/ In order to achi ...

Placing content in bottom and moving towards the left side

On my webpage, I have a div that I would like to fill with content similar to the image. How can I achieve this without resizing the div and instead populate it from bottom to top? ...