CSS How To
In this page:
Inline CSS
Inline CSS सीधे किसी element के style attribute के अंदर लिखा जाता है, जो सिर्फ उसी एक specific element पर लागू होता है, कहीं और नहीं। क्योंकि यह internal और external दोनों CSS को override कर देता है, inline styles को आमतौर पर quick tests या dynamically generated styles को छोड़कर हतोत्साहित किया जाता है।
उदाहरण: Inline CSS
<p style="color: red;">Styled with an inline style attribute</p>
Internal CSS
Internal CSS page के head में रखे गए एक style tag के अंदर लिखा जाता है। यह सिर्फ उसी एक HTML document पर लागू होता है, site के किसी और page पर नहीं। यह approach छोटे, single-page projects के लिए सुविधाजनक है लेकिन site के कई pages में बढ़ने पर maintain करना मुश्किल हो जाता है।
उदाहरण: Internal CSS
<head>
<style>
p { color: blue; }
</style>
</head>
<body>
<p>Blue text from the internal style block.</p>
</body>
External CSS
External CSS अपनी .css file में रहता है, किसी भी HTML page से पूरी तरह अलग, और इसे एक link tag का इस्तेमाल करके जोड़ा जाता है। जो भी page इसे link करता है वह बिल्कुल वही styles share करता है।
यह असली projects के लिए recommended approach है क्योंकि यह styling को पूरी site में centralized, cacheable, और reusable बनाए रखता है।
उदाहरण: External CSS
<link rel="stylesheet" href="styles.css">
<p>Styled by a separate .css file</p>
Methods के बीच Precedence का Order
जब एक ही element को एक से ज़्यादा method से style किया जाता है, तो inline CSS internal और external CSS पर जीतती है, और आमतौर पर बाद वाले rules पहले वाले rules पर जीतते हैं जब specificity बाकी सब बराबर हो।
उदाहरण: Order of Precedence Between Methods
<head>
<style>
p { color: blue; }
</style>
</head>
<body>
<p style="color: red;">Inline wins over the internal style above</p>
</body>
सही Method चुनना
उस method को चुनें जो बदलाव के scope से मेल खाता हो: एक one-off tweak के लिए inline, एक self-contained page के लिए internal, और कई pages में shared किसी भी चीज़ के लिए external।
उदाहरण: Choosing the Right Method
<!-- One-off tweak: inline -->
<p style="color: red;">One-off</p>
<!-- Self-contained page: internal -->
<style>p.local { color: blue; }</style>
<p class="local">Internal</p>
<!-- Shared across pages: external -->
<link rel="stylesheet" href="styles.css">
- पूरी site पर inline styles का ज़्यादा इस्तेमाल करना, जो consistent design और बाद के updates को कहीं ज़्यादा मुश्किल बना देता है।
- यह भूल जाना कि inline styles default रूप से internal और external CSS को override करती हैं, जिससे 'मेरी CSS क्यों काम नहीं कर रही' जैसे confusing moments आते हैं।
- गलत file path के साथ किसी stylesheet को link करना, जिससे external CSS कभी load ही नहीं होती।
- Inline CSS style attribute का इस्तेमाल करके सीधे किसी element पर लिखी जाती है।
- Internal CSS किसी एक page के head में style tag के अंदर रहती है और सिर्फ उस page को असर करती है।
- External CSS अपनी .css file में रहती है और पूरी site में share की जा सकती है।
CSS जोड़ने के तीनों तरीके (inline, internal, external) हर modern browser में एक जैसे supported हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: