Backgrounds

CSS backgrounds define the background colors and images of HTML elements. They allow you to use different colors, gradients, or images behind the content. In this chapter, we will learn about various CSS background properties, including how to set background colors, apply images, adjust their size and position, control repetition, and more.

CSS Backgrounds Example

The following section shows an example of setting color, image, and gradient for an HTML element.

Background: None

Background: Color

Background: Gradient

Background: Image

Try Different Background Options

CSS Background Shorthand Property

The background shorthand property allows you to specify all background properties in a single declaration.

The correct order of properties when using the shorthand background property is as follows:

  • background-color
  • background-image
  • background-position
  • background-size (must be used with /)
  • background-repeat
  • background-origin
  • background-attachment
  • background-clip

Syntax

The syntax for the background property is as follows:

background: bg-color bg-image bg-position bg-size bg-repeat bg-origin bg-clip bg-attachment | initial | inherit;/* Example */background: green url('image.jpg') top/20% no-repeat border-box content-box fixed;

Note: If background-size is to be added, it must be included immediately after the background-position, separated with ‘/’. For example: “left/50%”.

Setting Background Color

You can set the background color for elements like div, span, body, paragraph, etc using the background-color property.

Example

In this example, we have used three different types of color values for body and div tags. For a complete reference of color in CSS, check out the CSS colors chapter.

