← Back to HTML Course | Chapter 3: Page Structure | Lesson 9 of 12

HTML Responsive

सोचिए आप अपनी दीवार पर लगाने के लिए एक खूबसूरत poster डिज़ाइन कर रहे हैं। यह बिल्कुल perfect दिखता है। लेकिन अब आपसे कहा जाता है कि उसी poster को सिकोड़कर wrist watch के face पर fit करें, और साथ ही उसे बड़ा करके highway billboard पर भी fit करें। Responsive design इसी design problem को हल करती है। यह ऐसे web pages बनाने की कला है जो हर आकार की screens पर—widescreen desktop monitors से लेकर compact mobile phone displays तक—अपने-आप scale, shrink, और expand होकर खूबसूरत दिखें। Responsive design के बिना, mobile users को आपके page पर एक single sentence पढ़ने के लिए भी zoom in, pinch, और horizontally slide करना पड़ेगा।
Syntax
markup
<meta name="viewport" content="width=device-width, initial-scale=1.0">

Viewport Meta Tag

Mobile browsers बड़े desktop layouts को छोटी screens पर फिट करने के लिए अपने-आप zoom out कर देते हैं, जिससे text बहुत छोटा और पढ़ने में मुश्किल हो जाता है।

viewport meta tag mobile devices को page layout को device के native scale पर render करने का निर्देश देता है, जिससे पढ़ने का size आरामदायक बना रहता है।

Note:
  • आप जो भी webpage design करें, उसके head section में हमेशा viewport meta tag शामिल करें।
  • Accepted attributes:
  • name — viewport
  • content — width=device-width | initial-scale=1 | minimum-scale | maximum-scale | user-scalable=yes|no
Warning: यह viewport configuration tag शामिल न करने से mobile devices आपकी site को एक छोटी, zoomed-out desktop screen की तरह display करेंगे।

उदाहरण: The Viewport Meta Tag

markup
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

Fluid Container Widths

container widths को fixed pixels में define करने के बजाय (जो छोटी mobile screens पर overflow हो सकती हैं), fluid layouts relative percentage values इस्तेमाल करते हैं। इससे आपके columns और boxes किसी भी screen size में फिट होने के लिए dynamically shrink और expand हो सकते हैं।

Note: बड़ी screens पर content को बहुत ज्यादा फैलने से रोकने के लिए percentage width के साथ max-width सेटिंग भी इस्तेमाल करें।
Warning: बड़े containers पर fixed pixel widths सेट करने से बचें, ताकि छोटी screens पर layout cutoff और horizontal scrolling की समस्याओं से बचा जा सके।

उदाहरण: Fluid Container Widths

markup
<div style="width: 80%; max-width: 1000px;">
  Fluid container
</div>

Scalable Responsive Images

बड़ी images mobile screens पर display के किनारे से बाहर निकलकर आसानी से आपका page layout बिगाड़ सकती हैं। CSS का इस्तेमाल करके relative widths और height controls लगाने से, आप अपनी images को किसी भी device size में फिट होने के लिए अपने-आप scale down करा सकते हैं।

Note: अपनी images को साफ तरीके से scale कराने और proportional बनाए रखने के लिए max-width: 100% और height: auto वाले style rules इस्तेमाल करें।
Warning: images पर fixed pixel width attributes सेट करने से बचें, जब तक कि आप न चाहें कि वे सभी screen dimensions पर एक static size में रहें।

उदाहरण: Scalable Responsive Images

markup
<img src="photo.jpg" style="max-width: 100%; height: auto;" alt="Photo">

Fluid Relative Typography

Fixed pixel font sizes (जैसे font-size: 16px) अलग-अलग screens के हिसाब से adjust नहीं होते, जिससे text पढ़ना मुश्किल हो सकता है।

Relative CSS units (जैसे em, rem, या vw) इस्तेमाल करने से आपके text sizes user के browser या screen dimensions के सापेक्ष dynamically scale हो पाते हैं।

Note: सभी screen sizes पर consistent, accessible text scaling के लिए rem units इस्तेमाल करें।
Warning: font sizes के लिए viewport width units (vw) का उपयोग करते समय सावधान रहें, क्योंकि mobile devices पर text बहुत छोटा हो सकता है।

उदाहरण: Fluid Relative Typography

markup
<p style="font-size: 1rem;">Text that scales with the root font size</p>

