Posts
Posts
Quick and easy way to get your site just working on mobile

Quick and easy way to get your site just working on mobile

AKA how to use <meta name="viewport">.

So you're making a website. Let's use this as an example:

Screenshot of a quite blue website. It has a large logo banner at the top, and a three column layout below. Navigation links are on the left and right columns, and the main content is in the center column.

Looks great on desktop! On a phone, however:

The website. now on a phone. The top banner goes off-screen, and the two sidebars overlap the main content, making it unreadable.

Ouch. Luckily, there's a really easy way to just get it looking just as it does on desktop. People on mobile will still have to zoom and pan around, but at least they can use your website.

Introducing the <meta name="viewport"> tag!

This special tag, put inside of your <head>, essentially tells mobile browsers how it should display your website. You may already have it on your page. This is a very common one:

HTML
<head>
  ...
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  ...
</head>

However, you likely shouldn't have it like the above! This is pretty much telling browsers that your website should look fine on mobile, so if your website doesn't, it'll just let it break and bleed like it did to the example above.

A potentially very easy solution would be to just remove it all together. By doing so, mobile browsers will default to just shrinking down your page to try to make it look right. Here's that above website on mobile, but without the <meta viewport> tag:

The website on mobile again. It appears as it does on desktop, with it being shrunken down to fit. There's some empty space on both sides of the page however, which could be reduced.

But this isn't really ideal either. It would be best to say specifically what your website works with instead of just leaving it up to the browser.

So let's actually utilize <meta name="viewport">!

Telling it how to shrink your page

You do this using width. Like for instance:

HTML
<head>
  ...
  <meta name="viewport" content="width=800" />
  ...
</head>
...

Here, mobile browsers will display your page as if it's in a 800 pixel wide window, and then shrink it down to make it fit.

Figuring out what size to use for your website

In most browsers, Inspect Element (really called the Developer Tools) will tell you how big the page in pixels is when you resize the page.

So, right click on your webpage, and click Inspect. You can just leave it open; we won't be touching it.

Resize your browser right before the point your website starts to break down. While you're doing this, your browser should show the size of the page at the top right:

Screenshot of a browser with the website open. Inspect element is open below. At the top right of the page, it says "750px by 845.33px".

Zoom up of the previous screenshot on the top right, where the page size is.

The first number is what you should plug into width. So, for this page, it would be 750:

HTML
<head>
  ...
  <meta name="viewport" content="width=750" />
  ...
</head>
...

(If you get a decimal, just round to the nearest whole number.)

That's pretty much all there is to it! Mobile users can now at least now just use your website.