CSS Viewport
The viewport is the visible area of a web page displayed inside a browser window or device screen.
The viewport is especially important for responsive websites because mobile devices have much smaller screen widths than desktop computers.
What is a Viewport?
The viewport is the area of a webpage that is currently visible to the user.
On a desktop, the viewport can be wide, while on a mobile phone it is much narrower.
Viewport Diagram
This area represents the visible portion of the webpage.
The viewport represents the visible area available for displaying a webpage.
Viewport Meta Tag
The viewport is commonly configured using the viewport meta tag inside the <head> section of an HTML document.
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
This tells the browser to use the device's actual screen width as the viewport width.
It also sets the initial zoom level to 1.0.
width=device-width
<meta name="viewport"
content="width=device-width">
width=device-width makes the viewport width equal to the device's screen width.
This helps webpages display correctly on different devices.
initial-scale
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
initial-scale defines the initial zoom level of the webpage.
A value of 1.0 represents the default scale.
Common Viewport Properties
width
Defines the width of the viewport. The commonly used value is device-width.
initial-scale
Defines the initial zoom level of the webpage.
minimum-scale
Defines the minimum zoom level that can be applied to the page.
maximum-scale
Defines the maximum zoom level that can be applied to the page.
Responsive Viewport Example
<!DOCTYPE html>
<html>
<head>
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Responsive Page</title>
</head>
<body>
<h1>Responsive Website</h1>
<p>
This page can adapt to different screen sizes.
</p>
</body>
</html>
The viewport meta tag allows the browser to use the device's actual width when displaying the webpage.
This is an important foundation for building responsive websites.
Output
Responsive Website
This page can adapt to different screen sizes.
Why is the Viewport Important?
- Helps webpages adapt to mobile screens.
- Allows the browser to use the device width.
- Provides the foundation for responsive layouts.
- Works together with CSS media queries.
- Helps prevent unwanted scaling on mobile devices.
Real-World Uses
- Mobile-friendly websites
- Responsive web applications
- E-commerce websites
- Training institute websites
- Business websites
Summary
The viewport represents the visible area of a webpage. The viewport meta tag allows a webpage to use the device's actual screen width and define its initial scale. The configuration width=device-width, initial-scale=1.0 is commonly used as a foundation for responsive web design.