CSS Media Queries का परिचय

HTML आपके content को structure करता है, लेकिन पूरी तरह responsive designs बनाने के लिए CSS media queries ही असली काम करते हैं। ये आपके कोड में conditional statements की तरह काम करते हैं, जिससे आप user की screen width के आधार पर खास layout rules लागू कर पाते हैं।

Note: पहले mobile screen dimensions के लिए अपनी website layouts design करें, फिर बड़ी screens के लिए उन्हें adapt करने के लिए media queries इस्तेमाल करें।
Warning: सुनिश्चित करें कि आपके media query break points सही तरीके से सेट हों, ताकि tablets जैसी medium-sized screens पर layouts टूटने से बचें।

उदाहरण: Introduction to CSS Media Queries

markup
<style>
  @media (max-width: 600px) {
    body {
      font-size: 14px;
    }
  }
</style>

CSS Breakpoints समझना

एक breakpoint एक खास screen width होती है जहाँ आपका layout उपलब्ध space में बेहतर फिट होने के लिए बदल जाता है, जिसे एक media query से define किया जाता है।

आम breakpoints लगभग phone, tablet, और desktop sizes को target करते हैं, हालांकि सही numbers इस बात पर आधारित होने चाहिए कि आपका खुद का design असल में कहाँ बिगड़ना शुरू होता है, किसी मनमाने device width पर नहीं।

Note: किसी खास phone model की exact pixel values कॉपी करने के बजाय, breakpoints उस जगह के आधार पर design करें जहाँ आपका content स्वाभाविक रूप से अटपटे तरीके से wrap होता है।
Warning: हर possible device size के लिए एक unique breakpoint बनाने की कोशिश करना messy, maintain करने में मुश्किल CSS की ओर ले जाता है — लगभग किसी भी site के लिए कुछ अच्छी तरह चुने गए breakpoints ही काफी होते हैं।

उदाहरण: CSS Breakpoints Explained

markup
<style>
  @media (min-width: 768px) {
    .container {
      width: 750px;
    }
  }
</style>
<div class="container">Resize the viewport to see the width change at 768px</div>

Responsive Tables Technique

कई columns वाली चौड़ी tables को mobile-friendly बनाना सबसे मुश्किल कामों में से एक है, क्योंकि हर column को छोटा करने से text पढ़ने लायक नहीं रह जाता।

एक आम technique यह है कि table को एक scrollable container में wrap कर दिया जाए ताकि users साइड में swipe कर सकें, या CSS का इस्तेमाल करके छोटी screens पर हर row को card layout में restack किया जाए।

Note: किसी table को overflow-x: auto वाले div में wrap करना सबसे आसान fix है — इससे table पूरी तरह बरकरार रहती है और संकरी screens पर horizontal scrolling की सुविधा मिलती है।
Warning: सिर्फ font sizes घटाकर किसी wide table को mobile पर fit करने के लिए force करना content को पढ़ना मुश्किल या नामुमकिन बना देता है।

उदाहरण: Responsive Tables Technique

markup
<div style="overflow-x: auto;">
  <table>
    <tr><td>Wide table content</td></tr>
  </table>
</div>

Mobile-First Approach

Mobile-first का मतलब है पहले अपना base CSS छोटी screens के लिए लिखना, और फिर उल्टा करने के बजाय, screen बड़ी होने पर complexity जोड़ने के लिए min-width media queries इस्तेमाल करना।

यह approach आमतौर पर सरल, तेज़ी से load होने वाला CSS बनाता है, क्योंकि phones — सबसे सीमित devices — बिना किसी overrides के वही styles पाते हैं जो सबसे ज़रूरी हैं।

Note: हर stylesheet की शुरुआत सरल, single-column mobile styles से करें, फिर min-width queries का इस्तेमाल करते हुए tablets और desktops के लिए layout को धीरे-धीरे बेहतर बनाएं।
Warning: desktop-first approach, जो चीज़ों को सिकोड़ने के लिए max-width queries का उपयोग करता है, अक्सर ज्यादा overridden CSS और heavier stylesheets पैदा करता है, जिन्हें phones को फिर भी download करना पड़ता है।

उदाहरण: Mobile-First Approach

markup
<style>
  .box { width: 100%; }
  @media (min-width: 768px) {
    .box { width: 50%; }
  }
</style>
<div class="box">Full width on mobile, half width from 768px up</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.