I am using responsive design for my site but the css media queries are not working for Windows Phone. I found a solution here http://timkadlec.com/2013/01/windows-phone-8-and-device-width/ but still is not working for me.
I am using on my header file on html this:
<meta name="viewport" content="width=device-width, initial-scale=1">
then I add on the head this js:
<script type="text/javascript">
(function() {
if ("-ms-user-select" in document.documentElement.style && navigator.userAgent.match(/IEMobile\/10\.0/)) {
var msViewportStyle = document.createElement("style");
msViewportStyle.appendChild(
document.createTextNode("#-ms-viewport{width:auto!important}")
);
document.getElementsByTagName("head")[0].appendChild(msViewportStyle);
}
})();
</script>
before any other script and last on my css page I have:
#-webkit-viewport{width:device-width}
#-moz-viewport{width:device-width}
#-ms-viewport{width:device-width}
#-o-viewport{width:device-width}
#viewport{width:device-width}
#media only screen and (max-width: 599px) {
#-ms-viewport{
width:320px;
}
}
But still I cannot make it work. Does anyone have an idea what could be the problem?
Thanks in advance.
Try this media queries
/* Smartphones (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
}
/* Smartphones (landscape) ----------- */
#media only screen
and (min-width : 321px) {
/* Styles */
}
/* Smartphones (portrait) ----------- */
#media only screen
and (max-width : 320px) {
/* Styles */
}
Related
So I'm creating a responsive design website depending on the users device, however when viewing the site on an iPhone 5C the css link for the smartphone version css/smartphone/header.css doesn't work, and neither does the tablet css/tablet/header.css file. It just shows the desktop version on the mobile phone (which I don't want). But when I view the site on the Google Developer 'Dimensions' extension, which allows you to mimic a mobile device (in this case the iPhone 5), the css links work as intended.
Can someone tell me what is wrong with my css links which causes them to not work on the iPhone 5? Thank you
<link type='text/css' rel='stylesheet' media='screen and (min-width: 1024px)' href='css/desktop/header.css' />
<link type='text/css' rel='stylesheet' media='screen and (min-width: 200px) and (max-width: 767px)' href='css/smartphone/header.css' />
<link type='text/css' rel='stylesheet' media='screen and (min-width: 768px) and (max-width: 1023px)' href='css/tablet/header.css' />
Try: <meta name="viewport" content="width=device-width, initial-scale=1.0" />. For more info, see this article on MDN:
https://developer.mozilla.org/en/docs/Mozilla/Mobile/Viewport_meta_tag.
Try to use some solid device specific media queries when building responsive websites.
In your case the media queries are not the right ones for iphone.
For instance you could use these ones as a starting point and then if needed you can search for other resources online.
/*
* From css-tricks.com
* http://css-tricks.com/snippets/css/media-queries-for-standard-devices/
*/
/* Smartphones (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
}
/* Smartphones (landscape) ----------- */
#media only screen
and (min-width : 321px) {
/* Styles */
}
/* Smartphones (portrait) ----------- */
#media only screen
and (max-width : 320px) {
/* Styles */
}
/* iPads (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Styles */
}
/* iPads (landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
/* Styles */
}
/* iPads (portrait) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
/* Styles */
}
/* Desktops and laptops ----------- */
#media only screen
and (min-width : 1224px) {
/* Styles */
}
/* Large screens ----------- */
#media only screen
and (min-width : 1824px) {
/* Styles */
}
/* iPhone 4 ----------- */
#media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}
Here you can find a more extensive list with specific devices: http://nmsdvid.com/snippets/
I am working on a responsive design and I would like to re-order some divs at certain breakpoints. I am not sure if this is even possible, but here it goes.
Here is a rough fiddle showing the current layout: http://jsfiddle.net/Ubjmc/ above a certain breakpoint. As you can see the order (of the divs) go - one, two and then three.
And this is how I want it to look after my breakpoint: http://jsfiddle.net/AfT4D/ The order of the divs for this one is - one, three and then two.
I am not sure this is possible to do with straight css. What I have been doing so far is making a 4th element that hides above the breakpoint and shows after the breakpoint and then hiding the element it replaces at that breakpoint (hopefully that makes sense).
Is that method the only reliable css-only way of doing what I'd like (that isn't extremely convoluted)?
Edit - I already have my breakpoint set and am using media queries. The second fiddle in my example is how I want the first to look, in the order of the first (or I want to find out if that is possible).
<div class="wrap">
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
</div>
The question is if I have my order like shown in the code above, if there any possible way to get it to look like this: http://jsfiddle.net/AfT4D/, without editing the order of the divs?
You have already a property in CSS called order, and it is available in flex display.
Set the container to
.wrap {
display: flex;
flex-wrap: wrap;
}
Then you can set the order 3 to two
.two {
order: 3;
}
And that is it !
demo
Support isn't very high, but it is coming (Firefox should release flex-wrap support this month)
You would have to use media queries, I think it would be better to do it in the HTML instead of css such as
<link href="smartPhone.css" type="text/css" rel="stylesheet" media="screen and(min device-width : 320px) and (max-device-width : 480px)>
<link href="tablets.css" type="text/css" rel="stylesheet" media="screen and(min device-width : 481px) and (max-device-width : 1024px)>
<link href="computer.css" type="text/css" rel="stylesheet" media="screen and(min device-width : 1025px)>
You can find out more here http://www.htmlgoodies.com/html5/tutorials/an-introduction-to-css3-media-queries.html#fbid=T7Km4lhd7oK
JSFiddle Example: http://jsfiddle.net/Ubjmc/1/
You have to put .three below .one
<div class="wrap">
<div class="one"></div>
<div class="three"></div>
<div class="two"></div>
</div>
And edit your CSS ( ??? = Screen Width ):
#media only screen and (max-width: ???px) {
.one,.three{width:50% !important;}
.two{width:100% !important;}
.two{clear:both;}
}
.three {
float: right;
/* ... */
}
You'll want to investigate media queries. This way you can customize the layout at different widths.
Here's a starter template:
/* Smartphones (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
}
/* Smartphones (landscape) ----------- */
#media only screen
and (min-width : 321px) {
/* Styles */
}
/* Smartphones (portrait) ----------- */
#media only screen
and (max-width : 320px) {
/* Styles */
}
/* iPads (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Styles */
}
/* iPads (landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
/* Styles */
}
/* iPads (portrait) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
/* Styles */
}
/* Desktops and laptops ----------- */
#media only screen
and (min-width : 1224px) {
/* Styles */
}
/* Large screens ----------- */
#media only screen
and (min-width : 1824px) {
/* Styles */
}
/* iPhone 4 ----------- */
#media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}
If you're having trouble getting the desired results, you can also try jQuery's insertAfter and insertBefore and build the columns on the fly based on screen size.
I just did a speed test on desktop & mobile on gtmetrix and have the error code: Serve Scaled Images - But only on the mobile speed test. The error reads as follows:
**.jpg is resized in HTML or CSS from 1200x431 to 318x114. Serving a scaled image could save 520.9KiB (92% reduction).
Is there a specific code I can put with the image to have it one size when on mobile and leave the desktop one at the original/other size. Or is there another way such as serve a particular image for mobile (same image smaller size) then another image for serving desktops?
Thanks.
You can do like this and define different background url on different media queries for same class.
/* Smartphones (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
}
/* Smartphones (landscape) ----------- */
#media only screen
and (min-width : 321px) {
/* Styles */
}
/* Smartphones (portrait) ----------- */
#media only screen
and (max-width : 320px) {
/* Styles */
}
/* iPads (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Styles */
}
/* iPads (landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
/* Styles */
}
I think you might want something like this:
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
if(isMobile.any()){
// Mobile!
} else {
// Not mobile
}
You can then do something like: (untested below(needs tweaking))
if(isMobile.any()){
img { height:140%;width:140%
You can alter the size of images through the style tag. img { should alter all <img srctags on the page as I've done it before. And a revision of the above code should alter it only for mobile or desktop computers.
I have different views on portrait and landscape
/* portrait ----------- */
#media only screen
and (max-width : 320px) {
body{
padding:20px;
}
}
/* landscape----------- */
#media only screen
and (min-width : 321px) {
body
{
padding:60px;
}
}
/* webpage----------- */
body
{
padding:0px;
}
however, landscape css effects on webpage view. how do I spilt webpage up?
I tried to make another media query on webpage, but it didnt work.
also I tried (min-device-width : 321px) for devices only, but it doesnt work
As explained in this article, the media query spec includes orientation detection. It should look something like this:
#media screen and (orientation:landscape) and (min-width:321px) {
foo {
padding:60px;
}
}
#media screen and (orientation:portrait) and (max-width:320px) {
foo {
padding:20px;
}
}
And so on.
I need to write different styles in following cases
Device width greater than device height
/* Landscape */
#media screen and (orientation:landscape) {
.bg img {
height: auto;
width: 100%;
}
}
Device height greater than device width
/* Portrait */
#media screen and (orientation:portrait) {
.bg img {
height: 100%;
width: auto;
}
}
Orientation doesn't work perfectly in some stages on resizing the browser.
How to write correct CSS?
Is it possible to do so with CSS?
EDIT :
I am attaching image how it looks on resizing the browser
You have access to the browser's aspect ratio with these media query features: aspect-ratio | min-aspect-ratio | max-aspect-ratio. For more info, check out CSS media queries on MDN.
Portrait has an aspect ratio greater than 1:1 and landscape is less. To verify, I made a JSFiddle that changes color when you switch from "landscape" to "portrait".
Try this:
/* Landscape (i.e. wide viewport) */
#media screen and (min-aspect-ratio: 1/1) {
.bg img {
height: auto;
width: 100%;
}
}
/* Portrait (i.e. narrow viewport) */
#media screen and (max-aspect-ratio: 1/1) {
.bg img {
height: 100%;
width: auto;
}
}
Update: The image is part of the flow of the document, and won't fill the viewport unless the body also fills the viewport with body {height: 100%;}, as in this JSFiddle.
Try img {position: absolute;} to pull the image out of the flow, so it's dimensions aren't constrained by the body's size. See JSFiddle.
The problem that you were having was that you were relying on the text "orientation:landscape" which is not recognised by browsers. Use the code below which check the height and width of a device to calculate its orientation. Credit to css-tricks.com who can really help with media queries, here is an example of the most common uses of media queries.
/* Smartphones (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
}
/* Smartphones (landscape) ----------- */
#media only screen
and (min-width : 321px) {
/* Styles */
}
/* Smartphones (portrait) ----------- */
#media only screen
and (max-width : 320px) {
/* Styles */
}
/* iPads (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Styles */
}
/* iPads (landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
/* Styles */
}
/* iPads (portrait) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
/* Styles */
}
/* Desktops and laptops ----------- */
#media only screen
and (min-width : 1224px) {
/* Styles */
}
/* Large screens ----------- */
#media only screen
and (min-width : 1824px) {
/* Styles */
}
/* iPhone 4 ----------- */
#media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}
Source http://css-tricks.com/snippets/css/media-queries-for-standard-devices/
I have a similar need but I was using:
<link rel='stylesheet' media='screen and (max-aspect-ratio: 4/3)' href='css/tall.css' />
<link rel='stylesheet' media='screen and (min-aspect-ratio: 4/3)' href='css/wide.css' />
The only problem was, when I hit 768 x 1024 it displayed correctly, but when I went to 1024 x 768, I got a blank page. I was using a simple css display assignment like:
display:none;
to turn the div on or off, which works but my question is how can you make a continuos flow without that break? at 1024 x 768
I am using this right now:
<link rel='stylesheet' media='screen and (orientation:portrait)' href='css/tall.css' />
<link rel='stylesheet' media='screen and (orientation:landscape)' href='css/wide.css' />
I would like to use max-aspect-ratio and what not, because that gives me more control over when change happens. I mean I can't put 1.333 ratio and 1.334 bummer...
--NEW UPDATE
<!-- tall -->
<link rel='stylesheet' media='screen and (max-aspect-ratio:4/3) and (min-width:0px) and (max-width:1023px)' href='css/tall.css'/>
<!-- tall -->
<link rel='stylesheet' media='screen and (max-aspect-ratio:4/3) and (min-width:1025px) and (max-width:9999px)' href='css/tall.css'/>
<!-- wide -->
<link rel='stylesheet' media='screen and (min-aspect-ratio:4/3)' href='css/wide.css'/>
I guess I fixed my problem by doing the above code, which is disappointing. But it works so far, I am just going to have to test for almost every screen to make sure higher resolutions that are EXACTLY 4:3 by the Query "terms" still show. I tried 2048 x 1536 iPad3 Retina and that shows up, don't know why 1024 x 768 fails... but is working with the fix above.
----UPDATE 2 (I hate to be a pain but)
This seems to be the cleanest solution for aspect-ratio:4/3 :
<!-- tall 1.33301 -->
<link rel='stylesheet' media='screen and (max-aspect-ratio:4095/3072)' href='css/tall.css'/>
<!-- wide 1.33333 -->
<link rel='stylesheet' media='screen and (min-aspect-ratio:4096/3072)' href='css/wide.css'/>