Media Query doesn't work correctly - css

I'm trying to set a rule using media queries:
#media screen and (min-width: 1920px){
#dados_contato_rodape p{
font-size: 5.5em;
border: 1px solid yellow;
}
}
But this format only applies when the screen width is 1930px or more (testing in addons responsive tools for Chrome).
My viewport tag is:
<meta name = "viewport" content = "width=device-width, maximum-scale = 1, minimum-scale=1" />
HTML:
<div id="dados_contato_rodape">
<p>contato#contato.com.br</p>
<p>55 51 9999 9999</p>
</div>
UPDATE:
If I put jQuery/Javascript code in this width screen (1920px), the result is 1920px:
$(window).outerWidth()
Media queries are working fine in other situations, but only in this case (until this moment) it's not working.
Can someone help me with it?
Thank you.

CSS screen widths and JS screen widths (both inner width and outer width) don't always match for all browsers.
This is the best explanation I know of: http://www.matanich.com/2013/01/07/viewport-size/
So in summary, trust your CSS #media setting rather over what's returned by JS in most situations.
Good luck!

Use a comparison of the jQuery method to the CSSOM and documentElement properties:
var window_diff = window.outerWidth - $(window).outerWidth();
var screen_diff = screen.availWidth - $(window).outerWidth();
var document_diff = document.documentElement.clientWidth - $(window).outerWidth();
It looks like jQuery is using the clientWidth property.
Use min-device-width as an alternative:
#media screen and (min-device-width: 1920px) {
...
}

Related

CSS media queries for print paper size

Paper isn't the same shape the world over. I have a document that I want to print differently when it's printed on A4 versus US Letter. Some elements should be hidden or shown. The obvious suggestion is to use a media query like so:
#media print and (max-height: 280mm) {
.a4-only {
display: none;
}
}
This doesn't appear to work, though, presumably because it's using the total document height or some irrelevant window height rather than the page height.
Is there a way of addressing page size accurately?
Browser support for print specific media queries is varied and there doesn't seem to be any good resources for it. It's really not possible to do this cross-browser, in some browsers the support is not there at all. Safari for example, seems to use the size of the browser rather than the page for it's media queries.
You can get it working in Chrome and Firefox. I knocked up a very rough demo using the size ratio to show what is possible with a bit of work. Currently tested and working on current versions of Chrome and Firefox on macOS. You should get a message at the start of the page with the printed page size (only when printed).
http://gsgd.co.uk/sandbox/print-test.html
The main trick is using vw units to check for height, hence using the aspect ratio you can target specific paper sizes:
#media print and (min-height:160vw) and (max-height: 170vw) { /* legal-size styling */ .standard.container::before { content: "LEGAL"; } }
#media print and (min-height:135vw) and (max-height: 145vw) { /* A4 styling */ .standard.container::before { content: "A4"; } }
#media print and (min-height:125vw) and (max-height: 135vw) { /* letter-size styling */ .standard.container::before { content: "LETTER"; } }
Unfortunately it seems like Chrome's page sizes for printing don't match the output page size so I guesstimated some styles that match for Chrome.
#media print and (min-height:120vw) and (max-height: 150vw) { /* legal-size styling */ .chrome.container::before { content: "LEGAL"; } }
#media print and (min-height:100vw) and (max-height: 120vw) { /* A4 styling */ .chrome.container::before { content: "A4"; } }
#media print and (min-height:80vw) and (max-height: 100vw) { /* letter-size styling */ .chrome.container::before { content: "LETTER"; } }
With an incredibly rudimentary browser detector script
if(navigator.userAgent.match(/chrome/i)) {
document.querySelector('.container').className = 'chrome container'
}
An idea to get something to work for Safari would be to manually resizing the window, but that would likely be a ton of work and require the user to select print size up front.
All that said you might get better mileage fixing up your layout to respond better to different widths.

Target IE11 on specific screen sizes using CSS Media queries [duplicate]

