Using WebP in Sass can be achieved by converting your images to the

I have this code in my SCSS file:

.banner{
    background-image: url("/images/image.jpg");
}

Now, I've switched to a WebP image format. I know how to implement it in HTML like this:

<picture>
    <source type="image/webp" srcset="images/image.webp">
    <source type="image/jpeg" srcset="images/image.jpg">
    <img src="images/image.jpg">
</picture>

But how can I achieve something similar within a Sass SCSS file? (I'm working with Reactjs)

Answer №1

It is not possible to determine webp support using CSS alone.

Your best option is to use JavaScript to detect support and apply a class to an element (such as with modernizr). As the body element is typically beyond the reach of React-modified elements, adding the class there with modernizr should work seamlessly.

You can then utilize this class to determine which style rule to apply:

.banner{
    background-image: url("/images/image.jpg");
}

.webp .banner{
    background-image: url("/images/image.webp");
}

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

Converting dates from JavaScript to Central Daylight Time (CDT) using the new

I have a function below that is used to format a date from an API Response. However, when the date "2021-10-02" is received in the response, the new Date() function converts it to Mon Oct 01 2021 19:00:00 GMT-0500 (Central Daylight Time), which is one da ...

Guide to deploying a Next.js application with React and Redux integration on cPanel

When it comes to transferring static files from the build folder in React to cPanel, Direct Admin, or another shared server, there are established methods. But what about Next.js? Is there a solution that doesn't involve using Node.js and installing ...

Retrieving InnerHTML of a Span Element with Selenium

<div id="ctl00_cphRoblox_rbxGroupFundsPane_GroupFunds" class="StandardBox" style="padding-right:0"> <b>Funds:</b> <span class="robux" style="margin-left:5px">128</span> </div> So, I am trying to retrieve the value 128 ...

Troubleshooting Z-Index Problem in Internet Explorer 7 with CSS Dropdown

Despite trying various solutions with JQuery and CSS Tricks, I'm encountering a problem where my drop-down menu won't display properly over my accordion slider in IE7. This issue doesn't appear in other browsers. Any assistance on resolving ...

The value retrieved from redux appears to be varying within the component body compared to its representation in the return

Trying to fetch the most recent history value from the redux store to pass as a payload is presenting a challenge. When submitting a query, the history updates and displays the latest value within the map() function in return(), but when checking at // CON ...

Guide to implementing headers in axios.create within a React/Redux application

I'm encountering an issue while trying to retrieve JSON data in my react-redux application using axios.create. The middleware thunk is already installed, but I keep receiving a "No 'Access-Control-Allow-Origin' header is present on the reque ...

Having successfully configured and published Google Tag Manager, unfortunately, I am encountering difficulties in displaying the tag on the website

After setting up my GTM account and creating containers, tags, etc., I encountered an issue. Even after publishing my container and creating a version, when I checked my website, all the code was hidden within a div tag with display none and visibility hid ...

Ways to implement a Try Again button in React Error Boundary to reset state

My Pokemon app relies on React Hook Form, React Query, and React Error Boundary libraries for functionality. When there are no requests made yet, the message No Pokemon Yet, Please Submit a Pokemon should be displayed. In case of an error, a Try Again but ...

The embedded iFrame is failing to render the PDF page

I am attempting to insert a PDF into an iFrame. The src attribute is being set to the URL retrieved from a database. <div class="row"> <div class="col-md-8"> <iframe runat="server" src="{{quotat ...

How to prevent redundancy in CSS styling

Exploring CSS on my own and have the following HTML structure: <style type="text/css"> #content { display: block; width: 250px; height: 50px; background-color: #330000; } /* pink */ #one, #two, #three, #four { height: 25px; width: 25px; float: left ...

Tips for aligning div columns in HTML and CSS

My divs are displaying different heights based on content. I need a solution to make them all the same height without using bootstrap framework. Is there a way to achieve this with plain html/css, or perhaps with javascript/jquery/angularjs? Take a look ...

Is it achievable through CSS alone to eliminate the bullet point in a UL list when it consists of just a single item?

Is it feasible to achieve a solution using CSS alone that remains functional even on IE8, ensuring the following criteria: If a <ul> list contains 2 or more items, then maintain the bullets in front of each item as per usual. When there is only one ...

Using CSS to Align Two Images in a Relative Position

Imagine a scenario where you have one picture with a maximum width of 1200px and a relative width of 85%. Now, on top of this image lies another smaller picture that acts as a link. This second picture was cropped from the first to serve its link purpose. ...

Ways to perfectly align objects in a container without compromising the height of the parent element

Within my container div, I have two spans. An issue arises when I attempt to align the items to the center using align-items: center, as this causes the spans to lose their defined height and border-radius properties. My goal is to keep the heights and bor ...

"Pushing elements into an array does not function properly within a promise

I'm having trouble with my code - the push method isn't working and it's not returning anything. import {nearbyUsers, getLatitude, getLongitude} from './helper' const users = [] nearbyUsers(session, getLatitude(), getLongitude()).t ...

Aligning a lowercase "i" element within a container

Is there a more effective way to center the "i" element within this div without using specific pixel margins that adjust based on hover? Below is my code snippet: HTML: <div class="nav-collapse"> <i class="fa fa-bars fa-2x" id="bars"></ ...

What triggers the activation of the inline formatting context in this particular scenario?

MDN provides insight about the img element in regard to styling with CSS, highlighting how images interact within an inline formatting context. The positioning of images when used in-line can be affected by vertical-align: baseline due to <img> not ...

Distinguishing between a video or image when uploading files in JavaScript

I've been researching whether JavaScript can determine if a file is a video or an image. However, most of the information I found only shows how to accept these types using the input tag. What I really need is for JS to identify the file type and the ...

Creating mock element in React testing library to simulate document.getElementById

When working with headless UI components like Dialog and Transition, I ran into an issue while writing a test case. The error message I received was Cannot read properties of null (reading 'getElementById'). I am using React testing library to wr ...

A step-by-step guide on using a loop to display three images per row

The issue at hand is quite logical... I'm attempting to create a photo gallery by fetching image URLs from a database... The problem arises when I try to display them in HTML. I'm using bootstrap for the layout, where each row should contain 3 im ...