β’ **Absolute URL:** Points to an external website (e.g., `background="https://www.example.com/images/bg.gif"`)
β’ **Relative URL:** Points to a file within the same website (e.g., `background="images/bg.gif"`) | --- ## Browser Support All major web browsers historically supported the `background` attribute for backwards compatibility: * Google Chrome * Mozilla Firefox * Internet Explorer / Microsoft Edge * Safari * Opera --- ## Code Examples ### Legacy HTML Example (Deprecated) The following example demonstrates how the `background` attribute was traditionally used to apply a background image to a webpage. ```html
Hello World!
This page uses the legacy HTML background attribute.
``` ### Modern CSS Example (Recommended) Because the `background` attribute is obsolete, you should use the CSS `background-image` property instead. This can be applied via inline styles, an internal stylesheet, or an external stylesheet. #### Using Inline CSS: ```htmlHello World!
This page uses modern inline CSS to set the background image.
``` #### Using External/Internal CSS (Best Practice): ```htmlHello World!
This page uses an internal CSS stylesheet for cleaner, more maintainable code.
``` --- ## Compatibility & Best Practices * **HTML5 Compatibility:** The `background` attribute is completely removed from the HTML5 specification. Pages using this attribute may not validate correctly. * **Separation of Concerns:** HTML should only be used to define the structure of a webpage, while CSS should handle all visual presentation and styling. * **Enhanced Control with CSS:** Using CSS allows you to control additional background behaviors that the HTML attribute cannot handle, such as: * `background-repeat`: Controls whether the image tiles (repeats). * `background-size`: Scales the background image (e.g., `cover` or `contain`). * `background-position`: Aligns the image to the center, top, bottom, left, or right. * `background-attachment`: Determines if the background scrolls with the page or remains fixed.
YouTip