Wow! These answers are really complicated. I’m just a newbie, but I think I found a simple fix. It might take a while (and if you’re almost done with your website, it might take too long to do and you should just follow the other solutions), but it works.
Note: This solution, although simple, (obviously) requires a basic understanding of all 3 primary languages for creating websites, JavaScript, HTML, and CSS, since all 3 are used in this solution.
First off type this into the head tag in HTML.
<meta name=“viewport” content=“width=device-width, initial-scale=1.0“>
This basically tells the browser to make sure the width of the viewport (visible area of the website) is equal to the width of the device being used itself. It also tells the browser to have 100% zoom when opening the website, denoted by initial-scale=1.0
.
The next part is in JavaScript. You need to find the width of the viewport. One method you might be able to use is
console.log(window.innerWidth)
Basically, this returns the width of the viewport in pixels into the console
, which can be accessed using Ctrl + Shift + I > “Console”
Finally, the long, hard part. This one is done in CSS. You must convert all units on the site. Most of the units you probably use are all in px
, or pixels. Pixel width varies with different devices, so, for example, a picture at the complete right edge of a website might show up in a computer, but it might not show up on a phone or tablet because the position in px
from the left might exceed its amount of pixels, if that makes sense.
Anyway, you can convert px
to vw
. The vw
unit, while px
is a fixed unit, is a relative unit. It is relative to the width of the viewport.
1vw = 1% of the width of the viewport
Therefore, all devices have a width of 100vw
, even if they all have different pixel widths. So, using this knowledge, you can convert x
pixels into this unit using the equation below.
x*(100vw/device width in pixels)
Use a calculator to calculate this. Once all your units are in vw
, your browser will position all your elements relative to the viewport, and the look of your website will barely change.
Hopefully this helps!