DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > CSS media queries in JavaScript

CSS media queries in JavaScript

Sagar Ganatra user avatar by
Sagar Ganatra
·
May. 16, 12 · Web Dev Zone · Interview
Like (0)
Save
Tweet
5.06K Views

Join the DZone community and get the full member experience.

Join For Free

I like the media queries feature added to CSS 3, that allows web application developers or designers to define styling sheets for a range of output devices. A media query consists of a media type and one or more expressions that limits the style sheet scope. I was wondering if JavaScript can be used to update the elements in page, say changing the source attribute of an image based on the device width. Turns out there is a function matchMedia which accepts a media query string as input and returns the result.


Here's how it works:

if (window.matchMedia('only screen and (max-width: 480px)').matches) //do something

The matchMedia function accepts a media query string and returns an object of type MediaQueryList. The matches property returns a boolean value true if the document matches the media query list. I wrote a very simple page that changes the image source when the window is resized, here's the code:

<!DOCTYPE HTML> 
<html> 
<head> 
<script> 
function onresize(event){ 
var deviceImage = document.getElementById('device-image'); 
//device is a phone 
if (window.matchMedia('only screen and (max-width: 480px)').matches) { 
deviceImage.src = "phone.png"; 
} 
//device is a tablet 
else if (window.matchMedia('only screen and (min-width: 481px) and (max-width: 1024px)').matches) { 
deviceImage.src = "tablet.png"; 
} 
//device is a notebook 
else { 
deviceImage.src = "notebook.png"; 
} 
} 

//change the image when window is resized 
window.addEventListener('resize', onresize, false); 

//show the image based on the device width 
window.addEventListener('load',onresize,false); 

</script> 
</head> 
<body> 
Resize the window 
<div align="center"> 
<img id="device-image" src="notebook.png" /> 
</div> 
</body> 
</html>

Here I'm checking for the min-width and max-width properties and then based on these values I'm changing the source attribute of the image. Here's the working demo:

 

CSS Media (communication) Media queries Database JavaScript

Published at DZone with permission of Sagar Ganatra, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Slowness in Java Application Due To Increased FullGC Events: G1GC
  • The Engineer’s Guide to Creating a Technical Debt Proposal
  • How Many GPUs Should Your Deep Learning Workstation Have?
  • How a Low-Code API Platform Delivers Developer Productivity

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo