This is part question, and part hope for confirmation. I have my suspicions about the answer to this, and will add my two-cents, but I'd like independent confirmation since the Win8 Forums don't have this particular feature documented very well that I could find.
In Windows 8, when building Metro Style Applications using HTML/JS/CSS, the templates all include a series of CSS Media Queries in default.css. They are:
#media screen and (-ms-view-state: fullscreen-landscape) {}
#media screen and (-ms-view-state: filled) {}
#media screen and (-ms-view-state: snapped) {}
#media screen and (-ms-view-state: fullscreen-portrait) {}
I'm quite familiar with Media Queries, so my questions are: Why the need for vendor-prefixed expressions in Windows 8, and how do the -ms-view-state expressions differ from the W3C standard expressions like width, height and orientation?
Here's my take, please add yours below, and confirm, deny or correct me: I suspect that these rules are similar to the standard rules, but are a bit more adaptive, meaning that rather than defining sets of rules that match to a given width and height, these expressions encompass a state in a resolution-agnostic way. Because of this, I can use a single rule for my application in landscape mode -ms-view-state: fullscreen-landscape and it will work when the app is running at 1366x768, 1920x1080 or 2560x1440. Otherwise, I would have to define these rules three times:
#media screen and (width: 1366px) and (height: 768px) {}
#media screen and (width: 1920px) and (height: 1080px) {}
#media screen and (width: 2560px) and (height: 1440px) {}
Assuming my assertion is true, the only gap for me is why I couldn't then just define a single rule using 'min-' and orientation to achieve the same result:
#media screen and (min-width: 1366px) and (min-height: 768px) and (orientation: landscape)
Single rule, standard expressions. No need for a vendor-prefixed version. What am I missing?
In response to your two questions
Why the need for vendor-prefixed expressions in Windows 8?
Because view-state is not a part of the official CSS3 spec and vendor prefixes are there precisly so vendors can independently try new things outside the spec without breaking it for everyone else.
How do the -ms-view-state expressions differ from the W3C standard expressions like width, height and orientation?
They appear to be more semantic in their meaning and less restrictive on actual screen resolutions or sizes.
And while yes, your css media query of #media screen and (min-width: 1366px) and (min-height: 768px) and (orientation: landscape) does infact reduce the three lines youd previously posted down to one, it dosnt really describe what the -ms-view-states look like their trying to achieve.
They seem to be trying to give the developer/designer context to how their application is currently being consumed, explicitly stating the app is running either fullscreen or snapped (which i assume is the side by side mode microsoft have demostrated for having multiple apps running in metro simultaneously).
Update
Microsoft have released a blog post talking about their design decisions around screen resolutions and such. Which is pertinant to this conversation and is available here
Related
I want to change the font size of my page when it is viewed on galaxy fold device. But I am not sure how to deal with this using media queries. Can anyone help me and give me an idea about how can I remedy this ?
If you need media query for folded version of device you can use 320px screen width.
There is no other device with such screen width from popular ones so you can simply use this media query. Alternatively you can use JS module from npm to detect device and change font size dynamically
#media(max-width: 320px){
font-size: 10px
}
if you use just #media(max-width: 320px) , the code will be effect on others devices example : (Galaxy S8 Galaxy s7 ... ), use this css to fix the problem :
#media (min-width: 280px) and (max-width: 320px) { .your-class { font-size: 10px; } }
As of right now, there is a draft for a device posture API. Formerly it was the 'Screen Fold' API that would allow media queries of the type:
#media (device-posture: laptop) and (spanning: single-fold-horizontal){}
This would generally solve the issue of responsiveness for folding screens.
Sadly this isn't implemented yet, and alternatives like using a navigator.useragent property are unreliable and not recommended.
It's still in draft stage(as of Sep, 2022), but there's #container media query: https://drafts.csswg.org/css-contain-3/
In MDN web docs,
This doesn't quite achieve what media queries do for our entire layout however. Media queries give us the ability to size things based on ranges. When we add a class or target the element we decide that when the object is in the sidebar it must use the stacked layout. In terms of available space however, it may well be that on large screens the object in the sidebar would have enough space to display in the side-by-side layout.
Meanwhile, I think #media is the most viable option available for now as #Gîrbu Nicolae stated
#media (min-width: 900px) and (max-width: 911px) and (orientation: portrait) {}
Tolerance ~5px, working fine for me, I test it
Client has the following display settings on Windows Surface. He is seeing a website quite differently that to what I am seeing. What CSS media query can I use to be able to make adjustments to allow for these specific settings
I would start with:
#media only screen and (min-width: 2736px) and (min-height: 1824px) and (orientation: landscape) {
...
}
You can also detect it via JS.
Read this: How to check if device is Windows Surface Tablet and browser is chrome or IE
And if it's a Surface, add some CSS class dynamically to the appropiate elements.
For my CSS media queries I've set it up mobile first which deals with all the overall styling.
I then have:
#media only screen and (min-width : 790px) {
}
#media only screen and (min-width : 990px) {
}
and I've added in
#media screen and (orientation: landscape) and (max-width: 520px) {
}
which deals with the CSS changes when the smart phone is turned round to landscape mode, but it doesn't seem to work, am I writing the landscape media query wrong?
Had a similar issue: my iPod/iPhone devices were detected as portrait orientation even when rotated.
I managed to resolve that with the following media query:
#media screen and (min-aspect-ratio: 4/3)
{*your styles here*}
If you want to target any case when width is greater than height, I think something like (min-aspect-ratio: 101/100) or something like this might work. However, for current devices 4/3 is sufficient, I think.
If you need aspect ratio for landscape, I think min-aspect-ratio: 1 suffices ... and therefore max-aspect-ratio: 1 for portrait.
But, even when the CSS is correct, there's an additional step required for a Cordova / PhoneGap app: Why doesn't my Cordova/PhoneGap iOS app rotate when the device rotates?
I found this StackOverflow item before that one, so perhaps others will also find a cross-link useful.
When writing a CSS media query, is there any way you can specify multiple conditions with "OR" logic?
I'm attempting to do something like this:
/* This doesn't work */
#media screen and (max-width: 995px OR max-height: 700px) {
...
}
Use a comma to specify two (or more) different rules:
#media screen and (max-width: 995px),
screen and (max-height: 700px) {
...
}
From https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
Commas are used to combine multiple media queries into a single rule. Each query in a comma-separated list is treated separately from the others. Thus, if any of the queries in a list is true, the entire media statement returns true. In other words, lists behave like a logical or operator.
CSS Media Queries & Logical Operators: A Brief Overview ;)
The quick answer.
Separate rules with commas:
#media handheld, (min-width: 650px), (orientation: landscape) { ... }
The long answer.
There's a lot here, but I've tried to make it information dense, not just fluffy writing. It's been a good chance to learn myself! Take the time to systematically read though and I hope it will be helpful.
Media Queries
Media queries essentially are used in web design to create device- or situation-specific browsing experiences; this is done using the #media declaration within a page's CSS. This can be used to display a webpage differently under a large number of circumstances: whether you are on a tablet or TV with different aspect ratios, whether your device has a color or black-and-white screen, or, perhaps most frequently, when a user changes the size of their browser or switches between browsing devices with varying screen sizes (very generally speaking, designing like this is referred to as Responsive Web Design)
Logical Operators
In designing for these situations, there appear to be four Logical Operators that can be used to require more complex combinations of requirements when targeting a variety of devices or viewport sizes.
(Note: If you don't understand the the differences between media rules, media queries, and feature queries, browse the bottom section of this answer first to get a bit better acquainted with the terminology associated with media query syntax
1. AND (and keyword)
Requires that all conditions specified must be met before the styling rules will take effect.
#media screen and (min-width: 700px) and (orientation: landscape) { ... }
The specified styling rules won't go into place unless all of the following evaluate as true:
The media type is 'screen' and
The viewport is at least 700px wide and
Screen orientation is currently landscape.
Note: I believe that used together, these three feature queries make up a single media query.
2. OR (Comma-separated lists)
Rather than an or keyword, comma-separated lists are used in chaining multiple media queries together to form a more complex media rule
#media handheld, (min-width: 650px), (orientation: landscape) { ... }
The specified styling rules will go into effect once any one media query evaluates as true:
The media type is 'handheld' or
The viewport is at least 650px wide or
Screen orientation is currently landscape.
3. NOT (not keyword)
The not keyword can be used to negate a single media query (and NOT a full media rule--meaning that it only negates entries between a set of commas and not the full media rule following the #media declaration).
Similarly, note that the not keyword negates media queries, it cannot be used to negate an individual feature query within a media query.*
#media not screen and (min-resolution: 300dpi), (min-width: 800px) { ... }
The styling specified here will go into effect if
The media type AND min-resolution don't both meet their requirements ('screen' and '300dpi' respectively) or
The viewport is at least 800 pixels wide.
In other words, if the media type is 'screen' and the min-resolution is 300 dpi, the rule will not go into effect unless the min-width of the viewport is at least 800 pixels.
(The not keyword can be a little funky to state. Let me know if I can do better. ;)
4. ONLY (only keyword)
As I understand it, the only keyword is used to prevent older browsers from misinterpreting newer media queries as the earlier-used, narrower media type. When used correctly, older/non-compliant browsers should just ignore the styling altogether.
<link rel="stylesheet" media="only screen and (color)" href="example.css" />
An older / non-compliant browser would just ignore this line of code altogether, I believe as it would read the only keyword and consider it an incorrect media type. (See here and here for more info from smarter people)
FOR MORE INFO
For more info (including more features that can be queried), see: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries#Logical_operators
Understanding Media Query Terminology
Note: I needed to learn the following terminology for everything here to make sense, particularly concerning the not keyword. Here it is as I understand it:
A media rule (MDN also seems to call these media statements) includes the term #media with all of its ensuing media queries
#media all and (min-width: 800px)
#media only screen and (max-resolution:800dpi), not print
#media screen and (min-width: 700px), (orientation: landscape)
#media handheld, (min-width: 650px), (min-aspect-ratio: 1/1)
A media query is a set of feature queries. They can be as simple as one feature query or they can use the and keyword to form a more complex query. Media queries can be comma-separated to form more complex media rules (see the or keyword above).
screen (Note: Only one feature query in use here.)
only screen
only screen and (max-resolution:800dpi)
only tv and (device-aspect-ratio: 16/9) and (color)
NOT handheld, (min-width: 650px). (Note the comma: there are two media queries here.)
A feature query is the most basic portion of a media rule and simply concerns a given feature and its status in a given browsing situation.
screen
(min-width: 650px)
(orientation: landscape)
(device-aspect-ratio: 16/9)
Code snippets and information derived from:
CSS media queries by Mozilla Contributors (licensed under CC-BY-SA 2.5). Some code samples were used with minor alterations to (hopefully) increase clarity of explanation.
There are two ways for writing a proper media queries in css. If you are writing media queries for larger device first, then the correct way of writing will be:
#media only screen
and (min-width : 415px){
/* Styles */
}
#media only screen
and (min-width : 769px){
/* Styles */
}
#media only screen
and (min-width : 992px){
/* Styles */
}
But if you are writing media queries for smaller device first, then it would be something like:
#media only screen
and (max-width : 991px){
/* Styles */
}
#media only screen
and (max-width : 768px){
/* Styles */
}
#media only screen
and (max-width : 414px){
/* Styles */
}
yes, using and, like:
#media screen and (max-width: 800px),
screen and (max-height: 600px) {
...
}
Yes, but not by using OR, you need to use and. Like,
#media screen and (max-width: 995px) and (max-height: 700px) {
...
}
And also, we can do it this way. The comma implies OR relationship,
#media screen and (max-width: 995px),
screen and (max-height: 700px) {
...
}
More information - Click here
I'm making a website which has 3 breakpoint 768px, 1024px and 1900 px. Which size of CSS is good to keep outside media query containers?
Adding example
All specific styling inside media queries and all common styling outside
h1 {color:red}
#media only screen and (min-width: 480px) {
h1 {font-size:18px}
}
#media only screen and (min-width: 768px) {
h1 {font-size:22px}
}
#media only screen and (min-width: 1024px) {
h1 {font-size:28px}
}
or
Most common used desktop first
#media only screen and (min-width: 1024x) {
h1 {font-size:28px; font-color:red}
}
#media only screen and (min-width: 480px) {
h1 {font-size:18px}
}
#media only screen and (min-width: 768px) {
h1 {font-size:22px; }
}
or
Mobile first
#media only screen and (min-width: 480px) {
h1 {font-size:18px; font-color:red}
}
#media only screen and (min-width: 1024x) {
h1 {font-size:28px; }
}
#media only screen and (min-width: 768px) {
h1 {font-size:22px; }
}
I believe you mean to ask what CSS should not be inside of the media query blocks, right?
If that is the case I recommend that any CSS that does not change be placed outside of the media query blocks. Any colors, font styling, etc. Any CSS that changes placement of elements, the padding, floats, inline or block display types, any structure-type CSS is what I would put in the media query blocks.
Update: To respond to the updated question, are you asking which order you should put the media blocks in? If that's the case as far as I know it doesn't really matter what order they go in. But to comment on the number of possible media queries, I would separate that CSS into different style sheets just to make it more maintainable. Your media queries would then be a part of the links to your style sheets in your HTML.
There are so many ways to approach this problem - and the decision may be different depending on the circumstances. For example, is there an existing site that you are reverse engineering to be responsive or are you starting from scratch?
STARTING FROM SCRATCH
If starting from scratch, one method is to create all of the basic styles OUTSIDE of any media query - so that these styles can be seen by any device (especially those devices that do not support media queries).
Basic styles could include just colors, and fonts etc - or it could be everything except layout.
Then, media queries are used to add the different layouts on top of the basic styles.
MIN or MIN AND MAX
The next question is how will you work your different media queries...
Will you allow them to be applied on top of one another - in which case you may start small and build up - using min-width only.
For example:
#media only screen and (min-width: 600px)
OR you may want to set them in a series of brackets - so that styes for one size do not interact with another size.
For example:
#media only screen and (min-width: 600px) and (max-width: 800px)
Again, there is no right or wrong - both have strengths and weaknesses. The first option allows you to use styles that flow through all widths. The second option allows you to fully control styles that appear in a specific width - without having to deal with the cascade.
DEALING WITH IE
There are a range of ways for dealing with older versions of IE including.
allow IE to see basic styles only
place media queries in separate CSS files and link to these files using media queries... then also link to a selection of these files (like wide screen CSS files only) via conditional comments.
Use some sort of JS solution like respond.js or others to force IE to understand the media queries.
HTH
I've read many articles recently that suggest starting with the smallest resolution first and working your way upwards using media queries. To me that also makes a lot of sense. The only problem is old browsers (IE) not understanding media queries. There are solutions to that problem though (if you Google).