Auto-fit fixed-width layout to screen width - javascript

I have a website with a fixed-width layout, and that cannot be changed easily. Its width is around 1300px, so it doesn't render well on a smaller screen.
I am looking for a solution to make the website looks good even on small screens. It should be possile, because if the user changes the browser zoom level to something like 75%, everything looks quite good. But I read that changing browser zoom level is not possible in JavaScript, and that the behaviour of this feature is not consistent across browsers.
Is there a standard solution (a library or something) to solve this problem?

You already put the tag "responsive" in your tags. This means you know that you want a responsive website.
You could create mediaqueries on specific width's and set the zoom property to a suitable value (but Firefox does not support this: http://caniuse.com/#search=zoom ; you can use transform: scale() as fallback).
EXAMPLE:
#media (max-width: 600px) {
body {
zoom: 0.75;
moz-transform: scale(0.75, 0.75);
}
}
The best solution would be to create mediaqueries on specific width's and take the effort of changing the width's of the elements. I don't see why the width can't be changed easily.
EXAMPLE:
#media (max-width: 600px) {
.myElement {
width: 600px; /* whatever, maybe 100%? */
}
}

For a start include this meta tag to your page's <head>:
<meta viewport="width=device-width, initial-scale=1.0">
The above will force the website to scale on mobile devices. But you need to do some more work using media queries
You can create different rules for different screen sizes:
#media(max-width: 768px){
/** Small Mobile Screens */
}
#media(min-width: 768px){
/** Large Mobile Screens, tablets e.t.c */
}
#media(min-width: 992px){
/** Desktops, Laptops */
}
#media(min-width: 1200px){
/** Larger Screens (Desktops, Laptops, e.t.c) */
}
You can set the width of your page to a different size in pixels for every screen

Related

How do I scale an entire webpage for tablet screen width on load with javascript?

I have a webpage that is 1800x1200px in dimension (made for desktop PCs). On tablets the page isn't viewed entirely, part of it spans over the right viewport. What I want to achieve is that the webpage displays correctly on tablets using a smaller zoomfactor. I'm absolute beginner with javascript, can anyone explain the js code to me to do that?
Try adding this in the <head> section
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Also you might have to use CSS media queries. If you are not familiar with that, Its better to learn it first.
For the time being, use the below media queries in your css
#media (min-width:320px) { /* smartphones, iPhone, portrait 480x320 phones */ }
#media (min-width:481px) { /* portrait e-readers (Nook/Kindle), smaller tablets # 600 or # 640 wide. */ }
#media (min-width:641px) { /* portrait tablets, portrait iPad, landscape e-readers, landscape 800x480 or 854x480 phones */ }
#media (min-width:961px) { /* tablet, landscape iPad, lo-res laptops ands desktops */ }
#media (min-width:1025px) { /* big landscape tablets, laptops, and desktops */ }
#media (min-width:1281px) { /* hi-res laptops and desktops */ }
Sample usage
/* Use a media query to add a breakpoint at 768px: */
#media screen and (min-width: 768px and max-width: 1023px) {
.main{
width: 80%; /* The main class's width is 80% , when the viewport is gretaer than 768px or smaller than 1023px which is ideal for tablets (not big tablets) */
}
}
You can use media queries for achieving this behaviour, example as follows -
#media only screen and (max-width: 600px) {
body {
background-color: lightblue;
}
}
This will apply a background color when the screen size will be 600px or smaller.
The best answer i can give right now is to use css zoom
body{zoom:30%}
body:after{content:"lol"}

How can I roughly determine the physical size of a device in javascript or css?

I am building a web page which contains svg graphics. I want to make it responsive.
If the page is loaded on a physically small device, i.e. a smart phone, the graphics is too small to be viewed well, so I have to change it in case of a smart phone client.
The exact device or even the exact screen resolution is not important. Important is that I can answer the question "Is the client a small device?".
How can I do that?
Assuming that this is a common question, there should be a best practise for this or a common library. Can you help me?
I'd suggest doing it like this, putting this tag on the head of your index.html:
<meta name="viewport" content="width=device-width, initial-scale=1">
This will allow you to work with media queries, where you can choose different css style properties depending on the size of the screen. Some examples:
#media all and (min-width:1200px){ ... }
#media all and (min-width: 960px) and (max-width: 1199px) { ... }
#media all and (min-width: 768px) and (max-width: 959px) { ... }
#media all and (min-width: 480px) and (max-width: 767px){ ... }
#media all and (max-width: 599px) { ... }
#media all and (max-width: 479px) { ... }
There are many other ways to set up the media queries, there is definitely one that is perfect for what you're trying to do.
W3 media query example
Using media queries by Mozilla
Finally, I'd suggest setting the height, max-height and width and max-width styles with the vh and vw units, such as:
height: 50vh;
width: 50vw;
VH and VW stands for viewport height and viewport width. This will make it so an image's size, for example, is exactly half the size of a device screen.

How to change the body width when the browser window is resized, so that the content is easily read?

Here, the web page is displayed in a 1349x659 browser window. (it's exactly the dimensions of the image). With the body width set to 60%, the content is nicely packed and easily read.
Here I have resized the browser window to 478x642. And you can see that the 60% body width no longer gives the good view.
Since the web page can be resized without the server even knowing, the solution must be in the client side. Or done in the css file.
I suggest this algorithm:
If the visitor is mobile, then the body width should be 100% regardless of any other thing.
If the visitor is not mobile (tablet, pc, ...) then (
If the width of the browser window is larger than the height, body width will be equal to the window height.
but if the width is less than the height, then the body width is 100%.
)
Here is a fiddle
https://jsfiddle.net/rawj7vxc/
<body>
Some posts are not public. To access them, please login to your account, or register if you have none yet. By logging in to your account, you're no longer considered a "guest", this has some benefits which come with account promotions. Seeing more posts is one of those benefits. Registered accounts need verification. You'll be told how to this after the first unverified login. The verification will, however, not be immediate.
</body>
body {
background-color: rgb(31,25,0);
width: 60%;
padding: 0;
margin: 0 auto;
font-family: consalos;
text-align: justify;
color: rgb(215,200,0);
overflow: auto;
}
You need to look into using media queries. These will allow you to cater your design to the width and height of your screen.
/* Large Devices, Wide Screens */
#media only screen and (max-width : 1200px) {
}
/* Medium Devices, Desktops */
#media only screen and (max-width : 992px) {
}
/* Small Devices, Tablets */
#media only screen and (max-width : 768px) {
}
/* Extra Small Devices, Phones */
#media only screen and (max-width : 480px) {
}
/* Custom, iPhone Retina */
#media only screen and (max-width : 320px) {
}
Media Queries
You could use some framework like Bootstrap or media queries:
#media (min-width: 568px) {
.myDiv{
width: 550px;
}
}
#media (min-width: 992px) {
.myDiv{
width: 970px;
}
}
#media (min-width: 1200px) {
.myDiv{
width: 1170px;
}
}
Here you have more information: w3schools.com
And also about Bootstrap: getbootstrap.com
I think you could use the metatag viewport (see documentation here)
viewport element gives the browser instructions on how to
control the page's dimensions and scaling.
The width=device-width part sets the width of the page to follow the
screen-width of the device (which will vary depending on the device).
The initial-scale=1.0 part sets the initial zoom level when the page
is first loaded by the browser.
In your head section, simply add following tag:
<meta name="viewport" content="width=device-width, initial-scale=1">
If you want to add width: 60% only when the screen is "wide" you could use:
#media screen and (orientation:landscape) {
body {
width: 60%;
}
}
See here
I hope it helps you, bye.
Ok, here is my solution with pure javascript,
document.getElementByTagName("body").onresize=function(){
var fullwidth=screen.width;
var fullheight=screen.height;
if(fullwidth<aval){ //checking if mobile
/*
* make width 100%
*/
}
else
{
if(fullwidth>fullheight)
//make width = fullheight
else
// make width=100%
}
}
I have given you the logic structure only, if you want full css change(width and height change) code, mention here.
N.B. aval is the threshold value under which screen size device will be treated as mobile device

to make buttons resizable according to screen sizes using jquery/javascript

I want to make my button controls resizable according to the screen sizes, like they should adjust themselves on other mobile devices as well(iPhones and iPads) .How is it possible?
Css3 has mediaqueries which allows you make screen specific styles. This is not very well supported in older IE's, that is why you always have to define an normal.
The cascading effect stays in affect, you do not need to redefine properties from normal in the mediaqueries (for example, background will be green in all scenarios)
/*normal*/
button{
width: 200px;
background: green;
}
#media only screen and (max-width: 600px) {
button{
width: 150px;
}
}
#media only screen and (max-width: 480px) {
button{
width: 100px;
}
}
This is called responsive design, the design responds to the widths. IE will do nothing, but if you are using Firefox and make the width of the browser smaller, it will hop automatically to the media styles
Well you gotta use media queries for that :
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries
Use percentage based sizes on your elements so that they scale automatically, or use media queries for specific window sizes, and set your element sizes accordingly.
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries
You can make them resizable by setting their width in percentage, so that they would resize according to the screen size,
.buttonclass
{
width:80%;
}
This should work..
if you want to use pixels, then make use of media queries according to various screens you need to support,
#media screen and (max-width: 480px) and (min-width: 320px) {
.buttonclass{
width:300px;
}
}

Resolution-related buttons/elements - howto?

I need to place some graphic elements in a div that is supposed to be seen on various screens with different resolutions (for example, mobile screens). Just for example, something similar to the "X" button that closes an overlay window. I might think of several options to implement it:
Have several JPEGs (one for each resolution) and select them in JavaScript
Render the graphics (it is simple) using HTML5 features
Any CSS support probably?
Anything else?
It would be great to have any kind of advice/well-known practice, so I won't reinvent the wheel.
css rule:
img {
width: 100%;
}
no?
And use #media rule:
#media only screen and (min-width: 768px) and (max-width: 991px) {
div {
width: 100px;
}
}

Categories