Apply Background Styles in CSS

CSSCSSBeginner
Practice Now

Introduction

In this lab, you will learn how to apply advanced background styles using CSS, focusing on enhancing web page design through color and image manipulation. The lab covers essential techniques such as setting background colors for HTML elements, adding and sizing background images, controlling image positioning and repetition, and combining multiple background properties to create visually appealing web layouts.

Through practical, hands-on examples, you will explore different methods of specifying background colors using named colors, hexadecimal codes, and RGB functions, as well as learn how to incorporate background images with precise sizing and positioning techniques. By the end of this lab, you will have gained practical skills in using CSS background properties to create more dynamic and engaging web page designs.

Set Background Color for HTML Elements

In this step, you'll learn how to set background colors for HTML elements using CSS. Background colors are a fundamental way to enhance the visual design of web pages by adding color to different elements.

First, let's create an HTML file to demonstrate background color styling. Open the WebIDE and create a new file named styles.html in the ~/project directory.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Background Color Example</title>
    <style>
      /* We'll add our CSS here */
    </style>
  </head>
  <body>
    <div class="box1">First Box</div>
    <div class="box2">Second Box</div>
    <p class="paragraph">This is a paragraph with a background color.</p>
  </body>
</html>

Now, let's add some CSS to set background colors for different elements. Update the <style> section in the HTML file:

.box1 {
  background-color: blue;
  color: white;
  padding: 20px;
  margin: 10px;
}

.box2 {
  background-color: #ff5733;
  color: white;
  padding: 20px;
  margin: 10px;
}

.paragraph {
  background-color: rgb(200, 230, 255);
  padding: 15px;
}

In this example, we've demonstrated three ways to specify background colors:

  1. Named color (blue)
  2. Hexadecimal color code (#FF5733)
  3. RGB color function (rgb(200, 230, 255))

Save the file and open it in a web browser to see the different background colors applied to the elements.

Add Background Image with Sizing

In this step, you'll learn how to add background images to HTML elements and control their sizing using CSS. We'll continue building on the previous example by modifying the styles.html file in the ~/project directory.

First, you'll need to download a sample image. Use the following command in the terminal:

wget https://labex.io/courses/images/background-sample.jpg -O ~/project/background-sample.jpg

Now, update the styles.html file with the following content:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Background Image Sizing</title>
    <style>
      .image-container {
        width: 500px;
        height: 300px;
        border: 2px solid black;
        margin: 20px;
      }

      .cover-background {
        background-image: url("background-sample.jpg");
        background-size: cover;
      }

      .contain-background {
        background-image: url("background-sample.jpg");
        background-size: contain;
      }

      .custom-size-background {
        background-image: url("background-sample.jpg");
        background-size: 200px 150px;
      }
    </style>
  </head>
  <body>
    <div class="image-container cover-background">
      Cover: Fills entire container
    </div>
    <div class="image-container contain-background">
      Contain: Fits entire image
    </div>
    <div class="image-container custom-size-background">
      Custom Size: 200x150 pixels
    </div>
  </body>
</html>

This example demonstrates three different ways to size background images:

  1. background-size: cover; - Scales the image to cover the entire container
  2. background-size: contain; - Scales the image to fit entirely within the container
  3. background-size: 200px 150px; - Sets a specific pixel size for the background image

When you open this HTML file in a browser, you'll see three different background image sizing techniques.

Position Background Image

In this step, you'll learn how to control the positioning of background images using CSS. We'll continue modifying the styles.html file in the ~/project directory to demonstrate different background positioning techniques.

Update the styles.html file with the following content:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Background Image Positioning</title>
    <style>
      .image-container {
        width: 500px;
        height: 300px;
        border: 2px solid black;
        margin: 20px;
        color: white;
        font-weight: bold;
      }

      .top-left {
        background-image: url("background-sample.jpg");
        background-size: cover;
        background-position: top left;
      }

      .center {
        background-image: url("background-sample.jpg");
        background-size: cover;
        background-position: center;
      }

      .bottom-right {
        background-image: url("background-sample.jpg");
        background-size: cover;
        background-position: bottom right;
      }

      .custom-position {
        background-image: url("background-sample.jpg");
        background-size: cover;
        background-position: 20% 80%;
      }
    </style>
  </head>
  <body>
    <div class="image-container top-left">Top Left Position</div>
    <div class="image-container center">Center Position</div>
    <div class="image-container bottom-right">Bottom Right Position</div>
    <div class="image-container custom-position">Custom Position (20% 80%)</div>
  </body>
</html>

This example demonstrates four different ways to position background images:

  1. background-position: top left; - Positions the image at the top-left corner
  2. background-position: center; - Centers the image within the container
  3. background-position: bottom right; - Positions the image at the bottom-right corner
  4. background-position: 20% 80%; - Uses percentage values for custom positioning

When you open this HTML file in a browser, you'll see how different positioning values affect the background image placement.

Control Background Image Repetition

In this step, you'll learn how to control background image repetition using the background-repeat CSS property. We'll continue modifying the styles.html file in the ~/project directory to demonstrate different repetition techniques.

Update the styles.html file with the following content:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Background Image Repetition</title>
    <style>
      .image-container {
        width: 500px;
        height: 300px;
        border: 2px solid black;
        margin: 20px;
        color: white;
        font-weight: bold;
      }

      .repeat {
        background-image: url("background-sample.jpg");
        background-size: 100px 100px;
        background-repeat: repeat;
      }

      .repeat-x {
        background-image: url("background-sample.jpg");
        background-size: 100px 100px;
        background-repeat: repeat-x;
      }

      .repeat-y {
        background-image: url("background-sample.jpg");
        background-size: 100px 100px;
        background-repeat: repeat-y;
      }

      .no-repeat {
        background-image: url("background-sample.jpg");
        background-size: 200px 200px;
        background-repeat: no-repeat;
      }
    </style>
  </head>
  <body>
    <div class="image-container repeat">Repeat (Default)</div>
    <div class="image-container repeat-x">Repeat X (Horizontal)</div>
    <div class="image-container repeat-y">Repeat Y (Vertical)</div>
    <div class="image-container no-repeat">No Repeat</div>
  </body>
</html>

This example demonstrates four different background image repetition techniques:

  1. background-repeat: repeat; - Repeats the image both horizontally and vertically (default)
  2. background-repeat: repeat-x; - Repeats the image only horizontally
  3. background-repeat: repeat-y; - Repeats the image only vertically
  4. background-repeat: no-repeat; - Prevents the image from repeating

When you open this HTML file in a browser, you'll see how different repetition values affect the background image display.

Combine Multiple Background Properties

In this step, you'll learn how to combine multiple background properties to create more complex and interesting background designs. We'll modify the styles.html file in the ~/project directory to demonstrate advanced background styling techniques.

Update the styles.html file with the following content:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Multiple Background Properties</title>
    <style>
      .image-container {
        width: 600px;
        height: 400px;
        border: 2px solid black;
        margin: 20px;
        color: white;
        font-weight: bold;
        padding: 20px;
      }

      .multiple-backgrounds {
        background-image:
          linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)),
          url("background-sample.jpg");
        background-size: cover, cover;
        background-position: center, center;
        background-repeat: no-repeat, no-repeat;
      }

      .layered-backgrounds {
        background-image:
          url("small-pattern.png"), linear-gradient(to right, #ff6b6b, #4ecdc4);
        background-size:
          100px 100px,
          cover;
        background-position:
          top left,
          center;
        background-repeat: repeat, no-repeat;
      }
    </style>
  </head>
  <body>
    <div class="image-container multiple-backgrounds">
      Overlay Background with Image
    </div>
    <div class="image-container layered-backgrounds">
      Layered Background with Pattern and Gradient
    </div>
  </body>
</html>

First, download a small pattern image:

wget https://labex.io/courses/images/small-pattern.png -O ~/project/small-pattern.png

This example demonstrates two advanced background techniques:

  1. Multiple Backgrounds with Overlay:

    • Uses a linear gradient to create a semi-transparent overlay
    • Combines the gradient with a background image
    • Applies consistent sizing, positioning, and repetition
  2. Layered Backgrounds:

    • Combines a repeating pattern image with a linear gradient
    • Uses different sizing and positioning for each background layer

When you open this HTML file in a browser, you'll see how multiple background properties can create complex and visually interesting designs.

Summary

In this lab, participants learned how to apply background styles in CSS through a comprehensive, step-by-step approach. The lab covered essential techniques for enhancing web page design, including setting background colors using different methods such as named colors, hexadecimal codes, and RGB functions, and exploring background image manipulation.

The practical exercises demonstrated how to add background images to HTML elements, control their sizing, positioning, and repetition, and combine multiple background properties. By working through hands-on examples, learners gained practical skills in using CSS to create visually appealing and dynamic web page backgrounds with various styling techniques.