Pattern Background
Using repeating patterns as background
Learn how to set an image as the background of a webpage using the background attribute and CSS background-image property.
To create an HTML webpage that demonstrates how to use an image file as the background of a webpage using the background attribute and CSS background-image property.
There are two ways to set an image as the background of a webpage:
background attribute in the <body> tag (deprecated but still works).background-image property (recommended modern method).background attribute in the <body> tag is deprecated in HTML5. Use CSS for modern web development. However, both methods are covered here for learning purposes.
background-size, background-repeat, background-position, and background-attachment.
Using repeating patterns as background
CSS gradients can also be used as backgrounds
This simulates a city image background
url() or background attribute matches your actual image filename.| Property | Purpose | Example |
|---|---|---|
background-image |
Sets the background image | url('image.jpg') |
background-size |
Controls the size of the background image | cover, contain, 100% |
background-repeat |
Controls if the image repeats | no-repeat, repeat-x, repeat-y |
background-position |
Sets the position of the image | center, top right, 50% 50% |
background-attachment |
Controls if the background scrolls | fixed, scroll |
background |
Shorthand for all background properties | url('img.jpg') no-repeat center cover |
| Value | Description | Effect |
|---|---|---|
cover |
Scales the image to cover the entire element | Image may be cropped, but fills the whole area |
contain |
Scales the image to fit within the element | Entire image is visible, may leave empty space |
auto |
Default, image displays at original size | May repeat if smaller than element |
| Percentage | Scales to percentage of parent | 100% 100% stretches to fill |
| Pixel values | Fixed size in pixels | 300px 200px |
| Mistake | Why It's Wrong | Correct Way |
|---|---|---|
| Wrong file path | Image won't load | Check the path and filename |
| Image too large | Slow page load | Optimize images for web |
| Poor text readability | Text hard to read on image | Add overlay or use contrasting text |
| Using background attribute in HTML5 | Deprecated | Use CSS background-image |
Missing url() in CSS |
Image won't load | Use url('image.jpg') |
background attribute in the <body> tag is deprecated in HTML5.background-size, background-repeat, and background-position.background-size: cover fills the entire page without distorting the image.background-attachment: fixed creates a parallax-like effect.You have successfully:
background-image, background-size, background-repeat, and background-attachment.background-size: cover for full-page backgrounds and add overlays for better text readability.
background-blend-mode for creative effects. Experiment with multiple backgrounds using CSS. Learn about responsive backgrounds that work on all screen sizes. Try using gradients as an alternative to images!
ایک HTML ویب صفحہ بنانا جو بیک گراؤنڈ کے طور پر تصویری فائل کو استعمال کرنے کا طریقہ ظاہر کرتا ہے background صفت اور CSS background-image پراپرٹی کا استعمال کرتے ہوئے۔
ویب صفحے کا بیک گراؤنڈ تصویر سیٹ کرنے کے دو طریقے ہیں:
<body> ٹیگ میں background صفت کا استعمال (متروک لیکن اب بھی کام کرتا ہے)۔background-image پراپرٹی کا استعمال (تجویز کردہ جدید طریقہ)۔<body> ٹیگ میں background صفت HTML5 میں متروک ہے۔ جدید ویب ڈیولپمنٹ کے لیے CSS استعمال کریں۔ تاہم، سیکھنے کے مقاصد کے لیے دونوں طریقوں کا احاطہ کیا گیا ہے۔
background-size، background-repeat، background-position، اور background-attachment جیسی پراپرٹیز کے ساتھ زیادہ کنٹرول دیتا ہے۔
بیک گراؤنڈ کے طور پر دہرائے جانے والے پیٹرن کا استعمال
CSS گریڈینٹس بھی بیک گراؤنڈ کے طور پر استعمال کیے جا سکتے ہیں
یہ شہر کی تصویر کے بیک گراؤنڈ کی نقل کرتا ہے
url() یا background صفت میں فائل کا نام آپ کی اصل تصویری فائل کے نام سے مماثل ہے۔| پراپرٹی | مقصد | مثال |
|---|---|---|
background-image |
بیک گراؤنڈ امیج سیٹ کرتا ہے | url('image.jpg') |
background-size |
بیک گراؤنڈ امیج کے سائز کو کنٹرول کرتا ہے | cover، contain، 100% |
background-repeat |
کنٹرول کرتا ہے کہ امیج دہرائی جائے یا نہیں | no-repeat، repeat-x، repeat-y |
background-position |
امیج کی پوزیشن سیٹ کرتا ہے | center، top right، 50% 50% |
background-attachment |
کنٹرول کرتا ہے کہ بیک گراؤنڈ سکرول کرتا ہے یا نہیں | fixed، scroll |
background |
تمام بیک گراؤنڈ پراپرٹیز کے لیے شارٹ ہینڈ | url('img.jpg') no-repeat center cover |
| قدر | وضاحت | اثر |
|---|---|---|
cover |
پورے عنصر کو ڈھانپنے کے لیے تصویر کو پیمانہ کرتا ہے | تصویر کٹ سکتی ہے، لیکن پورے علاقے کو بھرتی ہے |
contain |
عنصر کے اندر فٹ ہونے کے لیے تصویر کو پیمانہ کرتا ہے | پوری تصویر نظر آتی ہے، خالی جگہ چھوڑ سکتی ہے |
auto |
ڈیفالٹ، تصویر اصل سائز میں ظاہر ہوتی ہے | اگر عنصر سے چھوٹی ہو تو دہرائی جا سکتی ہے |
| فیصد | والدین کے فیصد کے مطابق پیمانہ کرتا ہے | 100% 100% بھرنے کے لیے کھینچتا ہے |
| پکسل اقدار | پکسلز میں مقررہ سائز | 300px 200px |
| غلطی | یہ کیوں غلط ہے | صحیح طریقہ |
|---|---|---|
| غلط فائل پاتھ | تصویر لوڈ نہیں ہوگی | پاتھ اور فائل کا نام چیک کریں |
| تصویر بہت بڑی | صفحہ لوڈ سست | ویب کے لیے تصاویر کو بہتر بنائیں |
| ناقص ٹیکسٹ پڑھنے کی اہلیت | ٹیکسٹ کو تصویر پر پڑھنا مشکل | اوورلے شامل کریں یا متضاد ٹیکسٹ استعمال کریں |
| HTML5 میں background صفت کا استعمال | متروک | CSS background-image استعمال کریں |
CSS میں url() غائب |
تصویر لوڈ نہیں ہوگی | url('image.jpg') استعمال کریں |
<body> ٹیگ میں background صفت HTML5 میں متروک ہے۔background-size، background-repeat، اور background-position جیسی پراپرٹیز کے ساتھ زیادہ کنٹرول فراہم کرتا ہے۔background-size: cover تصویر کو بغیر بگاڑے پورے صفحے کو بھرتا ہے۔background-attachment: fixed پیرالیکس جیسا اثر پیدا کرتا ہے۔آپ نے کامیابی سے:
background-image، background-size، background-repeat، اور background-attachment جیسی CSS پراپرٹیز کا استعمال کیا۔background-size: cover استعمال کریں اور بہتر ٹیکسٹ پڑھنے کی اہلیت کے لیے اوورلے شامل کریں۔
background-blend-mode جیسی مختلف بیک گراؤنڈ پراپرٹیز استعمال کرنے کی کوشش کریں۔ CSS کا استعمال کرتے ہوئے متعدد بیک گراؤنڈز کے ساتھ تجربہ کریں۔ ذمہ دار بیک گراؤنڈز کے بارے میں سیکھیں جو تمام اسکرین سائز پر کام کرتے ہیں۔ تصاویر کے متبادل کے طور پر گریڈینٹس استعمال کرنے کی کوشش کریں!