DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. Embracing All Media for Your Message with CSS3 Media Queries

Embracing All Media for Your Message with CSS3 Media Queries

John Esposito user avatar by
John Esposito
·
Nov. 22, 11 · Interview
Like (0)
Save
Tweet
Share
4.06K Views

Join the DZone community and get the full member experience.

Join For Free

Some media, of course, the developer doesn't need to worry about -- you don't need to track (AM)OLED technology, for example, in order to develop for mobile.

But at some level media do matter, and for these the developer has often been forced to make some tough decisions.

For example: you want to display lots of site navigation on the user's screen -- but you don't want your site navigation to print, because hyperlinkable paper hasn't become cheap enough yet.

CSS2.1 could already handle the coarse-grained distinction between screen and print styles. The code to do this was simple, e.g.:

<link rel="stylesheet" type="text/css" media="screen" href="sans-serif.css">
<link rel="stylesheet" type="text/css" media="print" href="serif.css">

in the html doc and

@media screen {
  * { font-family: sans-serif }
}

in the css file.

Print and screen were by far the most common media types, though a few other 'media types' could also be specified in HTML4/CSS2 (braille, for example).

But these days, while 'print' means more or less the same as it did ten years ago (at least until e-ink and OLED destroy all competitors), 'screen' really doesn't. In fact, with resolutions, form-factors, and even contrast-levels proliferating like lop-eared rodents, modern web developers need to worry about perhaps a dozen different possible display media. 

Wherefore was introduced the new CSS3 Media Queries specification: not a larger set of media types, but a way for CSS to ask and receive true/false information about the user's display (or, in principle, other media type; but realistically this probably means screen).The CSS3/HTML5-compliant browser can then make styling decisions, specified in the style sheet, depending on the true/false answers to these queries.

For example, instead of specifying a media type (media=print), an HTML5 <link> tag can specify a stylesheet to follow depending on media properties:

<link rel="stylesheet" media="screen and (color)" href="example.css" />

and a stylesheet can run logical checks on media properties using CSS grammar (roughly the same as CSS2.1 grammar).

CSS3 can query lots of media features:

  • width
  • height
  • device-width
  • device-height
  • orientation
  • aspect-ratio
  • device-aspect-ratio
  • color-index
  • monochrome
  • resolution
  • scan (progressive or interlaced)
  • grid


some of which would have helped in the old GeoCities days (e.g., width), others will probably become useful only in our highly graphical HTML5 future (e.g., scan).

This is all really cool -- foundational if not sexy (unless you like circuits, in which case it is sexy) -- but might be easier to follow in another medium.

So here's a little video of David Powers speaking at the latest London HTML5 User Group meetup, with full explanation and quite a few notes on browser and device support.

Enjoy -- on your desktop! or iPad! or Lumia 800! or any medium whatsoever.

CSS Media (communication) Media queries Database

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Seamless Integration of Azure Functions With SQL Server: A Developer's Perspective
  • How To Perform Local Website Testing Using Selenium And Java
  • Navigating Progressive Delivery: Feature Flag Debugging Common Challenges and Effective Resolution
  • When Should We Move to Microservices?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: