← Back to HTML Course | Chapter 2: Text, Formatting & Media | Lesson 16 of 26

HTML Background Images

एक regular <img> tag page पर content के रूप में एक picture रखता है, जो किसी दूसरे element की तरह space के लिए text के साथ compete करता है। एक background image इसके बजाय किसी element के content के पीछे एक picture paint करती है, room में furniture के पीछे wallpaper जैसी -- content इसके ऊपर बैठता है, और image खुद पूरी तरह decorative है, page की actual information में कोई role नहीं निभाती।
Syntax
markup
<tagname style="background-image: url('image-url');">content</tagname>

एक बुनियादी Background Image सेट करना

background-image: url("path/to/image.jpg") किसी element के content के पीछे एक image paint करता है, जो एक सामान्य <img> src attribute जैसे ही relative या absolute path syntax का उपयोग करता है।

<img> tag के विपरीत, image खुद page के HTML content में कभी नहीं दिखती -- यह विशुद्ध रूप से एक CSS painting effect है।

Note: हमेशा ऐसी image चुनें जिसमें पर्याप्त plain area या low contrast हो जहां आपका text बैठेगा, ताकि foreground content readable रहे।
Warning: बिना explicit background-color fallback वाली एक background image अगर image file load होने में fail हो जाए तो सिर्फ खाली स्थान दिखाती है।

उदाहरण: Setting a Basic Background Image

markup
<style>
  .hero {
    background-image: url("photo.jpg");
    background-color: gray;
  }
</style>
<div class="hero">Hero content</div>

background-repeat से Tiling नियंत्रित करना

By default, अपने container से छोटी एक background image उपलब्ध जगह भरने के लिए horizontally और vertically दोनों तरह से repeat (tile) होती है। background-repeat: no-repeat image को सिर्फ एक बार दिखाता है, जबकि repeat-x या repeat-y tiling को एक direction तक सीमित कर देता है, जो किसी decorative strip pattern के लिए उपयोगी है।

Note: photographic hero images के लिए no-repeat का उपयोग करें, और असल tiling को छोटे, seamless pattern textures के लिए बचाकर रखें जो repeat होने के लिए ही design किए गए हों।
Warning: अगर container image से बड़ा हो तो default repeat setting पर छोड़ी गई एक बड़ी photo गलती से एक जटिल, unintended grid में tile हो सकती है।

उदाहरण: Controlling Tiling with background-repeat

markup
<style>
  .hero {
    background-image: url("photo.jpg");
    background-repeat: no-repeat;
  }
</style>
<div class="hero">Hero content</div>

background-position से Positioning

background-position नियंत्रित करता है कि image अपने container के अंदर कहां anchor होती है, center या top right जैसे keywords, या 20px 50% जैसे explicit values का उपयोग करते हुए।

यह तय करता है कि जब container पूरी image से छोटा हो, तो image का कौन सा हिस्सा सबसे पहले दिखेगा।

Note: जब आपको ठीक-ठीक पता न हो कि container कैसे crop होगा, तो background-position: center का उपयोग करें, क्योंकि यह image के सबसे महत्वपूर्ण हिस्से को visible रखता है।
Warning: अगर container छोटा और चौड़ा हो तो "bottom" जैसी एक background-position value किसी portrait-oriented photo के top को अप्रत्याशित रूप से crop कर सकती है।

उदाहरण: Positioning with background-position

markup
<style>
  .hero {
    background-image: url("photo.jpg");
    background-position: center;
  }
</style>
<div class="hero">Hero content</div>

background-size से Scaling

background-size नियंत्रित करता है कि image अपने container के relative कितनी बड़ी render होती है -- cover किसी भी overflow को crop करते हुए image को पूरे container में fill करने के लिए scale करता है, और contain बिना crop किए image को पूरी तरह container के अंदर fit करने के लिए scale करता है, जिससे उसके आसपास खाली जगह भी बच सकती है।

Note: full-bleed hero banners के लिए background-size: cover का उपयोग करें, और जब आपको गारंटी चाहिए कि पूरी image visible रहे, जैसे कोई logo, तब contain का उपयोग करें।
Warning: background-size: cover बहुत wide या बहुत narrow containers पर किसी image के महत्वपूर्ण हिस्सों (जैसे faces) को crop कर सकता है, इसलिए crop area को ध्यान से चुनें।

उदाहरण: Scaling with background-size

markup
<style>
  .hero {
    background-image: url("photo.jpg");
    background-size: cover;
  }
</style>
<div class="hero">Hero content</div>

कई Background Images को Layer करना

CSS url() values को commas से अलग करके एक ही element पर एक से अधिक background-image allow करता है -- सूची में सबसे पहली image सबसे ऊपर render होती है और बाद वाली उसके नीचे, जो किसी photo पर gradient overlay combine करने के लिए उपयोगी है ताकि overlaid text readable रहे।

Note: किसी photo background पर एक हल्का semi-transparent dark gradient layer करें ताकि photo के content की परवाह किए बिना light-colored text हमेशा readable रहे।
Warning: कई background images को layer करते समय, background-position और background-size values को सही layer पर apply करने के लिए matching comma-separated order में list किया जाना चाहिए।

उदाहरण: Layering Multiple Background Images

markup
<style>
  .hero {
    background-image: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)), url("photo.jpg");
  }
</style>
<div class="hero">Hero content</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. #}

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.