Skip to main content

HTML Background Images

A background image can be set on almost any HTML element.


Background Image on an HTML element

To put a background image on an element, use the style attribute with the CSS background-image property. You can also set it in a <style> element in the <head>.

Inline

A background image set inline on a paragraph.

Prompt
A page with a paragraph that has a background image set inline with the style attribute and the CSS background-image property (img_girl.jpg), plus some text inside it.
Output: complete HTML page

In the style element

A background image set for all paragraphs in the <style> element.

Prompt
A page that sets a background image on all paragraphs using a style block in the head (background-image url img_girl.jpg), with a sample paragraph.
Output: complete HTML page

Background Image on a Page

To give the whole page a background image, set background-image on the <body> element.

Example

A background image for the entire page.

Prompt
A page with a background image on the whole page, set on the body element via a style block (img_girl.jpg).
Output: complete HTML page

Background Repeat

If the background image is smaller than the element, it repeats across and down until the element is filled. To stop this, set the background-repeat property to no-repeat.

Repeating

A small background image repeating to fill the page.

Prompt
A page with a small background image on the body that repeats to fill the page (default behaviour).
Output: complete HTML page

No repeat

The same image set not to repeat.

Prompt
A page with a background image on the body that does not repeat, using background-repeat: no-repeat.
Output: complete HTML page

Background Cover

To make the background image cover the whole element, set background-size to cover. Set background-attachment to fixed so the element stays covered. The image keeps its proportions and is not stretched.

Example

A background image covering the whole page.

Prompt
A page with a fixed background image that covers the whole element without stretching, using background-size: cover, background-attachment: fixed and no-repeat.
Output: complete HTML page

Background Stretch

To make the background image stretch to fill the whole element, set background-size to 100% 100%. Resizing the window stretches the image, but it always covers the element.

Example

A background image stretched to fill the page.

Prompt
A page with a fixed background image stretched to fill the whole element using background-size: 100% 100% and no-repeat.
Output: complete HTML page

Learn More CSS

These examples show that background images are styled with the CSS background properties. To learn more, study the PromptCookbook CSS Background tutorial.