I have a website that has a fixed width of 1024px and is centered on desktop. On mobile devices, the width should match the size of the phone or tablet.
When using device-width, like this
<meta name="viewport" content="width=device-width, initial-scale=1" />
I get following result:
I get the same result using width=1024.
... Which is really confusing, since the user is unable to gather an overview. I don't know why device-width sets the website's viewport to this particular size. I would expect otherwise.
What I'm expecting and what I really want is something this:
Question: How do I tell the device to fit the website on the screen exactly?
In order to make a website adaptive and responsive I recommand using
#media screen and (max-width: the max width you want){ }
You can also add this in your . This will make the initial scale 1 and allow for some zooming into 3.
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3" />
You also need to use % instead of pixels on wrap elements. This way it adjust according to your screen.
Here is an example:
For devices with 0-500px width, run the second CSS code.
For devices with 500-1600px width, run the first CSS code.
You can also specify for retina using this:
#media
only screen and (-webkit-min-device-pixel-ratio: 2),
only screen and (-moz-min-device-pixel-ratio: 2),
only screen and (-o-min-device-pixel-ratio: 2/1),
only screen and (min-device-pixel-ratio: 2),
only screen and (min-resolution: 192dpi),
only screen and (min-resolution: 2dppx){ }
This is the other example:
/** From 500 - 1600px width **/
#media screen and (max-width: 1600px){
body {
background-color: #fff;
}
}
/** From 0 - 500px width **/
#media screen and (max-width: 500px){
body {
background-color: #000;
}
}
I asked a similar question earlier. You can check it out here.
What you got when remove scale factor from meta tag? And another question: do you want users to zoom (scale) page or not? I shall try better:
<meta name="viewport" content="width=device-width" />
and also:
<meta name="viewport" content="width=device-width, maximum-scale=1.0" />
For fitting height and width you can play with those meta:
<meta name="viewport" content="height=device-height, width=device-width, minimum-scale=1.0, user-scalable=yes|no" />
Related
I have a iPhone 11 and try to reach my site https://tryggarehem.se/, I have specified meta tags relevant for using media queries. However, it seems not to work at all. I am totally lost here, this is my code:
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
...
<link rel="stylesheet" href="assets/css/main.css" />
...
</head>
CSS file:
#media only screen and (max-width: 950px) and (min-width: 751px) {
.main-content {
margin-top: -200px !important;
}
}
#media only screen and (max-width: 750px) {
.main-content {
margin-top: 0px !important;
width: 100% !important;
}
}
iPhones are non-standard in their handling of CSS pixels vs device pixels. It might be possible your screen width is different than what you are assuming. You can fetch current screen width and check whether it lies within your applied range.
var w = window.innerWidth;
var h = window.innerHeight;
var x = document.getElementById("data");
x.innerHTML = "Browser width: " + w + ", height: " + h + ".";
<html>
<body>
<p id="data"></p>
</body>
</html>
You can also try to use standard media queries for iPhones mentioned in this answer
I was having the same issue.
What worked for me
Instead of
#media only screen and (min-width: 600px)
I used
#media screen and (min-device-width: 600px)
Reason is
All you are essentially interested in is the width of the viewport no matter the device. However the main difference between width and device-width is that device-widths don't always match the layout viewport of said device. Many tablets and mobile devices don't always have 1 device pixel per CSS pixel.
I am trying to make a responsive website for desktops, tablets and mobile devices but I am facing some trouble. I created a document called responsive-Ipad.css that would work only in Ipads (I set the min-width to 600px) but somehow the css files are affecting the screen content in any device (tablet, mobile or desktop). I have 2 other css files.
style.css that I use to set the desktop layout,
responsive.css to set the mobile layout and responsive-Ipad.css for Ipads.
the style.css doesn't have media query
the responsive.css starts with this line code:
#media only screen and (max-width:420px){
and the responsive-Ipad starts with this line code:
#media only screen and (min-width: 600px){
Does anyone know how to fix this?
Image that represents the problem
Try using these media queries and add this meta tag
<meta name="viewport" content="width=device-width" />
#media only screen and (device-width: 768px) {
/* For general iPad layouts */
}
#media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait) {
/* For portrait layouts only */
}
#media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
/* For landscape layouts only */
}
Check whether you have included your CSS (File path should be proper) file properly in your HTML page. And for responsive web page you need the below meta tag to be included in your HTML page. Just before including your CSS file.
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0">
This will be better if you include these lines in your HTML page.
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0">
/*
CSS for your media query
*/
#media (max-width:420px){
/*your css for devices that are below 420px*/
}
#media (min-width:600px){
/*your css for devices that are above 600px*/
}
<html>
<head>
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/responsive.css">
<link rel="stylesheet" href="css/responsive-ipad.css">
</head>
<body>
<!--HTML Content-->
</body>
</html>
Make sure you include all your css files into your HTML file.
Hope this helps.
I use Bootstrap and I set:
#media (max-width: 991px) {
body {
min-width:500px;
}
}
And when I login with my phone I have viewport less then window width, like that:
I need to set viewport same as window width. In bootstrap I have header:
<meta name="viewport" content="width=device-width, initial-scale=1">
And bootstrap says that I can't delete that. How can I edit my code to have viewport on mobile same as window width?
I am not sure if your issue is related to the view port. But in case if it is, you can try the following view port code.
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, target-densitydpi=medium-dpi, user-scalable=0" />
Its just because you have set min-width property to body element.
Try to use .container class from bootstrap it will solve your problem.
If you just wanted to use body element then go for this one...
#media (max-width: 991px) {
body {
width:100%;
}
If your content should be at least 500px wide, set min-width on your body.
body {
min-width: 500px;
}
If you want it to start zoomed out (as if viewport is 500px wide), use shrink-to-fit:
<meta name="viewport" content="width=device-width, shrink-to-fit=yes">
I am less confused to make a website only for mobile and tablet not a desktop. I need to know only one thing.
Should I use <meta name="viewport" content="width=device-width, initial-scale=1"> and define #media only screen for specific device?
Yes, however your website still will be accesible from other devices.I can explain it for you:
Using tag:
<meta name="viewport" content="width=device-width, initial-scale=1">
It will adjust webpage width to match device width and set initial scale to 1 (no zooming). So for example, if you open this page using iPad, web browse will display page in 1024x768 (landscape) or 768x1024 (portrait).
initial-scale=1
This will force to display webpage with zoom set to 1.
Using media queries CSS3 you can set different CSS styles for different devices:
Styles for Tablets
#media screen and (min-width: 768px) and (max-width: 1024px) {
/* Styles for tablets */
}
Styles for Desktop
#media screen and (min-width: 1025px) and (max-width: 1280px) {
/* Styles for Desktops */
}
I am trying to make a responsive layout that has a phone view and a normal view only (ie. Tablets should load the page normally).
Everything is working correctly as far as the CSS goes however the viewport is screwing up the zoom on tablets when they're in portrait mode. (They're zoomed in to their pixel width)
Is there a way to get tablets to zoom out to the full page while still having the phones load scaled to their size?
Here's what I have... Changing the initial scale to .75 almost works but then the iPhone loads the normal site when in landscape, which I don't want.
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" type="text/css" media="only screen and (max-width: 568px)" href="css/mobile-styles.css" />
Don't mess with the viewport, it'll only cause headaches and accessibility issues. Instead, create the CSS you want for phones(small screens) and then include the styles for the "full" site (large screens) in a min-width:500px(or some other breakpoint) media query.
Example CSS:
/*this will fire for any size screen*/
nav{
width:100%;
}
/*this changes the above styles for any screen over 500px*/
#media (min-width:500px){
nav{
width:50%;
float:left;
}
}