Lecture

Viewport

The Viewport refers to the visible area of a web page that a user can see.

The viewport can be set within the HTML <meta> tag, located inside the HTML head element.


Example of using the meta tag

Adding a meta tag within the HTML head tag
<head> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> </head>

The meta tag example provided informs the web browser how to display the web page on mobile and various screen sizes.

The code tells the web browser to set the width of the webpage to the width of the device's screen, and set the initial zoom scale to 1.

  • name="viewport": This meta tag is related to the viewport settings.

  • content attribute:

    • width=device-width: Adjusts the page width to match the width of the device screen. This means the horizontal size of the webpage matches the actual width of the device.

    • initial-scale=1.0: Sets the initial zoom level when the page first loads. 1.0 means 100%, displaying the webpage to exactly fit the width of the screen.


vw and vh

vw and vh are units based on the viewport width and height.

Using vw and vh units allows for flexible adjustment of element sizes according to the screen dimensions.


vw (Viewport Width)

1vw means 1% of the viewport (screen) width.

If you divide the screen width into 100 equal parts, one part's width is 1vw.

If the total screen width is 1000px, setting an element's width to 50vw results in the element width being 500px.

CSS
.box { width: 50vw; /* 50% of the screen width */ }

vh (Viewport Height)

1vh means 1% of the viewport (screen) height.

If you divide the screen height into 100 equal parts, one part's height is 1vh.

If the total screen height is 800px, setting an element's height to 25vh results in the element height being 200px.

CSS
.box { height: 25vh; /* 25% of the screen height */ }
Mission
0 / 1

Questions about vw and vh units

If the screen width is 1200px, what would be the actual width of an element set to 30vw?
360px
400px
300px
240px

Lecture

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help