Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
How to change the src of an image dynamically with media queries in css or sass

I have a responsive website created with bootstrap and sass. I load the background image in the home page dynamically using bootstrap break points in sass.

At the same page I have other images that load heavily. I have created a way to load images based on width such that <img src='assets/img/myImage?width=500'> so that I reduce the size based on the viewport. But I coulnd't find a way to control the src of the img tag in sass file so that I use @media query and decide the width dynamically.

Any idea how to implement this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can't.

Images are content, not style. You can't use CSS to change them.

You can use media queries to change them, but you need to use a <picture> element to do so, not CSS.

Here is an example from MDN:

<picture>
    <source srcset="/media/cc0-images/surfer-240-200.jpg"
            media="(min-width: 800px)">
    <img src="/media/cc0-images/painted-hand-298-332.jpg" alt="" />
</picture>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!