Clean β’ Professional
When building responsive websites, two things matter the most:
Letβs break this down step by step : -
By default, mobile browsers shrink webpages to fit a desktop width, which makes text too small and layouts broken.
To fix this, we use the viewport meta tag in the <head> section of HTML.
width=device-width β Matches the screen width of the device.initial-scale=1.0 β Ensures no zooming when the page first loads.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- Viewport meta tag -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Meta Viewport Example</title>
</head>
<body>
<h1>Hello, Responsive World!</h1>
<p>This page adapts to your device screen size.</p>
</body>
</html>
CSS provides different units for sizing elements. They can be grouped into two categories:
These units scale based on other values (like parent or root font size).
<h1>Relative Units Demo</h1>
<div class="parent">
Parent Text
<p class="child">Child Text</p>
</div>
html {
font-size: 16px; /* base font size */
}
.parent {
font-size: 20px; /* parent font size */
margin: 5%;
}
.child {
font-size: 1.5em; /* 1.5 Γ parent's font size = 30px */
margin: 2rem; /* 2 Γ root font size = 32px */
}
These units adjust based on the browser window (viewport) size.
<header>
<h1>Viewport Units Demo</h1>
</header>
<section class="hero">
<p>This section takes full screen height & width.</p>
</section>body {
margin: 0;
font-family: Arial, sans-serif;
}
header {
background: #0078d7;
color: white;
text-align: center;
padding: 2vh; /* 2% of screen height */
}
.hero {
background: #28a745;
color: white;
height: 100vh; /* 100% of viewport height */
width: 100vw; /* 100% of viewport width */
display: flex;
align-items: center;
justify-content: center;
font-size: 3vmin; /* Scales with smaller side */
}Β