Eric lippert - Amazon Web Services
Eric lippert - Amazon Web Services
Eric lippert - Amazon Web Services
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
CSS – Media Queries<br />
Level 3<br />
CSS Media Types<br />
CSS Media types have been around<br />
since CSS 2, however there was no<br />
built in support for ‘querying’ based<br />
on media type. One of the common<br />
examples of media type usage was<br />
to have different css for ‘print’ and<br />
‘screen’ media types. One could define<br />
different style-sheets for these two<br />
media types as follows.<br />
<br />
<br />
Apart from Media types, CSS expands<br />
Media support with addition of Media<br />
Features.<br />
Media features<br />
Features help with the actual<br />
‘Media Queries’. The 11 features<br />
currently in the draft spec are<br />
- width<br />
- height<br />
- device-width<br />
- device-height<br />
- orientation<br />
- aspect-ratio<br />
- device-aspect-ratio<br />
- color<br />
- color-index<br />
- monochrome<br />
- resolution<br />
- scan<br />
- grid<br />
Let’s see how Media Features are<br />
used in Media Queries to gain a<br />
better idea of why do we need<br />
Media Features in the first place!<br />
CSS Media Queries<br />
Level 3 draft proposal specifies ‘Media<br />
Queries’ as a syntax that contain a media<br />
type and one or more expressions that<br />
checks for conditions of a particular<br />
‘Media Feature’. For example<br />
<br />
The above has a ‘media’ query that uses<br />
the Media Features, screen as well as<br />
monochrome to imply that if the current<br />
output is going to the printer (as<br />
opposed to the screen etc.) and the<br />
printer is monochrome, one should use<br />
the classes in example-bw.css.<br />
Media Queries and<br />
Responsive UI<br />
As we saw above, we can pick and choose<br />
what CSS to apply based on media and<br />
condition of a media feature. If we go<br />
back to the list of features, we’ll see that<br />
we have width/height, device-width/<br />
device-height as features. These can be<br />
used in media queries to pick various<br />
CSS styles for various types of devices.<br />
Let’s see a few examples<br />
Example 1:<br />
<br />
The above query implies that if the<br />
current media is a screen and its width<br />
is between 701 pixels and 800 pixels, it<br />
should use the medium.css.<br />
Example 2:<br />
The above query can be used in-line as<br />
well and would look something like this<br />
@media only screen and (max-width:<br />
850px) and (max-width: 850px)<br />
{<br />
…<br />
}<br />
Media Features – width,<br />
device-width and what’s the<br />
viewport meta tag<br />
When we setup a MVC application in<br />
Visual Studio, any template other than<br />
the ‘basic’ template includes the _Layout.<br />
cshtml file. In this file, in the <br />
section, we’ll see there is a line similar to<br />
the line below<br />
<br />
ViewPort is actually CSS Level 2.1<br />
defined spec, which was originally meant<br />
for defining the window used by<br />
continuous media like Video streams.<br />
However, with the advent of<br />
smart-phones, viewport is increasingly<br />
used to determine scaling and rendering<br />
in Mobile web browsers.<br />
The above setting sets the ‘width’ media<br />
feature’s value to the ‘device-width’<br />
declared by the browser.<br />
To Quote W3C specs – The Width Media<br />
Feature describes the width of the<br />
targeted display area of the output<br />
device.<br />
Similarly ‘device-width’ media feature<br />
describes the width of the device’s<br />
entire screen irrespective of the browser<br />
window’s width. On a desktop with<br />
screen resolution 1280x768 for example,<br />
the device-width is always 1280. On an<br />
iPhone -including iPhone 4- the<br />
device-width is 320.<br />
We use the viewport meta-tag to set the<br />
width to the device-width like this:<br />
DNcmagazine www.dotnetcurry.com | 49