I have a css file that I want him to be used only if the viewport is 1024 and higher.
how can I declare my css file to be loaded only and based on a viewport i'll decide about?
You can use the media attribute.
<link rel="stylesheet" media="screen and (min-device-width: 1024px)" href="style.css" />
Another solution:
<style>
#media only screen and (min-device-width: 1024px)
{
Whatever you want for css...
}
</style>
Related
This won t work. how can i accomplish something similar?
#media screen and (max-width:480px) {
#import url("style480.css");
}
Try like this
<link rel="stylesheet" media="screen and (max-width: 480px)" href="style480.css" />
In a website, I use a media query for small devices, effective for screen resolutions <=980px.
Problems is: on the iPad, in horizontal view (1024px), the css file is applied.
Why is that?
On the desktop (Firefox), I don't have this problem. I tried changing to max-device-width, no difference.
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="medium.css" media="(max-width:1150px)">
<link rel="stylesheet" href="mobile.css" media="(max-width:980px)">
Thanks for the answers so far.
To be clear: I am not looking for a way to target the iPad. I am looking for the reason behind the iPad's behaviour. It's screen has a width of 1024px, but it applies a stylesheet it should not. Why?
Edit:
I found the problem/solution. See below.
try to create the media query within a seperate CSS stylesheet, which will automatically detect what size the viewport is.
This site is a really good one:
http://css-tricks.com/snippets/css/media-queries-for-standard-devices/
Use this media query to to target all iPad versions (iPad 1-5 & Mini).
<link rel="stylesheet" media="all and (device-width: 768px) and (device-height: 1024px) and (orientation:portrait)" href="ipad-portrait.css" />
<link rel="stylesheet" media="all and (device-width: 768px) and (device-height: 1024px) and (orientation:landscape)" href="ipad-landscape.css" />
Additionally, check out the solution posted for this problem.
you don't have to create additional CSS file for this just use this and add your code
#media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait) {
----CODE HERE----
}
#media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
----CODE HERE----
}
The iPad browser works with the following information:
width and device-width: 768 px
height and device-height: 1024 px
The orientation of the device does not matter in regard to which value is height and which one is the width!
That means in landscape mode, the browser promotes width = 768 px
In my opinion, this is a bug. The 'width' property should contain the width of the browser window.
Now I use the following media query on the website:
<link rel="stylesheet" href="mobile.css" media="(max-device-width:768px) and (orientation:portrait), (min-device-width:769px) and (max-width:980px)">
That works very well.
This question already has answers here:
Why are my CSS3 media queries not working on mobile devices?
(23 answers)
Closed 9 years ago.
I keep trying to do a media query in my CSS doc doing something like:
#media screen and (max-device-width: 480px) { /*css here*/}
but it won't work when I test it on an iPhone. I've tried changing the sizes and using the portrait/landscape feature, but still nothing. What am I doing wrong?
Check that you have
<meta name="viewport" content="width=device-width, initial-scale=1.0">
in the head of your doc
I always call mine when I link the CSS in the head of the HTML.
An example from a current page I'm working on:
<link rel="stylesheet" type="text/css" media="screen and (min-device-width: 320px) and (max-device-width: 500px)" href="css/mobile.css" />
<link rel="stylesheet" type="text/css" media="screen and (min-device-width: 501px)" href="css/main.css" />
This selects the CSS doc that will be loaded right from the start instead of selecting styles after the CSS document is loaded (reduces number of HTTP requests)
When doing this within the CSS document itself, you should generally replace max-device-width with max-width.
this is a samples media query css
/************************************************************************************
smaller than 980
*************************************************************************************/
#media screen and (max-width: 980px) {
your css here
}
/************************************************************************************
smaller than 650
*************************************************************************************/
#media screen and (max-width: 650px) {
your css here
}
/************************************************************************************
smaller than 560
*************************************************************************************/
#media screen and (max-width: 480px) {
your css here
}
Hard to understand as you have not provided the code..but the common mistake people do it by not adding this meta data
<meta name="viewport" content="width=device-width, initial-scale=1">
Use "max-width" instead of "max-device-width" which is fixed per device.
I'm not sure but I think the max-device-width of an iphone is 640px. Give it a try.
#media only screen and (min-width : 480px) {
}
It seems to work fine in both Android and iPhone.
Just for tests and learning css3 i am trying to create small mobile website. But for now I have small problem with targeting stylesheets. I'm doing it in such way:
<link rel="stylesheet" media="only screen and (max-width: 180px)" href="xsmall.css" />
<link rel="stylesheet" media="only screen and (min-width: 240px)" href="small.css" />
<link rel="stylesheet" media="only screen and (min-width: 320px)" href="medium.css" />
<link rel="stylesheet" media="only screen and (min-width: 480px)" href="large.css" />
<link rel="stylesheet" media="only screen and (min-width: 540px)" href="wide.css" />
Unfortunatelly after changing xsmall.css change is visible also in other web versions ( so for 480px, 540 px etc ). I test websites (mobile one) on Opera Mobile Emulator. What I'm doing wrong?
thanks
What you are doing wrong is to think that your stylesheet selection includes only one of the style sheets.
A style sheet that you include with min-width will be included for any resolution that is larger, so if I for example have a 600px wide screen, I will get small.css, medium.css, large.css and wide.css, not only wide.css.
(Also, if I have a 200px wide screen, it would not include any style sheet at all...)
You would need to use both min-width and max-width to make it only include one of the style sheets.
I checked my blog on tablets and netbooks and it looked a bit bad, so I made an extra css and added to header.php in between :
<head>
<link media="all and (min-width: 481px) and (max-width: 768px)" type="text/css" rel="stylesheet" href="tablet.css">
</head>
But nothing happens. How to add an extra .css to make this work ?
Also tried to add this function to themes functions.php, but blog crashes.
wp_enqueue_style('my-custom-style', 'get_bloginfo('template_url') . '/css/tablet.css',false,'1.1','all and (min-width: 481px) and (max-width: 768px)');
What should I add in 'template_url' and what is the simpliest way of achieving my goal?
Thanks!
Try this :
<link rel="stylesheet" type="text/css" href="path/to/your/css/file.css">
Add it to you template header.php file, after <?php wp_head(); ?> and after your last stylesheet.
If this fails add it just before the </head> tag.
Also make sur your the path to your stylesheet is correct, if your not sure use the full path:
<link rel="stylesheet" type="text/css" href="http://www.site.com/path/file.css">
Hope it helps
You just add the code below into the bottom of your stylesheet and apply the styles that you want for those specifications in there. That's for an exact size though.
#media all and (max-width: 768px) and (min-width: 481px) {
Your styles go in here
}
The code below this will target a max-width of 768px or a min-width of 481px.
#media all and (max-width: 768px), (min-width: 481px) {
Your styles go in here
}