I want to make website non-responsive, but using col-md, col-lg and col-sm, it have to be non-responsive and ignore #viewport ( browser resize ) width , but have to be responsive on mobile and tablets.
I need to use col-md , sm, lg to make it responsive on mobile and tablets, but my website collapses depending on Browser #viewport because of I can't use col-xs for all columns.
Is it a possible thing to sort out ?
Thanks
Do something like this,
<script type="text/javascript">
$( document ).ready(function() {
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
$('head').append( $('<link rel="stylesheet" type="text/css" />').attr('href', 'your stylesheet url') );
}
});
</script>
You'll have to load jQuery. In head tag.
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
Try this:-
#media screen and (min-width:768px){ /* or whatever width you prefer for min-width of desktop view */
.container{
min-width:970px; /* or whatever width you prefer for desktop view */
width:970px; /* or whatever width you prefer for desktop view */
}
}
What it should do is, it will not resize the container until the screen is 768px wide. You can change this media screen min-width as per your requirement. Once the device or browser's with reduces beyond 768px, it will start the responsiveness automatically.
Above CSS is just an example. You don't need any JavaScript to deal in your situation.
Hope it helps.
I would load or not load responsive styles based on whether or not it's a touch device. The styles for mobile should be just the site with responsive css. Then a duplicate of that is used for non-touch devices but bring your columns out of the min-widths and set a width on your container. There's other ways of doing this, like loading another css file if it's touch and just keeping the desktop styles for all device, but the css for responsive is loaded after the desktop so that it is the last in the order.
MINI DEMO: http://jsbin.com/wuqita/1/edit
/* __________________ SUPPORTS TOUCH OR NOT __________________*/
/*! Detects touch support and adds appropriate classes to html and returns a JS object | Copyright (c) 2013 Izilla Partners Pty Ltd | http://www.izilla.com.au / Licensed under the MIT license | https://coderwall.com/p/egbgdw */
var supports = (function() {
var d = document.documentElement,
c = "ontouchstart" in window || navigator.msMaxTouchPoints;
if (c) {
d.className += " touch";
return {
touch: true
};
} else {
d.className += " no-touch";
return {
touch: false
};
}
})();
$(document).ready(function () {
if ($('html').hasClass('touch')) {
$('#desktopcss').prop('disabled',true);
}
if ($('html').hasClass('no-touch')) {
$('#responsivecss').prop('disabled',true);
}
});
CSS files linking and ids:
<link id="responsivecss" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link id="desktopcss" href="//bombdiggitydesign.com/jsbin/test.css" rel="stylesheet" type="text/css" />
Related
I can't for the life of me find out why min-width isn't working on mobile on my website. There is probably a really basic solution to this but I can't find anything.
Basically, I have an element with this CSS:
fullscreen {
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
position: absolute;
min-width: 100%;
min-height: 100%;
}
This works perfectly on desktop browsers, at all resolutions and aspect ratios, but it completely breaks down on mobile, although when I change min-width to width, it fills the screen perfectly on desktop and mobile.
And when I use height instead of min-height, it works as expected and I can position elements with bottom on mobile, but when using min-height, it uses some arbitrary position about midway up the screen. max-width and max-height seem to work correctly as well
What's even weirder to me is that using the mobile device "emulator" on Chrome, the results seem to be totally random, sometimes working, sometimes having that midway line at the same place as my actual phone, sometimes having it in a completely different place.
My phone consistently has the midway line at the same place.
I've seen a lot of people recommend the use of the meta viewport tag, and I'm pretty positive I have it implemented correctly:
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
The ideal solution for me would be to simply replicate the same behaviour as I have on desktop on mobile, since my desktop site already works for all resolutions.
Thanks alot for your help, I've spent at least 3 hours trying to figure this one out!
EDIT: I've tried implementing the behaviour I want in JavaScript, and for some reason, it still doesn't want to work on mobile. Here is a test I made:
<!DOCTYPE html>
<html>
<head>
<title>Mobile fit test</title>
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0">
<style>
body {
margin: 0;
padding: 0;
overflow-x: hidden;
overflow-y: hidden;
}
</style>
</head>
<body>
<img id = "fit" src = 'http://placehold.it/1000'>
<script>
var image = document.getElementById("fit");
var image_width, image_height;
window.onresize = function() {
console.log(image_width, image_height);
var width = 1.0 / window.innerWidth * window.innerHeight / image_height * image_width;
var height = 1.0;
if (width < 1.0) {
width = 1.0;
height = 1.0 / window.innerHeight * window.innerWidth / image_width * image_height;
}
width *= window.innerWidth;
height *= window.innerHeight;
image.style.width = width + "px";
image.style.height = height + "px";
image.style.position = "absolute";
image.style.left = window.innerWidth / 2 - width / 2 + "px";
image.style.top = window.innerHeight / 2 - height / 2 + "px";
}
document.body.onload = function() {
image_width = image.width;
image_height = image.height;
window.onresize();
}
</script>
</body>
</html>
With this, I can still zoom, window.innerWidth and window.innerHeight do not appear to be what they're supposed to be, and it still works perfectly with any resolution and the plugin that Mileta Dulovic suggested. This is driving me nuts!
Your meta tag is looking as it should be. If you don't want the user to zoom page you can use this
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
Okay so. Media query works like this
#media screen and (min-width: 768px) {
...
}
CSS will be applied only and only on screens that have width >= 768px. On the screens below, it won't be applied.
#media screen and (max-width: 768px) {
...
}
CSS will be applied only and only on screens that have width <= 768px. On the screens above it won't be applied.
If I understand your question good you have a problem when you use min-width with the media query. That is because you never tell it what to do on smaller screens.
Also, don't put too much faith in Chrome's mobile view. It is not good in most cases.. Rather install plugin for Chrome that helps you with that.
I currently have the following viewport for my webapp:
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, target-densityDpi=device-dpi" />
The problem with using height=device-height is that it is taking in account the entire screens height. I need device-height minus the browser GUI. As it stands, the page is about 40-50px too long. I don't want to hardcode the css to be subtracted by that amount since the GUI could be changed depending on the browser, accessibility settings, etc... Anybody have a solution?
Correct, the viewport is your complete device, including GUI.
The most simple solution; just don't set the height! Use the browsers height: 100% on html, body to get the desired absolute height.
Used a combination of answers I found in threads here
if (navigator.userAgent.match(/iPad;.*CPU.*OS 7_\d/i) && window.innerHeight != document.documentElement.clientHeight) {
var fixViewportHeight = function () {
document.documentElement.style.height = inner + "px";
if ($("input,textarea,select").is(":focus")) {
$('body').css('position', 'static');
}
else {
$('body').css('position', 'fixed');
}
};
window.addEventListener("scroll", fixViewportHeight, false);
window.addEventListener("orientationchange", fixViewportHeight, false);
fixViewportHeight();
document.body.style.webkitTransform = "translate3d(0,0,0)";
}
after reading a lot about browser viewport width issues, I concluded to make a trial to see that if I understood the concept.
I used javascript below: This script prints "Your viewport width is WidthxHeight"
Element 1: At a 1920 x 1080 resolution, HP x2301 screen without any scroll bar:
JS printed: Your viewport width is 1920x955
Element 2: At a 1920 x 1080 resolution, HP x2301 screen with scroll bar (I increased the height of page with lots of lorem Ipsum string paragraphs):
JS printed: Your viewport width is 1920x955
Element3: At Chrome, I inspected element1 view and element2 view. For element 2, with scroll bar, Chrome wrote width as 1903 pixel, not 1920.
My questions are:
Why element1 and element2 gave the same width? For element2, I was expecting new width = (1920 - scroll bar width). For example Chrome wrote 1903 pixel in its inspection tool.
I declared <meta name="viewport" content="initial-scale=1, width=device-width"> in my header as a meta tag. And in my CSS3, I declared #media screen and (max-width: 1000px) { change something for responsiveness } Since my aim is to be responsive in browser's viewport, does these 2 combination OK? At this point I should say that my viewport definition and aim is pure display width without vertical scroll bar. Because of my understanding, max-width:1000px means to me: be responsive in layout just after pure display width is <=1000px
javascript source link is: http://andylangton.co.uk/blog/development/get-viewport-size-width-and-height-javascript
<script type="text/javascript">
<!--
var viewportwidth;
var viewportheight;
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined')
{
viewportwidth = window.innerWidth,
viewportheight = window.innerHeight
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
else if (typeof document.documentElement != 'undefined'
&& typeof document.documentElement.clientWidth !=
'undefined' && document.documentElement.clientWidth != 0)
{
viewportwidth = document.documentElement.clientWidth,
viewportheight = document.documentElement.clientHeight
}
// older versions of IE
else
{
viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
viewportheight = document.getElementsByTagName('body')[0].clientHeight
}
document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
//-->
</script>
thank you in advance, regards
I got the point.
I changed the JS and got the true viewport width.
JS owner is Vilmantas Baranauskas from SO family.
related SO link: Get the height and width of the browser viewport without scrollbars using jquery?
related script:
<script type="text/javascript">
var viewportHeight;
var viewportWidth;
if (document.compatMode === 'BackCompat') {
viewportHeight = document.body.clientHeight;
viewportWidth = document.body.clientWidth;
} else {
viewportHeight = document.documentElement.clientHeight;
viewportWidth = document.documentElement.clientWidth;
}
document.write('<p>Your viewport width without scrollbars is '+viewportHeight+'x'+viewportWidth+'</p>');
</script>
1903 pixel width is true since scroll-bar std width is 17px as I know.
I also recommend to any one to use overflow-y:scroll; code in CSS Body or HTML tag in order to make browser display the scrollbar always even if for a blank draft web page.
I'm currently working on a responsive webdesign. The "smartphone view" is not yet ready because my client has to obtain some more budget.
Therefore I need to implement a temporary view which I want to realize with an fixed viewport that only gets activated on smartphones.
I set the viewport using on this way:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
I want to change the device-width to 700 pixels if the following media query gets triggered:
#media only screen and (max-width:700px){
}
The following code did not work:
#media only screen and (max-width:700px){
.pagewrapper{
width:700px;
}
#-ms-viewport{
width:700px;
}
#-o-viewport {
width: 700px;
}
#viewport {
width: 700px;
}
}
Do you know other solutions to get this done? Maybe using JavaScript / jQuery?
The accepted answer is correct, but if you're using jQuery and jQuery needs to be ready. On slow connections you get a problem with that code, so you might get '$' undefined errors.
That's why I prefer pure JavaScript in this case:
<meta name="viewport" id="viewport" content="width=device-width; initial-scale=1">
<script type="text/javascript">
(function setViewPort() {
if (screen.width < 640 && (window.orientation == 0 || window.orientation == 180)) {
document.getElementById("viewport").setAttribute("content", "width=480, initial-scale=0.68");
//alert('480 # 0.68, landscape');
} else if (screen.width < 640) {
// document.getElementById("viewport").setAttribute("content", "width=device-width, initial-scale=1");
// disabled, since its the same as the default. if this one is different, uncomment line above
//alert('device-width # 1, landscape');
} else if (screen.width >= 640) {
document.getElementById("viewport").setAttribute("content", "width=640; initial-scale=0.5");
//alert('640, high res phones');
}
})();
</script>
In my case I set 480px with 0.68 zoom for portrait, 320px with 1 zoom for landscape so the ppl can see bigger text and (because this is a mobile only page) if screen.width is higher than 640 (like on my android 5" phone with 1900x1080 screen size) to 640px with 0.5 zoom (since the width always stays at 320px).
Regards, Max
This is my solution, which works fantastic. This way the script just runs one time and gets executed before the document is ready. Also no-js is supported. Thanks for your help.
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript">
if($(window).width() < 765)
{
x=1;
if(x==1)
{
$('meta[name=viewport]').remove();
$('head').append('<meta name="viewport" content="width=765px, initial-scale=1.0">');
x=0;
};
};
</script>
#media only screen and (min-width:700px){
}
You can only change your css using this type of media query. If this gets hit min-width:700px write your css w.r.t this. And change your container width e.g <div class="pagewrapper"></div>
Following meta tag is perfect:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
If you are only targeting devices, I think you could try max-device-width instead of max-width or min-width.
if ($('body').width() < 700){
$('head').append('<meta name="viewport" content="width=700, initial-scale=1.0">');
}
else{
$('head').append('<meta name="viewport" content="width=device-width, initial-scale=1.0">');
}
I would like my webpage's viewport width to equal device-width as long as device-width > 450px, or 450px otherwise (my layout dynamically scales, but doesn't look good below 450px wide).
The following two meta tags work well on tablets, where the device-width > 450px:
<!-- uses device width -->
<meta name="viewport" content="width=device-width" />
<!-- use of initial-scale means width param is treated as min-width -->
<meta name="viewport" content="width=450, initial-scale=1.0" />
however, on phones (where e.g. device-width=320px) the former is too thin for the content; and the latter causes the browser to zoom in, so the user has to manually zoom out to see the content.
Alternatively, this meta tag works well on phones
<meta name="viewport" content="width=450" />
but doesn't take advantage of the extra width available on tablets.
Any help/ideas would be really appreciated (and if it makes a difference, I'm using GWT).
So you want to change the viewport tag's width dynamicaly .
Here you go :
<meta id="myViewport" name="viewport" content="width = 380">
<script>
window.onload = function () {
var mvp = document.getElementById('myViewport');
mvp.setAttribute('content','width=580');
}
</script>
See:http://www.quirksmode.org/mobile/tableViewport.html
Try this:
<meta id="vp" name="viewport" content="width=device-width, initial-scale=1">
<script>
window.onload = function() {
if (screen.width < 450) {
var mvp = document.getElementById('vp');
mvp.setAttribute('content','user-scalable=no,width=450');
}
}
</script>
Note that I have swapped the initial-scale=1, as I think you had it the wrong way round. You want initial-scale to be set to 1 when width=device-width, so that the page fits exactly in the window. When you set a specific viewport width, you don't want to set initial-scale to 1 (otherwise the page will start off zoomed in).
use a #media tag and css. It works wonders. Although it does not supply a minimal width to the view port, this is the preferred way to go.
Here is what I do for the viewport:
<meta name="viewport" content="initial-scale=1.0, width=device-width, user-scalable=yes, minimum-scale=1.0, maximum-scale=2.0">
Then I adjust the size for the panel attached to the viewPort:
#media all and (max-width: 1024px) {
/*styles for narrow desktop browsers and iPad landscape */
.myContentPanel{
width: 450;
}
}
#media all and (max-width: 320px) {
/*styles for iPhone/Android portrait*/
.myContentPanel {
width: 320;
}
}
Obviously you can have intermediate sizes too...
here's more in another example
The JavaScript code given in the other answers doesn't work in Firefox, but it will work if you remove the meta tag and insert a new one.
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
if (screen.width < 450){
var viewport = document.querySelector("meta[name=viewport]");
viewport.parentNode.removeChild(viewport);
var newViewport = document.createElement("meta");
newViewport.setAttribute("name", "viewport");
newViewport.setAttribute("content", "width=450");
document.head.appendChild(newViewport);
}
</script>
Or just always insert it in JavaScript:
<script>
var viewport = document.createElement("meta");
viewport.setAttribute("name", "viewport");
if (screen.width < 450) {
viewport.setAttribute("content", "width=450");
} else {
viewport.setAttribute("content", "width=device-width, initial-scale=1");
}
document.head.appendChild(viewport);
</script>
For my sanity, I wrote a polyfill to just add a min-width attribute to the viewport meta tag:
Set min-width in viewport metatag
With this, you could just do:
<meta name="viewport" content="width=device-width, initial-scale=1.0, min-width=450" />
<script type="text/javascript" src="viewport-min-width.js"></script>
In short, there is no need to set min-width on viewport because you can set it on body or html element instead, to make them and their content wider than viewport. User will be able to scroll or zoom out content.
body {
min-width: 450px;
}
I did some tests in Chrome for Android and it scales fonts of some elements up if viewport width is set to anything other than device-width. Also shrink-to-fit=yes is useful to have a page zoomed out initially.
Lastly, this approach supports desktop browsers that can have strange window sizes or current zoom settings (both of which affect reported viewport dimensions), but don't honor the viewport meta tag.
Extending #Brendan and other's answer. The viewport size doesn't adjust again on orientation (portrait, landscape) change. To cater this, add an event listener on orientation change and resize again.
<script>
const resize = () => {
if (screen.width < 450) {
var viewport = document.querySelector("meta[name=viewport]");
viewport.parentNode.removeChild(viewport);
var newViewport = document.createElement("meta");
newViewport.setAttribute("name", "viewport");
newViewport.setAttribute(
"content",
"width=450, maximum-scale=1.0, user-scalable=no"
);
document.head.appendChild(newViewport);
}
};
resize();
window.addEventListener("orientationchange", resize);
</script>
I just removed initial-scale=1 and perfectly working on Android Chrome and built-in browsers. No unexpected zoom anymore! :)
<meta name='viewport' content='width=device-width'>