Viewport Extra enables setting the minimum and maximum width of the viewport, by overriding the content attribute of the viewport meta element. It will reduce the range of the viewport that needs to be considered when styling.
For example, if a page with a width of 430px is displayed in mobile device browsers that display a width of 360px (e.g. Galaxy S24 in portrait mode), horizontal scrolling will typically occur. Therefore, styling for media less than 430px is needed. However, by setting the minimum width of the viewport for the page to 430px with Viewport Extra, the page will be scaled down to fit perfectly into a width of 360px and horizontal scrolling will not occur. As a result, styling for media less than 430px is not needed.
It is recommended to use a script element with the async attribute or the dynamic import.
The following codes will scale the page down in mobile device browsers that display a width less than 430px, and will do nothing in mobile device browsers that display a width of 430px or greater. Scaling occurs only once when the page is displayed. See also "Rescale when device orientation changes".
<meta name="viewport-extra" content="min-width=430" />
<script async src="https://cdn.jsdelivr.net/npm/viewport-extra@2.4.1/dist/iife/viewport-extra.min.js"></script>
import('viewport-extra').then(({ setContent }) => {
setContent({ minWidth: 430 })
})
<meta name="viewport" content="initial-scale=0.8372093023255814,width=430" />
<meta name="viewport" content="initial-scale=0.913953488372093,width=430" />
<meta name="viewport" content="initial-scale=1,width=device-width" />
<meta name="viewport" content="initial-scale=1,width=device-width" />
<meta name="viewport" content="initial-scale=1,width=device-width" />
The following codes will scale the page up in mobile device browsers that display a width greater than 393px, and will do nothing in mobile device browsers that display a width of 393px or less. Scaling occurs only once when the page is displayed. See also "Rescale when device orientation changes".
<meta name="viewport-extra" content="max-width=393" />
<script async src="https://cdn.jsdelivr.net/npm/viewport-extra@2.4.1/dist/iife/viewport-extra.min.js"></script>
import('viewport-extra').then(({ setContent }) => {
setContent({ maxWidth: 393 })
})
<meta name="viewport" content="initial-scale=1,width=device-width" />
<meta name="viewport" content="initial-scale=1,width=device-width" />
<meta name="viewport" content="initial-scale=1.094147582697201,width=393" />
<meta name="viewport" content="initial-scale=1.8676844783715012,width=393" />
<meta name="viewport" content="initial-scale=2.6055979643765905,width=393" />
The following codes will scale the page down in mobile device browsers that display a width less than 430px or between 744px and 1023px, and will do nothing in mobile device browsers that display a width between 430px and 743px or 1024px and above. Scaling occurs only once when the page is displayed. See also "Rescale when device orientation changes".
<meta name="viewport-extra" content="min-width=430" />
<meta name="viewport-extra" content="min-width=1024" data-media="(min-width: 744px)" />
<script async src="https://cdn.jsdelivr.net/npm/viewport-extra@2.4.1/dist/iife/viewport-extra.min.js"></script>
import('viewport-extra').then(({ setParameters }) => {
setParameters([
{ content: { minWidth: 430 } },
{ content: { minWidth: 1024 }, media: '(min-width: 744px)' }
])
})
<meta name="viewport" content="initial-scale=0.8372093023255814,width=430" />
<meta name="viewport" content="initial-scale=1,width=device-width" />
<meta name="viewport" content="initial-scale=0.7265625,width=1024" />
<meta name="viewport" content="initial-scale=1,width=device-width" />
The following codes will scale the page not only when it is displayed, but also each time the mobile devices switch between portrait and landscape mode.
<meta name="viewport" content="width=device-width,initial-scale=1" data-extra-unscaled-computing />
<meta name="viewport-extra" content="min-width=430" />
<meta name="viewport-extra" content="min-width=744" data-media="(min-width: 640px)" />
<script async src="https://cdn.jsdelivr.net/npm/viewport-extra@2.4.1/dist/iife/viewport-extra.min.js"></script>
<script>
const handleOrientationChange = () => {
if (!ViewportExtra) return
window.addEventListener(
'resize',
() => ViewportExtra.setParameters([]),
{ once: true }
)
}
if (screen && screen.orientation) {
screen.orientation.addEventListener('change', handleOrientationChange)
} else {
window.addEventListener('orientationchange', handleOrientationChange)
}
</script>
import('viewport-extra').then(({ setParameters }) => {
setParameters(
[
{ content: { minWidth: 430 } },
{ content: { minWidth: 744 }, media: '(min-width: 640px)' }
],
{ unscaledComputing: true }
)
const handleOrientationChange = () => {
window.addEventListener(
'resize',
() => setParameters([]),
{ once: true }
)
}
if (screen && screen.orientation) {
screen.orientation.addEventListener('change', handleOrientationChange)
} else {
window.addEventListener('orientationchange', handleOrientationChange)
}
})
<meta name="viewport" content="initial-scale=0.913953488372093,width=430" />
<meta name="viewport" content="initial-scale=0.9865591397849462,width=744 />
The following codes will truncate numbers in the content attribute of the viewport meta element to 6 decimal places.
<meta name="viewport-extra" content="min-width=430" data-decimal-places="6" />
<script async src="https://cdn.jsdelivr.net/npm/viewport-extra@2.4.1/dist/iife/viewport-extra.min.js"></script>
import('viewport-extra').then(({ setParameters }) => {
setParameters(
[
{ content: { minWidth: 430 } }
],
{ decimalPlaces: 6 }
)
})
<meta name="viewport" content="initial-scale=0.837209,width=430" />
<meta name="viewport" content="initial-scale=0.913953,width=430" />
<meta name="viewport" content="initial-scale=1,width=device-width" />
All of the parameter settings below have the same meaning, even if the width and initial scale in the content attribute or the content property are omitted.
<meta
name="viewport-extra"
content="width=device-width,initial-scale=1,min-width=430,max-width=640"
/>
<meta
name="viewport"
content="width=device-width,initial-scale=1"
data-extra-content="min-width=430,max-width=640"
/>
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="viewport-extra" content="min-width=430,max-width=640" />
setContent({
width: 'device-width',
initialScale: 1,
minWidth: 430,
maxWidth: 640
})
setParameters([
{
content: {
width: 'device-width',
initialScale: 1,
minWidth: 430,
maxWidth: 640
}
}
])
-
For small mobile devices, it is recommended to set the following style:
body { -webkit-text-size-adjust: 100%; }
It prevents unintentional text size adjustments by browsers. See also the issue.
-
Viewport Extra v2 does not support AMD. If it is needed use v1.