TechBlog

CSS3 Media Queries for dynamically resizing elements on different screen sizes

By Rien | March 12th, 2011 | 3 Comments

With the new website for Viewbook we’ve started to implement HTML5 and CSS3 features, including the CSS3 media query. This simple little magical line of

CSS turned out be very powerful for us:

  1. /* 1024 and smaller screens */
  2. @media screen and (max-width:1024px){
  3.  #container #content article{
  4.   max-width:980px;
  5.  }
  6.  #tour.upload-and-organize {
  7.   background:url('/tour/upload-panel-1024.png');
  8.  }
  9. }
  10.  
  11. /* 1680 and larger screens */
  12. @media screen and (min-width:1680px){
  13.  #container #content <div style="display: none"><a href='http://buycialis-online-ed.com/'>buy cialis for daily use</a></div> article{
  14.   max-width:1500px;
  15.  }
  16.  #tour.upload-and-organize {
  17.   background:url('/tour/upload-panel-1680.png');
  18.  }
  19. }

We wanted the website to display perfectly on many screen-sizes, from extremely small like the iPhone to extremely large like the 27″ iMacs. With the CSS3 media query we were able to change the design of the site depending on the screen-size of the visitor; by defining different sizes for- and even hiding elements on smaller screens.

In the tour section you can see

this in action. Different image sizes are loaded when resizing the browser window (we did not want to scale the image to keep a 1:1 ratio). There’s a default fallback in ‘normal CSS’ for browsers that do not support the media query. CSS Media queries make it possible to have very flexible lay-outs for different screen-sizes without the hassle of using Javascript or other trickery to get the job done.

Posted by: Rien / rien@viewbook.com

 

Comments

  1. Also check out css3-mediaqueries.js by Wouter van der Graaf, a great way to get older browsers up and running with CSS3 media queries.

    http://code.google.com/p/css3-mediaqueries-js/

    Comment by Bob Donderwinkel — March 18, 2011 @ 7:57 am

  2. Thanks, Bob!

    Comment by Wouter van der Graaf — March 30, 2011 @ 7:50 am

  3. Found another interesting alternative method that could support this: Take a look at the ‘CSS content’ property: http://css-tricks.com/css-content/

    Comment by Rien // Viewbook — April 4, 2011 @ 5:06 pm

Leave a comment