Exploring Background Properties in CSS: Customizing Background Images, Colors, and More


Background properties in CSS allow you to customize and style the background of elements on a web page. They provide control over background images, colors, positioning, repetition, and more. Here's an explanation of the key background properties in CSS:

In these chapters, you will learn about the following CSS background properties:

  1. background-image: Specifies the background image to be displayed. It can be a URL pointing to an image file or a gradient created using the linear-gradient() or radial-gradient() functions.
  2. background-color: Sets the background color for an element. It can be specified using color names, hexadecimal values, RGB values, or HSL values.
  3. background-repeat: Determines how the background image is repeated if it is smaller than the element's size. Values include repeat (default), repeat-x, repeat-y, and no-repeat.
  4. background-position: Specifies the starting position of the background image. It can be set using keywords like left, center, right, top, bottom, or using percentage or length values.
  5. background-size: Sets the size of the background image. It can be specified using keywords like cover, contain, or using length values, such as px, em, or percentage values.
  6. background-attachment: Determines whether the background image scrolls with the content or remains fixed. Values include scroll (default) and fixed.
  7. background-origin: Defines the positioning area of the background image relative to the element's padding box, border box, or content box.
  8. background-clip: Specifies the area where the background image or color is visible. It can be set to border-box, padding-box, or content-box.
  9. background-blend-mode: Allows blending of the background image or color with the content behind it. It provides effects like transparency, color mixing, and more.
  10. background-gradient: Represents a CSS gradient that can be used as the background. Gradients can be linear or radial and can include multiple color stops.

Background Color


In CSS (Cascading Style Sheets), the background color property is used to define the background color of an element on a web page. It allows you to specify a color value or keyword that determines the background color of the content area of an element.

The background color property can be applied to various HTML elements, such as <body>, <div>, <p>, <span>, etc. Here's the basic syntax:

selector {
  background-color: value;
}

The selector is the HTML element or class/id selector to which you want to apply the background color. The value represents the desired color for the background.

Example
h1 {
  background-color: green;
}

div {
  background-color: lightblue;
}

p {
  background-color: yellow;
}

Background Image


To set a background image using CSS, you can use the background-image property. Here's an example of how you can add a background image to an element:

selector {
  background-image: url("path/filename");
}

In the example above, replace "path/filename" with the actual path to your image file.

You can also specify additional properties to control how the background image is displayed, such as background-repeat, background-size, and background-position. Here's an example that includes some of these properties:

Example
body {
  background-image: url("rose.png");
}

Feel free to adjust these properties according to your specific requirements.

Background Repeat


In CSS, you can control how a background image repeats using the background-repeat property. This property allows you to specify whether the background image should repeat horizontally, vertically, both, or not at all. Here are the possible values for the background-repeat property

By default, the background-image property repeats an image both horizontally and vertically.

Some images should be repeated only horizontally or vertically, or they will look strange, like this:

The background-repeat property accepts the following values:

  • repeat: This is the default value. It repeats the background image both horizontally and vertically to cover the entire background area. If the background image is smaller than the element, it will be repeated to fill the space.
  • repeat-x: The background image is repeated only horizontally. It will be displayed in a row, repeating horizontally to cover the width of the element. The image is not repeated vertically.
  • repeat-y: The background image is repeated only vertically. It will be displayed in a column, repeating vertically to cover the height of the element. The image is not repeated horizontally.
  • no-repeat: The background image is not repeated. It is displayed only once and will not be repeated in any direction. If the image is smaller than the element, it may be cropped or not fully displayed.
  • space: This value is similar to repeat, but it adds space between the repeated images. The space is distributed evenly between the repeated images, creating gaps between them. If there is extra space left after distributing it evenly, it will be added at the ends.
  • round: This value is also similar to repeat, but it resizes the repeated images to fit the background area without distorting them. It scales the images up or down proportionally so that they cover the entire background area. If the images cannot be resized evenly, they will be repeated with the remaining space distributed as evenly as possible.
Example
body {
  background-image: url("gradient_bg.png");
  background-repeat: repeat-x;
}

In the example, the background image specified by url("image.png") will repeat only horizontally within the <div> element. It will create a row of repeated images to cover the width of the element. By controlling the background-repeat property, you can achieve different effects with background images, such as seamless patterns, tiled backgrounds, or single instances of an image.

Background Position


In CSS, the background-position property is used to control the positioning of a background image within its containing element. It specifies where the background image should be placed relative to the element's padding box.

The background-position property accepts one or two values, which can be specified using keywords, percentages, or length values.

The background-position property is used to specify the position of the background image.

Here are the different ways to specify the background-position values:

Keywords

  • left: Positions the background image at the left side of the element.
  • center: Positions the background image at the horizontal and vertical center of the element.
  • right: Positions the background image at the right side of the element.
  • top: Positions the background image at the top of the element.
  • bottom: Positions the background image at the bottom of the element.

Percentages

  • The first value represents the horizontal position, where 0% is the left edge of the element and 100% is the right edge.
  • The second value represents the vertical position, where 0% is the top edge of the element and 100% is the bottom edge.

Length values

  • The first value represents the horizontal position, which can be specified using pixels (px), ems (em), or any other CSS length unit.
  • The second value represents the vertical position, also specified using CSS length unit

The background-position property can also be combined with the background-repeat property to control both the positioning and repetition of the background image.

It's worth noting that if only one value is provided for background-position, the second value is assumed to be 50%. This centers the background image vertically if not specified explicitly.

By adjusting the background-position property, you can precisely control where the background image appears within its container and create various visual effects.

Example
body {
  background-image: url("img_tree.png");
  background-repeat: no-repeat;
  background-position: right top;
}

Background Attachment


In CSS, the background-attachment property is used to control whether a background image scrolls with the content or remains fixed in place when the content is scrolled.

The background-attachment property specifies whether the background image should scroll or be fixed (will not scroll with the rest of the page):

Example
body {
  background-image: url("img_tree.png");
  background-attachment: fixed;
}

The background-attachment property accepts the following values:

  • scroll: This is the default value. The background image scrolls along with the content as the user scrolls the element. When the element is scrolled, the background image moves accordingly.
  • fixed: The background image remains fixed in its position within the element's viewport, regardless of scrolling. As the content is scrolled, the background image stays in place, creating a "fixed" effect. This can be useful for creating parallax effects or backgrounds that remain static while the content scrolls over them.
  • local: The background image scrolls with the element's contents but is fixed relative to the element itself. This means that the background image scrolls along with the content within the element, but it does not scroll out of the element's boundaries. This is useful when you want the background image to be fixed within a specific area of an element, such as a scrolling panel.
  • initial or inherit: These values are used to inherit the background-attachment property from the parent element or set it to its initial value.

By utilizing the background-attachment property, you can create different visual effects and control how background images behave in relation to scrolling content.

Background Origin


In CSS, the background-origin property is used to control the positioning of the background image relative to its containing element's padding box, border box, or content box.

The background-origin property accepts the following values:

  • padding-box: This is the default value. The background image is positioned relative to the padding box of the element. The background image starts from the padding edge of the element.
  • border-box: The background image is positioned relative to the border box of the element. The background image starts from the border edge of the element.
  • content-box: The background image is positioned relative to the content box of the element. The background image starts from the content edge of the element.

Here's an example that demonstrates the usage of background-origin:

Example
body {
  background-image: url("img_tree.png");
  background-origin: border-box;
}

By adjusting the background-origin property, you can control the positioning of the background image within its containing element and achieve different visual effects.

Background Clip


In CSS, the background-clip property is used to control how the background image or color is clipped or displayed relative to the element's padding box, border box, or content box.

The background-clip property accepts the following values:

  • border-box: This is the default value. The background extends underneath the element's border, including the padding area. The background is clipped to the border box.
  • padding-box: The background extends to the edges of the element's padding box, excluding the border. The background is clipped to the padding box.
  • content-box: The background is displayed only within the element's content box, excluding the padding and border. The background is clipped to the content box.
  • text: The background is clipped to the shape of the text content within the element. The background appears only where there is text, and it doesn't extend to the padding, border, or margins.

Here's an example that demonstrates the usage of background-clip:

Example
body {
  background-image: url("img_tree.png");
  background-clip: border-box;
}

By adjusting the background-clip property, you can control how the background image or color is displayed and clipped relative to the element's box model, achieving different visual effects.

Background Blend Mode


In CSS, the background-blend-mode property is used to specify how the background images and background color of an element should blend or interact with each other.

The background-blend-mode property accepts various blending modes, similar to the blending modes used in image editing software. These blending modes determine how the colors of the background images and background color are combined.

The background-blend-mode property can be applied to individual background layers or to all background layers combined.

Example
body {
  background-image: url("img_tree.png");
  background-color: #ff0000;
  background-blend-mode: multiply;
}

Here are some commonly used blending modes for the background-blend-mode property:

  1. normal: This is the default value. The background images and background color are displayed without any blending or interaction.
  2. multiply: The resulting color is a combination of the multiplied values of the background images and background color. It creates a darker and more saturated look.
  3. screen: The resulting color is a combination of the inverse of the multiplied values of the background images and background color. It creates a lighter and more vibrant look.
  4. overlay: The resulting color is a combination of the multiply and screen blending modes. It darkens the image shadows and brightens the highlights.
  5. soft-light: The resulting color is a combination of the multiply and screen blending modes with a softer effect. It creates a gentle light and shadow effect.
  6. hard-light: The resulting color is a combination of the multiply and screen blending modes with a more pronounced effect. It creates a more vivid light and shadow effect.
  7. difference: The resulting color is the absolute difference between the background images and background color. It creates a high-contrast effect.
  8. color-dodge: The resulting color is a combination of the background images and background color by increasing the brightness. It creates a brightening effect.
  9. color-burn: The resulting color is a combination of the background images and background color by increasing the darkness. It creates a darkening effect.

By utilizing different blending modes with the background-blend-mode property, you can achieve various creative effects and control how the background images and color interact with each other.


Source Code

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Tutor Joes</title>
    <link href="https://fonts.googleapis.com/css?family=Pacifico" rel="stylesheet">
    <style>
    html{
        background:white;
    }
    body{
        background: teal;
        background-image: url(jerry.png);
        color:white;
        background-repeat: no-repeat;
        background-size: 100px 100px;
        background-position: top right;
        height: 800PX;
        width: 800px;
    }
    h1{
        font-family: 'Pacifico', cursive;
        font-weight:normal;
        font-variant: small-caps;
        color:yellow;
    }
    p{
        font-style:italic;
        font-size: 25px;
    }
    span{
        font: bold 12pt Arial; 
    }
    </style>
</head>
<body>
    <h1>Background Properties</h1>
    <p>
      <span>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Qui, 
      placeat eius! Error deserunt eaque sed non, laboriosam quaerat veritatis
      </span>veniam possimus? Facilis corporis quae at alias esse optio sint 
      dignissimos.Lorem ipsum dolor sit, amet consectetur adipisicing elit. Qui, 
      placeat eius! Error deserunt eaque sed non, laboriosam quaerat veritatis
      veniam possimus? Facilis corporis quae at alias esse optio sint 
      dignissimos.
    </p>
    
</body>
</html>
To download raw file Click Here

These background properties can be combined to create visually appealing backgrounds for elements on your web page. By leveraging background images, colors, gradients and other settings, you can enhance the overall design and aesthetics of your website.

CSS Tutorial


Properties Reference


Cascading Style Sheet


Flexbox Tutorial


CSS Grid Tutorial


Transitions Properties


CSS Properties with Examples


CSS Selectors


CSS Pseudo Elements


CSS Attribute Selectors


Input Pseudo Classes


CSS Examples


CSS Animation Projects