This question already has answers here:
Apply only to IE 11 within media query for screen size
(2 answers)
Closed 4 years ago.
I'm having some issues with IE11 similar to this post. I have a menu that expands at a certain size (1440px) and breaks in IE11. Using Media Queries I can target IE11 using (-ms-high-contrast: none), (-ms-high-contrast: active). However, I only want this at a certain resolution (> 1440px ).
I've tried various combinations of
#media screen and (min-width: 1440px) and (-ms-high-contrast: none) and (-ms-high-contrast: active)
But they don't seem to work
How to target and element in IE11 at screen size > 1440px without affecting the other browsers or using JS.
After searching through the depths of Stack Overflow I managed to find someone with the same problem.
Using:
#media only screen and (min-width: 1440px) {
_:-ms-fullscreen, :root .THECLASSNAME { width: 575px; }
}
Made it work.
Using CSS:
#media(min-width: 1440px){
.element{
-ms-high-contrast: none;
}
}
#media(max-width: 1440px){
.element{
-ms-high-contrast: active;
}
}
Remember that #media can also be used for print interfaces, so #media screen is simply specifying that these rules are to be implemented when displaying on a screen, but not when printing. #media by itself is more broad, and would apply to print.
Using jQuery:
if you want plain js you can translate it:
$( window ).width();
or
$( document ).width();
(use one or another depending on the behaviour you need)
you can programatically check screen sixze with:
$(document).ready(function(){
if($(window).width() > 1440){
$('.element').addClass('msClass');
}else{
if($('.element').hasClass('msClass'){
$('.element').addClass('msClass');
}
}
});
This only works when document loads, but you can add a dynamic function with:
//Window resize event listener, you'll need to put the other function first if you want it to being applied when you load the view.
$(window).resize(function(){
//if the actual window width is greater than 1440...
if($(window).width() > 1440){
//add a class to the desired element/s (this class must include the changes you want when more than 1440p)
$('.element').addClass('msClass');
//if the screen resizes below 1440p...
}else{
//if the class is set before
if($('.element').hasClass('msClass'){
//remove this class
$('.element').addClass('msClass');
}
}
});
You must create a class called msClass if you follow this example, where you put whatever you want to happen when >1440p.
Element is related to the element class (you can even use an id with $('#element') specifically set at the element/s you want to take this behaviour).
Hope it helps

What is the purpose of including "all" in #media rules?

So you see a lot of code examples do something like
#media all and (max-width:640px) {
div {
background-color:red;
}
}
Now afaik, the keywords "all" and "screen" and some others are for selecting the device type this applies to and the line is just supposed to provide a boolean output.
Since "all" applies to every device, one would imagine that its always 1 and (1 && x) always equals x so "all and" should make no difference whatsoever.
I tried out
#media (max-width:640px) {
div {
background-color:red;
}
}
and at least my browsers agree. Is there anything else I should know about?
See the spec: https://www.w3.org/TR/css3-mediaqueries/
The ‘print’ and ‘screen’ media types are defined in HTML4. The complete list of media types in HTML4 is: ‘aural’, ‘braille’, ‘handheld’, ‘print’, ‘projection’, ‘screen’, ‘tty’, ‘tv’. CSS2 defines the same list, deprecates ‘aural’ and adds ‘embossed’ and ‘speech’. Also, ‘all’ is used to indicate that the style sheet applies to all media types.
...
A shorthand syntax is offered for media queries that apply to all media types; the keyword ‘all’ can be left out (along with the trailing ‘and’). I.e. if the media type is not explicitly given it is ‘all’.
/* I.e. these are identical: */
#media all and (min-width:500px) { … }
#media (min-width:500px) { … }
/* As are these: */
#media (orientation: portrait) { … }
#media all and (orientation: portrait) { … }
In addition, the following media types: 'tty', 'tv', 'projection', 'handheld', 'braille', 'embossed', 'aural' have been deprecated in Media Queries Level 4.
all refers to: all media type devices, print: used for printing, screen: used for desktop screens, mobiles, tablets etc and speech: used for screen-readers that "reads" the page out loud.
In your case where you have specified media type as all, you can try printing the page by right clicking. The printed page will have all the styles applied in short it will exactly look the same.
Now take another example where you specify the media type as screen. If you try to print the page you will not see all the styles getting applied to the page as the styles were defined for screen alone.
If one does not specify all in media query it is by default taken as all.
#media screen {
div {
color: blue;
}
.print{
display: none;
}
}
#media print and (min-width: 200px){
div{
color: tomato;
}
div.not('.example'){
display:none !important;
}
.print{
display: block;
}
}
<div class="example">
<div>Try printing me. See if this blue color appears while printing</div>
<div class="print">I am only visible while printing.</div>
</div>

