The <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
line is an HTML head meta tag that ensures your web page appears correctly and compatibly on mobile devices. It is a cornerstone of responsive design.
Explanation:
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
Parameters:
-
width=device-width
: Sets the screen width to the actual pixel width of the device. -
initial-scale=1
: Displays the page at 100% zoom level (zoom 1) when it is first loaded. -
shrink-to-fit=no
: Prevents the content from being shrunk to fit the screen size. Commonly used in Safari.
Why is it Necessary?
-
Prevents display distortions on mobile devices.
-
Responsive CSS frameworks (e.g., Bootstrap) require this tag.
-
Since browsers' default width is usually 980px, mobile-compatible CSS will not work without this tag.
Example Usage with Mobile-Friendly CSS:
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="style.css">
</head>
body {
font-family: sans-serif;
margin: 0;
padding: 0;
}
.container {
width: 100%;
max-width: 1200px;
margin: 0 auto;
padding: 15px;
}
@media (max-width: 768px) {
.container {
padding: 10px;
}
.menu {
flex-direction: column;
}
}
Summary:
Without this meta tag, responsive CSS codes will not work effectively. It must be used between the <head>
tags to ensure that your page is rendered correctly on mobile devices. It should be included by default, especially in modern frontend development processes.