Why do some #media not get read instead of non #media counterpart? - html

I am facing a situation where for one of my classes, the #media query is just not being read, instead, the case where the class is not wrapped around #media query is being read even though the #media criteria is being met.
This is the code:
#media (min-width: 768px){
.buttonClass{
position: absolute;
width: 25%;
}
}
and the class not wrapped in #media:
.buttonClass{
position: absolute;
width: 50%;
}
I know I can solve this problem by specifying an #media query for both the classes with the appropriate resolution, but I just wanted to know why the #media query for screen sizes with widths larger than 768px was not being read.
In another case where I have 2 classes that modify the body and head tags, one of the them is wrapped in #media (width 768px or greater) and the other is not wrapped around anything. So basically it is exactly the same as the one above, expect it involves head/body tags. This seems to work as expected which is what is confusing me.
What do I do when faced with this situation?
Any help would be really appreciated.

#media screen and (min-width: 768px) {
.buttonClass{
position: absolute;
width: 25%;
}
}
Only executes when the viewport is 768px wide or wider, so even if the viewport is 2000px, this code still executes.
If you change the query:
#media screen and (max-width: 1000px) and (min-width: 768px)
This exectues when the browser's width is between from 768 to 1000px.
Are you thinking the opposite?
When none of the #media conditions are met, then css without #media executes:
.buttonClass{
position: absolute;
width: 25%;
}
And btw, i don't know if its just me, but it is very hard to understand your logic....mainly
but I just wanted to know why the #media query for screen sizes with
widths larger than 768px was being read even though the screen size is
larger.
Isn't this suppose to happen?

There are two possible reasons I can think of:
1.) wrong filepath when referencing the CSS file - but that would only apply if you are using more than one CSS file, or one file and the other CSS in a 'style' tag in the header of the HTML page.
2.) The reversed order of the two rules: The media query rule has to be read after the general rule, otherwise the general rule will override the MQ rule since it applies to everything. So if they are both in the same file, the MQ has to be below the general rule, if they are in two sperately referenced files, the file with the MQ has to be referenced after the one with the general rule/s.

Related

#media (max-width: not in concordance with set defined value

I need help to deal with this issue.
On my CSS is a query required to display
#media (max-width:1024px)
{
.header { display:none; }
}
--- COntent goes here ---
My problem is that the queries in this media only reflects when max-width screen is 910px
Also when #media (max-width:800px) changes expected to be made at max-screen width 800px; only appears when screen is 717px
I can't explain what the problem is, but i'd like to know if there is a way to make it responsive to assigned width size?
Remember everything is working perfect, just that it is not in concordance with required value.
Make sure you are adding everything you need in the query.
For example, when I make my media queries for responsive design I do this:
#media only screen and (min-width:1024px) {
/* CODE HERE */
}
That may fix your problem but please provide more information when asking questions.

Bug with browsers' interpretation of #media queries

There was a question related to this one a long time ago, but which was never responded to or resolved.
I am developing a web site that has distinct screen layouts, which depend upon the width of the viewport/window.
#media (min-width: 1120px) { }
/* behaviour as expected */
#media (min-width: 720px) and (max-width: 1119px) { }
/* inconsistent behaviour in Firefox and Edge, mixing elements of above and below */
#media (max-width: 719px) { }
In my browser of preference, Chrome, the layouts transition perfectly from the largest screen width to the smallest. The same is true of Opera. However, both Firefox and Edge demonstrate a strange behaviour whereby a single transitional width (of one pixel) causes the browser not to know how to interpret the content.
For example, as the window transitions to the narrowest media width, the title bar becomes narrower, no longer being required to accommodate the main menu, and the menu is hidden (display: none;) and replaced by a small menu icon at the side, which leads to a drop down on hover. But in Firefox and Edge, the main menu is not hidden, but instead crammed beside the logo until the window is collapsed one pixel further, after which the behaviour is as expected. These CSS changes are all under the same #media declaration!
Interestingly, the behaviour is not consistent. That is, the transition from the largest to the middle width is flawless, but the transition from the middle to the smallest is problematic.
Assuming that it was an issue with fractional size calculations, I added high precision to my #media query like this:
#media (max-width: 719.999px)
And again, the behaviour was inconsistent. If I adjusted the lower value up (i.e. 719px to 719.999px) there was no change. However, if I instead adjusted the higher value down (i.e. 720px to 719.001px) the problem was partially solved. Certain elements displayed expected behaviour, but again, other anomalies persisted. Similarly, the higher precision worked on Firefox but not Edge.
It seems probable, given the behaviour that I am seeing, that certain boundaries might cause problems, whilst others would not.
Is this just a known bug that web site developers have learnt to accept, or might there be an easy solution (other than simply selecting a different set of boundaries)?
It's difficult to give a specific answer without seeing what you are seeing, but it's possible that the browsers are handling the transition from one media query to the next in different ways discussed below.
Most Likely Cause: Windows Display Settings
I assume you are using Windows when you mention Edge, I suspect this might be because you have changed the scale of your display in Windows - Display Settings. If this is set to 125% for example, this can have an affect on all aspects of your display.
So really this isn't a bug with the media queries, so much as a discrepancy caused by the browsers not effectively handling the scaling by Windows Display settings.
UPDATE - Now that you have confirmed that you can stop on a particular point where this happens, then I'm pretty confident this is the cause. In my testing yesterday when looking into this, I was able to reproduce that behaviour when the display was scaled.
Using the following test case with original styling of an empty block with a red border, and different CSS applied at (max-width: 1119px) and (min-width: 1120px), the issue happens only when the display is scaled.
body{ margin:50px 0 0 0;}
.test {
border: 10px solid #f00;
height: 10px;
}
#media (min-width: 1120px) {
.test {
background: #ff0;
height: 500px
}
}
#media (min-width: 720px) and (max-width: 1119px) {
.test {
margin-left: 300px;
background: #0FF;
height: 200px
}
}
#media (min-width: 460px) and (max-width: 719px) {
.test {
margin-left: 300px;
background: #00f;
height: 200px
}
}
#media (max-width: 460px) {
.test {
background: #ff0;
height: 100px
}
}
<div class="test"></div>
Firefox, using Dev Console ruler to show window size:
Display Scale: 100% (i.e. no scaling) at 1119px width - CSS works as expected*
#media (min-width: 720px) and (max-width: 1119px) {
.test { margin-left: 300px; background: #0FF; height: 200px }
}
Display Scale 125% at 1119px width - no CSS media query applied, so CSS reverts to original
.test { border: 10px solid #f00; height: 10px; }
Possible Reason for the "glitch"
If the above is the case (or even if it isn't), I suspect your next question is why is it happening. Only the developers can answer that, but thinking logically we can come up with the reasoning below.
What are media queries meant to do?
First, we need to look at the purpose of media queries. According to the W3C CSS3 Media Queries recommendation:
The ‘width’ media feature describes the width of the targeted display area of the output device.
From this, we can assume they were intended for selecting the CSS to use depending on the media query that matches the screen/window size. Screen size can't have fractional pixels (e.g. 719.5px) and points between the whole pixels can only "exist" while the screen is being resized, and media queries are not intended to cover such temporary transitions.
OK, that's the why it isn't necessarily a "bug" and why it isn't always handled well in browsers, now...
What is causing this issue to happen during transitions?
If you have changed the scale, the browser has to recalculate the all the sizes to scale them up/down also. This "glitch" in the transition is most likely being caused by these calculations resulting in fractions of pixels. (If you haven't changed the scale, the same logic actually still applies)
It appears the browsers such as Chrome have been build to handle the recalculations/ size change in whole pixels, so the display will jump from the CSS applied by (e.g.) max-width:719px media query to the CSS for min-width:720px.
However other browsers such as Firefox don't seem to work like this and try to calculate the display based on the fractional pixels. This means there can be instances where the recalculated/changing size fall between e.g. max-width:719px nor min-width:720px.
In this case it seems to change the display to whatever CSS applies at that point - if there is another media query overlapping those sizes that would be applied, but more likely the original CSS is getting applied. So what you are seeing in that transition you describe is your CSS that exists outside of the media queries, e.g. if your CSS is written for mobile-first then you are seeing the CSS for mobile version of the site.
What can we do to "fix" it?
Aside from changing back the scale to 100% (which isn't a feasible option because you can't ask all your visitors to do that!) I don't know if there is a reliable solution.
One option is to use decimal values in your media queries like you mentioned, e.g.
#media (min-width: 720px) and (max-width: 1119.999px) { /* CSS....*/ }
#media (max-width: 719.999px) { /* CSS....*/ }
Another is to try is to make sure that you have suitable styling in your original CSS that will be applied at the "in-between" points, e.g. 719px - 720px.
Firefox and Edge appear to calculate and store their view port widths as floating point numbers, and media queries are applied to those values rather than the whole-pixel widths that they represent. There is consequently a difference in the way in which the parameters of our media queries are interpreted.
So whilst Chrome and Opera transition between 720 pixels and 719 pixels seamlessly, the same transition on Firefox or Edge can result in the browsers' simply skipping over the query and applying whatever default styling is otherwise relevant.
My original solution was to apply a browser-specific #media query to account for values between the whole pixels.
#-moz-document url-prefix() /* Firefox */
{
#media only screen and (min-width: 719.000001px) and (max-width: 719.999999px)
{
However, the real problem was the way in which I was specifying the parameters of the media queries. Unlike in programming languages, in which we can (and should) define mathematical parameters with equality and inequality (e.g. -1 ≤ x ≤ 1), CSS employs a hierarchy. For example, if #media (max-width: 600px) is followed by #media (max-width: 400px), whilst the two are not logically exclusive, the latter query will take precedence over the former.
So my solution was to change the way in which the #media queries were structured, such that there were max-width declarations only. If we try to define upper and lower limits of each interval, we run into the aforementioned problems with the way that the distinct browsers interpret the limits.
You could avoid any requirement to set up specific decimals by having no gaps between your queries at all.
For example:
(min-width 1px) // Instead of (max-width: 719px)
(min-width 720px) // Instead of (min-width: 720px) and (max-width: 1119px)
(min-width 1120px ) // Instead of (min-width: 1120px)
Currently, you have a middle band with one 'max-width' band and one 'min-width' band either side of it.
The above alternative solution just uses 1 direction (min-width), so there are no gaps to contend with.
Or:
That solution assumes you don't want to develop your whole front-end style for sub-720px and then media-query every larger size.
If you did want to do that, you could always just use the following: (sure Google search SEO may love you, but you may hate it!)
(min-width 720px) // Instead of (min-width: 720px) and (max-width: 1119px)
(min-width 1120px ) // Instead of (min-width: 1120px)
You would then need to incorporate your code that you wanted to put in your max-width: 719px media query into your base ("non- media query") code.
Which one?
I'd use the first solution.
Based on the OP, that one would be more applicable in that situation too.
Actually, I wouldn't use the first solution. I'd code for, say, >1230px, and then use media queries that use max-width for the responsive steps below that.

CSS Media Queries - Viewport Width being ignored by iPhone

I have this meta tag:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
I have this CSS:
#media (max-width: 1100px) and (max-device-width: 1100px) {
.wrapper {
width: 200px;
}
.page {
width: 100%;
}
I have also tried the following css rules:
#media (max-width: 1100px) and (max-device-width: 1100px) {
and
#media only screen and (max-width : 767px) { .wrapper { width: 100%; } }
and
#media only screen and (min-width: 1024px) and (max-width: 1280px)
and
#media only screen and (min-width: 993px) and (max-width: 1024px)
None of these CSS rules work.
Its all being totally ignored on an iPhone.
But it works fine in a browser resized to iphone size.
Have a look for yourselves:
http://www.moonshineandfuggles.com/
Ive extended the max-width to ridiculous values to try and include iPhones.
I dont want the .wrapper to be 200px, I want it to be 100% and for it to take into account the width of the device.
It seems like the iPhone thinks it should make this page about 700ish pixels wide.
How can I make the iPhone realize I have made this site responsive so it adjusts to the correct size?
The following has some information you might find useful of media queries -
What is the difference between "screen" and "only screen" in media queries?
My first recommendation is that you design the flow of your CSS to work from your smallest supported resolution outwards, such as designing your CSS file to support mobile devices and then using media queries to deal with changing the properties in your CSS file to support larger resolution windows / devices.
The following link explains the benefits of this more clearly than I am ever likely to - http://unmatchedstyle.com/news/working-with-media-queries-and-min-width.php.
Using this design approach makes it easier to deal with questions of specificity, which can occur when you try to use a media query to override an already defined property. One handy way of avoiding such conflicts I obtained from Media Query Styles Not Overriding Original Styles - note Adrift's usage of body.
You do not need to use media queries to override a property if you are not going to actually change it - e.g. you do not need to specify the width of wrapper in your media queries since it has already been defined in the main body of your CSS file.
As for your problems regarding width, try setting the width of body (or, failing that, wrapper) to 100vw if you are having trouble with 100%.
I hope that the above is not too confusing.
If you have any questions, then please feel free to reply.

How do I delete certain html on media queries?(on iphone it won't be there)

I know how to edit css on media queries, but how do I make it so that when the screen is less than 500px, certain html tags aren't even there? Thanks
You can't. They will always be part of the DOM (at least until you start adding JavaScript into the mix).
The closest you can come would be to write a style that sets them to display: none so they won't be rendered.
Below 500px screen size, you can hide the html tags you want.
#media screen and (max-width: 500px) {
p {
display:none;
}
}

Min-width Media Query displaying at the incorrect specified amount

This is a really strange problem, because I have not come across any media query problems in many months, but this one has stumped me.
I have a simple min-width definition to show the user name in the header if there is sufficient width:
.userFullName {
#media screen and (min-width: 800px) {
display: inline-block;
}
}
Unfortunately, according to Chrome's ruler, the media query is only getting applied at 1000px. At 999px and below, it disappears... so it seems like it's 200 pixels off exactly.
I'd post images to show what I mean, but I don't have enough rep.
I am not understanding why it would do this. Any ideas? Thanks!
I think you have to write like this
#media screen and (min-width: 800px) {
.userFullName {display: inline-block;
}
}
Otherwise please check is there any other media-query that over-writing this.