Sizing the viewport to the browser's dimensions

I would like to present a demo of a tablet application on a website (targeting desktop browsers).
I chose an iPad 2 for the demo whose resolution is 1024x768. Adding an iPad transparent graphic cover, the demo final size is 1210x1315px.
With such a resolution, most of the screens will be too small to display the demo properly.
I don't want to resize manually all the design, or to use CSS transform without knowing the relevant scale. Therefore, I'm looking for a way to resize automatically the design according to the available display resolution.
I tried to use the #-viewportproperty with no success...
Here is my non working code:
#media (min-height: 1400px) { /* if the screen's height is smaller than 1400px... */
#-viewport{
height:1400px; /* ... then, let's pretend it's 1400px high*/
}
}
I also tried this :
<meta name="viewport" content="height=1400, initial-scale=1" />
EDIT : jQuery workaround:
function resize(){
var documentHeight = $(document).innerHeight();
var targetedHeight = 1500;
if (documentHeight < targetedHeight){
var ratio = documentHeight / targetedHeight;
$('#container').css('transform','scale('+ratio+')');
$('#container').css('-webkit-transform','scale('+ratio+')');
$('#container').css('-moz-transform','scale('+ratio+')');
$('#container').css('-ms-transform','scale('+ratio+')');
$('#container').css('-o-transform','scale('+ratio+')');
}
}
This is what I finally did to achieve the expected result. I would have prefered a pure CSS solution...
I think you should be using media queries: Logic in Media Queries

Different screen orientations with the same css file

I've put the following meta tag in my mobile HTML
<meta name = "viewport" content = "initial-scale = 1.0">
After I coded the css file for mobile version, I realized it doesn't look good on lanscape mode since it has a different width size. I get an empty 160 pixel area on the right side.
Other than writing a separate css file for landscape mode, is there any way getting out of this?
You also need to bind the orientation change event. You can do it with this sample script:
<script>
$(function(){
function orient() {
if (window.orientation == 0 || window.orientation == 180) {
$('.featured').css('display','none');
orientation = 'portrait';
return false;
}
else if (window.orientation == 90 || window.orientation == -90) {
$('.featured').css('display','block');
orientation = 'landscape';
return false;
}
}
$(window).bind( 'orientationchange', function(e){
orient();
});
})();
</script>
If your css layout is based on screen percents instead of absolute values it should allow you to adjust to any screen layout without multiple css files just fine.
Look at the percent option: http://www.w3schools.com/cssref/pr_dim_width.asp
Or if you had a layout you wanted constant, you could center it.
center align the outise wrapper.
body{
max-width:786;/*target size of page*/
margin:0 auto auto auto;
}
is the easiest way.
You can use media queries to detect orientation changes and run different styles for each all in the same stylesheet.
Also for mobile it's a good idea to you use % rather than px for widths - what units do you use for css for mobile web apps?
/* Portrait */
#media screen and (orientation:portrait) {
/* Portrait styles */
}
/* Landscape */
#media screen and (orientation:landscape) {
/* Landscape styles */
}

Resources