Side effects of removing "width=device-width" from viewport meta tag when "initial-scale=1.0" is also set
Asked Answered
A

1

15

While the <meta name="viewport"> tag is unstandardized, it "is respected by most mobile browsers due to de-facto dominance."

One downside of it not being a true web standard is detailed documentation is not as available as other standards. The CSS Working Group has a specification for this, so that is what I am mainly using as an authoritative work.

My main question is:

What would the perceived difference be between the following declarations?

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="initial-scale=1.0">

Alternatively asked, what are the differences between these two @viewport CSS at-rules:

/* Translated from <meta name="viewport" content="width=device-width, initial-scale=1.0"> */
@viewport {
    zoom: 1.0;
    min-width: extend-to-zoom;
    max-width: 100vw;
}
/* Translated from <meta name="viewport" content="initial-scale=1.0"> */
@viewport {
    zoom: 1.0;
    min-width: extend-to-zoom;
    max-width: extend-to-zoom;
}

How I arrived at those @viewport translations:

width=device-width to min-width: extend-to-zoom; max-width: 100vw;

The CSS Device Adaptation Module Level 1 document states:

The width and height viewport <META> properties are translated into width and height descriptors, setting the min-width/min-height value to extend-to-zoom and the max-width/max-height value to the length from the viewport.

They additionally give an example:

This <META> element:

<meta name="viewport" content="width=500, height=600">

translates into:

@viewport {
    width: extend-to-zoom 500px;
    height: extend-to-zoom 600px;
}

The width shorthand descriptor is described as:

This is a shorthand descriptor for setting both min-width and max-width. One <viewport-length> value will set both min-width and max-width to that value. Two <viewport-length> values will set min-width to the first and max-width to the second.

So it stands to reason that width: extend-to-zoom 500px; is equivalent to min-width: extend-to-zoom; max-width: 500px;.

That only leaves the 100vw part. Within section 10.4, they explain:

device-width and device-height translate to 100vw and 100vh respectively

So we can finally see how width=device-width is translated to min-width: extend-to-zoom; max-width: 100vw;.

initial-scale=1.0 to zoom: 1.0; width: extend-to-zoom;

This one is a verbatim example:

This <META> element:

<meta name="viewport" content="initial-scale=1.0">

translates into:

@viewport {
    zoom: 1.0;
    width: extend-to-zoom;
}

The other question I have here is, what exactly is the extend-to-zoom value?

The documentation on it and its resolution procedure are difficult to grasp. If anyone can point me toward some further examples on this that'd be greatly appreciated.


In addition to everything above, I've put together a quick site - https://romellem.github.io/temp-site/viewport/ - to test some viewport configurations.

Namely:

This may help with testing.

Appalachia answered 15/4, 2020 at 21:2 Comment(0)
R
19

Before we delve into what you're asking, let's review a little about why the viewport meta tag exists in the first place. Here's what I've gathered.


Why do we need the viewport tag?

A viewport is an area where the web content can be seen. Usually, the rendered page (web content) is bigger than the viewport. As a result, we usually use scrollbars to see the hidden content (because the viewport can't display everything). Quoted from CSS Device Adaptation Module Level 1:

The narrow viewport is a problem for documents designed to look good in desktop browsers. The result is that mobile browser vendors use a fixed initial containing block size that is different from the viewport size, and close to that of a typical desktop browser window. In addition to scrolling or panning, zooming is often used to change between an overview of the document and zoom in on particular areas of the document to read and interact with.

In mobile devices (and other smaller devices), the initial containing block is usually larger than the viewport. For example, a mobile device that has a screen width of 640px might have an initial containing block of 980px. In this case, the initial containing block is shrunk to 640px so that it can be fit into the mobile screen. This 640px width (screen width) is what is called initial-width of the viewport which will be pertinent to our discussion.

So.... Why do we need this viewport tag? Well, nowadays, we have media queries which allows us to design for mobile devices. However, this media query depends on the actual viewport's width. In mobile devices, the user agent automatically styles the initial viewport size to a different fixed one (usually larger than the initial viewport size). So if the viewport's width of a mobile device is fixed, the CSS rules we use in media queries won't be executed simply because the viewport's width never changes. Using the viewport tag, we can control the actual viewport's width (after being styled by the UA). Quoted from MDN:

However, this mechanism is not so good for pages that are optimized for narrow screens using media queries — if the virtual viewport is 980px for example, media queries that kick in at 640px or 480px or less will never be used, limiting the effectiveness of such responsive design techniques.

Note that the viewport tag can change the actual viewport's height too, not just the width


viewport tag's width

The width in a viewport tag is translated to max-width in the @viewport rule. When you declare the width as device-width, it is translated to 100% in the @viewport rule. Percentage value is resolved based on the initial-width of the viewport. So if we're still using the above example, the max-width will resolve to a value of 640px. As you've found out, this only specifies the max-width. The min-width will automatically be extend-to-zoom.


extend-to-zoom

Your question was what exactly is the value of extend-to-zoom? From what I've gathered, it's the value that's used to support the viewport extending itself to fit the viewing area at a given zoom level. In other words, it's a viewport size value that changes based on the zooming value specified. An example? Given that the max-zoom value of the UA stylesheet is 5.0 and the initial-width is 320px, <meta name="viewport" content="width=10"> will resolve to an initial actual width of 64px. This makes sense because if a device only has 320px and can only be zoomed 5x the normal value, then the minimum viewport size would be 320px divided by 5, which means showing only 64px at a time (and not 10px because that would require zooming 32x!). This value will be used by the algorithm to determine how to extend (change) the min-width and max-width values, which will play a role in determining the actual viewport width.


Putting it all together

So essentially, what's the difference between <meta name="viewport" content="width=device-width, initial-scale=1.0"> and <meta name="viewport" content="initial-scale=1.0">? Simply redo the steps of the algorithm (this and this). Let's do the latter (the one without width attribute) first. (We will assume that the initial-width of the viewport is 640px.)

  • width is not set, this results in max-width and min-width being extend-to-zoom in the @viewport rule.
  • initial-scale is 1.0. This means that the zoom value is also 1.0
  • Let's resolve extend-to-zoom. Steps:
    1. extend-zoom = MIN(zoom, max-zoom). The MIN operation resolves to the value that is non-auto. Here, zoom is 1.0 and max-zoom is auto. This means that extend-zoom is 1.0
    2. extend-width = initial-width / extend-zoom. This is easy; divide 640 by 1. You get extend-width is equal to 640
    3. Because extend-zoom is non-auto, we will skip to the second conditional. max-width is indeed extend-to-zoom, this means that max-width will be set to extend-width. Thus, max-width = 640
    4. min-width is also extend-to-zoom, this means setting min-width to max-width. We get min-width = 640
  • After resolving the non-auto (i.e. the extend-to-zoom) values for max-width and min-width. We can proceed to the next procedure. Because min-width or max-width is not auto, we will use the first if in the procedure, thus setting the initial actual viewport width to MAX(min-width, MIN(max-width, initial-width)), which equates to MAX(640, MIN(640, 640)). This resolves to 640 for your initial actual viewport width
  • Moving on to the next procedure. In this step, width is not auto. The value isn't changed and we end up with the actual viewport width of 640px

Let's do the former.

  • width is set, this results in max-width being 100% (640px in our case) and min-width being extend-to-zoom in the @viewport rule.
  • initial-scale is 1.0. This means that the zoom value is also 1.0
  • Let's resolve extend-to-zoom. If you follow the steps carefully (almost the same as above), you will end up with a max-width of 640px and a min-width of 640px.
  • You can probably see the pattern now. We will get the actual viewport width of 640px.

So what's the perceived difference? Essentially none. Both of them do the same thing. Hope my explanation helps ;-) If anything was amiss, do tell me.

Rivalee answered 24/4, 2020 at 14:12 Comment(7)
This is great and is in line with what I was looking for. I'll mark this as the answer but I'd love some explanation of when you could find differences. It seems like the main differences would be if you have initial content of some small with and an initial zoom not set to 1.0, but I'm unsure on this. Anyways, great answer, thanks!Appalachia
@Appalachia Yes, one of the ways you can find a perceived difference when specifying the viewport tag attribute is when you do not specify the initial-scale. For example, when you write <meta name="viewport" content="360px">, you know that you aren't controlling the initial zoom level. As such, from my limited testing, the UA style always shrinks (through some procedure that might not be normative) the viewport. However, the actual viewport will always stay at a width of 360px, regardless of the intial viewport size.Rivalee
@Appalachia Continuation. However, when you write <meta name="viewport" content="360px", initial-scale=1.0>, you're making the min-width to extend-to-zoom and coupled with a zoom value, this will always resolve to the largest value between the initial viewport width divided by zoom value and the max-width. In the constraining procedure (section 7.2), you will see that the width = MAX(min-width, MIN(max-width, initial-width)). From my limited testing, my inference is that when the max-width specified is smaller than the...Rivalee
@Appalachia Continuation. ... actual viewport width, it will always resolve to the initial viewport width. However, when the max-width value is larger than the initial viewport width, the minimum value would be the initial viewport width and the maximum value will be the, well, max-width value. The extend-to-zoom semantically makes sense because the extend-to-zoom on the min-width literally extends the value to the viewport width when the zoom is 1.0. And this aligns with the answer above, that extend-to-zoom alters/extends/enlongates the width value given a zoom level.Rivalee
@Appalachia Of course, it's not limited to just controlling the initial-scale attribute, but it's one of the ways to produce a perceived difference. Revision to my first comment, the UA style can also zoom-in (not only shrink) the viewport when the device width is larger than 360px.Rivalee
"A viewport is an area where the web content can be seen". On desktop we can resize the browser-window. Does this mean viewport width and height change? Is 'viewport' really the same as part of the browser window which can show user-content?Transpierce
@PanuLogic The Net Ninja has a nice video on this.Robbi

© 2022 - 2024 — McMap. All rights reserved.