-
Hajipur, Bihar, 844101
The viewport is the visible area of a web page in the user's browser window. On desktops, the viewport size is typically the browser window size. On mobile devices, it's the screen width of the device, but can behave differently if not properly configured.
To ensure websites look and behave correctly on different devices, we must configure the viewport using the <meta>
tag.
Without setting the viewport:
Pages appear zoomed out on mobile
Users need to scroll horizontally
Fonts and layouts do not scale properly
The responsive styles (e.g., media queries) don’t work as expected
<meta name="viewport" content="width=device-width, initial-scale=1.0">
width=device-width
: Sets the page width to match the device screen width.
initial-scale=1.0
: Sets the zoom level when the page is first loaded.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Viewport Example</title>
<style>
body {
font-family: Arial;
font-size: 16px;
}
.box {
width: 90%;
margin: 20px auto;
background: #f0f0f0;
padding: 20px;
}
</style>
</head>
<body>
<div class="box">This box scales based on the screen size.</div>
</body>
</html>
Attribute | Purpose |
---|---|
minimum-scale |
Limits how much the user can zoom out |
maximum-scale |
Limits how much the user can zoom in |
user-scalable=no |
Disables pinch zoom |
height=device-height |
Not often used; sets height explicitly |
✅ Write HTML/CSS to solve these tasks:
Q1. Create a basic responsive page that uses the correct viewport tag.
Q2. Remove the viewport tag and observe how the layout breaks on mobile.
Q3. Set a fixed width and compare it with width=device-width
.
Q4. Add maximum-scale=2.0
and test zooming on mobile.
Q5. Disable zooming using user-scalable=no
.
Q6. Style a div that adjusts width using width: 90%
.
Q7. Make font-size scale properly using em
or rem
units.
Q8. Apply a media query to change layout on small viewports.
Q9. Create two versions of a page: one with and one without the viewport tag.
Q10. Combine viewport tag with responsive images and test on different devices.
Q1: What does the viewport meta tag control?
Q2: What is the purpose of width=device-width?
Q3: What does initial-scale=1.0 do?
Q4: Which tag should be placed inside <head> for mobile responsiveness?
Q5: What happens if you skip the viewport meta tag?
Q6: What does user-scalable=no mean?
Q7: Where is the viewport tag defined?
Q8: What does maximum-scale=1.0 do?
Q9: Which one is a correct viewport tag?
Q10: What is the best practice for mobile-friendly websites?