<!DOCTYPE html><html><head><style>  
    body {
        background-color: lightgray;
    }
    div{
        padding: 25px;
    }
    .firstDiv{
        background-color: rgb(255, 215, 0);
    }
    .secondDiv{
        background-color: #f0f0f0;
    }
    .thirdDiv{
        background-color: hsl(120, 100%, 75%);
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;CSS Background Colors&lt;/h2&gt;
Body Color: lightgray; &lt;br&gt;&lt;br&gt;&lt;div class="firstDiv"&gt; Color: rgb(255, 215, 0) &lt;/div&gt;&lt;br&gt;&lt;div class="secondDiv"&gt; Color: #f0f0f0&lt;/div&gt;&lt;br&gt;&lt;div class="thirdDiv"&gt; Color: hsl(120, 100%, 75%)&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Setting Images in Background

To set an image as background for another element such as div, span, body, paragraph, etc., you can use the background-image property. It can be used to set one or more than one image as the background. To set multiple images as background, we separate the images using commas.

Example

In this example, we have set a background image for the body element.

<!DOCTYPE html><html lang="en"><head><style>
    div{
        background-color: rgba(255, 255, 255);
        opacity: 70%;
        padding: 20px;
    }
    body {
        background-image: url(/css/images/logo.png);
        height: 350px;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div&gt;&lt;h1&gt;Welcome to My Website&lt;/h1&gt;&lt;p&gt;
        This is an example of setting a 
        background image using CSS
    &lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Define Background Position

The background-position property sets the initial position of the element's background image. The position of the image is relative to the value set by the background-origin property.

Example

In this example, we have set the position of the background image to the right of the div element using the background-position property.

<!DOCTYPE html><html><head><style>  
    .position-right {
        background-image: url('/css/images/logo.png');
        background-position: right;
        background-repeat: no-repeat;
        width: 100%;
        height: 300px;
        border: 3px solid black;
        position: relative;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="position-right"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Setting Background Size

To set the size of the background image of an element, you can use the background-size property. The background image can either be stretched, constrained, or left to its normal size.

Example

In this example, we have set the size of the background image to 100% of the div element using the background-size property.

<!DOCTYPE html><html><head><style>
    .size-contain {
        background-image: url('/css/images/pink-flower.jpg');
        background-size: contain; 
        width: 300px;
        height: 300px;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;CSS background-size property&lt;/h2&gt;&lt;div class="size-contain"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Repeating Background Image

You can control the repetition of the background images using the background-repeat property. The image can be repeated along the horizontal and vertical axes, or not repeated.

Example

In this example, we have used the background-repeat property to repeat the background image horizontally and vertically.

<!DOCTYPE html><html><head><style>
    .repeat {
        background-image: url('/css/images/logo.png');
        background-repeat: repeat;
        width: 800px;
        height: 400px;
        position: relative;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt; CSS background-repeat property &lt;/h2&gt;&lt;div class="repeat"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Defining Background Origin

CSS background-origin property is used to set the origin of the background, which could be from the start of the border, inside the border, or inside the padding.

Example

In this example, we have used the background-origin property to set the origin of the background image to the content box of the div element.

<!DOCTYPE html><html><head><style>
    div {
        border: 10px rgb(13, 7, 190);
        border-style: dashed;
        margin: 5px;
        padding: 1cm;
        font: 700 1em sans-serif;
        color: aliceblue;
        display: inline-block;
        background-image: url('/css/images/yellow-flower.jpg');
        height: 200px;
        width: 200px;
        background-size: contain;
    }
    .content-box {
        background-origin: content-box;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="content-box"&gt;&lt;/div&gt;&lt;p&gt; 
    This image background start from content box of 
    div element.
&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Controlling Background Scrolling

You can use the background-attachment property to determine whether the position of the background image is fixed within the viewport or scrolls within its container.

Example

In this example, we have used the background-attachment property to set the background image to be fixed within the viewport.

<!DOCTYPE html><html><head><style>
    .fixed {
        background-image: url('images/logo.png');
        background-repeat: no-repeat;
        background-attachment: fixed;
        background-position: left top;
        background-color: lightblue;
        background-size: 40% 30%;
        padding: 5rem;
        width: 250px;
        height: 500px;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;CSS background-attachment Property&lt;/h2&gt;&lt;div class="fixed"&gt;&lt;p&gt;
        Lorem Ipsum is simply dummy text of the printing 
        and typesetting industry. Lorem Ipsum has been the 
        industry's standard dummy text ever since the 1500s, 
        when an unknown printer took a galley of type and 
        scrambled it to make a type specimen book. 
        It has survived not only five centuries, but also 
        the leap into electronic typesetting, remaining 
        essentially unchanged. It was popularized in the 
        1960s with the release of Letraset sheets containing 
        Lorem Ipsum passages, and more recently with desktop 
        publishing software like Aldus PageMaker including 
        versions of Lorem Ipsum.
    &lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Controlling Background Display

You can use CSS background-clip property to specify how the background image or color should be displayed within an element's padding box, border box, or content box. It determines the area of an element to which the background will be applied.

Example

In this example, we have used the background-clip property to apply the background to the content and padding area of the div element.

<!DOCTYPE html><html><head><style>
    p {
        border: 10px dotted black;
        padding: 15px;
        background: green;
        color: white;
    }
    .border-area {
        background-clip: border-box;
    }
    .padding-area {
        background-clip: padding-box;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;CSS background-clip property&lt;/h2&gt;&lt;p class="border-area"&gt;
    Background applied to the entire element.
&lt;/p&gt;&lt;p class="padding-area"&gt;
    Background applied to the content &amp; padding area.
&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

CSS Background Properties

All the properties related to background are listed in the table below:

PropertyDescriptionExample
backgroundShorthand for background-related properties.Try It
background-attachmentSpecifies the position of the background relative to the viewport, either fixed or scrollable.Try It
background-clipControls how far a background image extends beyond the element's padding or content box.Try It
background-colorSets the background color of an element.Try It
background-imageSets one or more background image(s) on an element.Try It
background-originSets the origin of the background.Try It
background-positionSets the initial position of each image in a background.Try It
background-position-xSets the initial horizontal position of each image in a background.Try It
background-position-ySets the initial vertical position of each image in a background.Try It
background-repeatControls the repetition of an image in the background.Try It
background-sizeControls the size of the background image.Try It
background-blend-modeDetermines how an element's background images blend with each other.Try It

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *