Loading...
Please wait.

Join LetsLearnLights for expert coding and programming guidance. Study popular languages like Java, Python and JavaScript. Improve your skills today

CSS Background Property: Understanding Related Properties and Examples

Description:

The CSS background property is a shorthand property for setting the background-color, background-image, background-repeat, background-attachment, and background-position properties of an element all at once.

Headlines.

  • The CSS background property is used to set the background of an HTML element.
  • It can be used to set the color, image, or gradient as the background of an element.
  • The background-color property sets the background color of an element.
  • The background-image property sets an image as the background of an element.
  • The background-repeat property controls whether the background image should repeat or not.
  • The background-position property sets the position of the background image.
  • The background-attachment property controls whether the background image is fixed or scrolls with the page.
  • The background shorthand property allows you to set multiple background properties in one line of code.
  • The background property can also be used in conjunction with CSS gradients to create dynamic backgrounds.

Advertise...

Explain in Details.

Set All properties in one Property background.


<style>
    #testing-bg1 {
        background: #f0f0f0 url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png") no-repeat center fixed;
        height: 100px;
        font-size: large;
    }
</style>
<div id="testing-bg1">
    This div is for testing...
</div>

This div is for testing...
In this example, the background color of the body element is set to #f0f0f0, the background image is set to "https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png", the image is not repeated (no-repeat), the image is positioned in the center, and the image is fixed in the viewport.

Set All background properties individually.

  • background-color: sets the background color of an element, which can be a color name, hex value, or RGB value.
  • background-image: sets the background image of an element, which can be a URL to an image file.
  • background-repeat: sets if and how the background image should repeat. Possible values are "repeat", "repeat-x", "repeat-y", and "no-repeat".
  • background-attachment: sets if and how the background image should scroll with the page. Possible values are "scroll" and "fixed".
  • background-position: sets the starting position of the background image. Possible values are a combination of "left", "center", "right", "top", "bottom", or length or percentage values.

<style>
    #testing-bg2 {
        background-color: #f0f0f0;
        background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
        background-repeat: no-repeat;
        background-attachment: fixed;
        background-position: center;
        height: 100px;
        font-size: large;
    }
</style>
<div id="testing-bg2">
    This div is for testing...
</div>

This div is for testing...
This example sets the same background as the shorthand example above but with individual properties.

Other Properties:

background-clip

  • The background-clip CSS property specifies the area within which the background image or color of an element will be painted. It has four possible values:

    • border-box: The background is painted within the element's border box. This is the default value.

    <style>
        #case1 {
            background-color: red;
            background-clip: border-box;
            padding: 20px;
            border: 10px solid black;
        }
    </style>
    <div id="case1">
        This div is for testing...
    </div>

    This div is for testing...
    • padding-box: The background is painted within the element's padding box.

      <style>
          #case2 {
              background-color: red;
              background-clip: padding-box;
              padding: 20px;
              border: 10px solid black;
          }
      </style>
      <div id="case2">
          This div is for testing...
      </div>

      This div is for testing...
      • content-box: The background is painted within the element's content box.

      <style>
          #case3 {
              background-color: red;
              background-clip: content-box;
              padding: 20px;
              border: 10px solid black;
          }
      </style>
      <div id="case3">
          This div is for testing...
      </div>

      This div is for testing...
      • text: The background is painted within the element's text.
      <style>
          p#case4 {
              background-color: red;
              background-clip: text;
              padding: 20px;
              border: 10px solid black;
          }
      </style>
      <p id="case4">
          This paragraph is for testing...
      </p>

      This paragraph is for testing...

      In this example, the background color will be painted within the text of the p element and not the p element. Please note that text value is not widely supported across all browsers.

      background-origin
      The background-origin CSS property sets the background positioning area for an element.
      This property has 3 possible values:
      • padding-box: This value sets the background positioning area to be the padding box of the element. With this value, the background image will be positioned within the padding area of the element, and will not extend beyond the border of the element.

      <style>
          #case5 {
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-origin: padding-box;
              background-repeat: no-repeat;
              background-position: center;
              padding: 20px;
          }
      </style>
      <div id="case5">
          This div is for testing...
      </div>


      This div is for testing...
      In this example, the background image will be positioned at the center of the padding box, which is 20 pixels away from the edges of the element.
      • border-box: This value sets the background positioning area to be the border box of the element. With this value, the background image will be positioned within the area of the element that includes the content, padding, and border, and will extend to the edge of the border.

      <style>
          #case6 {
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-origin: border-box;
              background-repeat: no-repeat;
              background-position: center;
              border: 5px solid black;
          }
      </style>
      <div id="case6">
          This div is for testing...
      </div>


      This div is for testing...
      In this example, the background image will be positioned at the center of the border box, which includes the content, padding, and border, and will extend to the edge of the border.
      • content-box: This value sets the background positioning area to be the content box of the element. With this value, the background image will be positioned within the area of the element that includes only the content and will not extend beyond it.

      <style>
          #case7 {
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-origin: content-box;
              background-repeat: no-repeat;
              background-position: center;
              padding: 20px;
              border: 5px solid black;
          }
      </style>
      <div id="case7">
          This div is for testing...
      </div>


      This div is for testing...
      In this example, the background image will be positioned at the center of the content box, which is 20 pixels away from the edges of the element and does not include the border.

      It's worth noting that the default value for background-origin is padding-box if background-clip is not set, otherwise it's border-box.

      background-size

      The CSS background-size property sets the size of a background image. It has several possible values:

      • length: sets the width and height of the image in pixels or other units. Example: background-size: 50px 100px;
      • percentage: sets the width and height of the image as a percentage of the containing element. Example: background-size: 50% 75%;
      • cover: scales the image to be as large as possible while still covering the entire background area.
      • contain: scales the image to be as small as possible while still being entirely visible.
      length value allows you to set the width and height of the background image in pixels or other units. The value can be set as one or two values, separated by a space. If only one value is provided, it sets the width and height to the same value. For example, background-size: 50px; sets the width and height to 50px, while background-size: 50px 100px; sets the width to 50px and the height to 100px.

      <style>
          /* sets the width and height of the background image to 50px */
          .example1 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-size: 50px;
          }

          /* sets the width of the background image to 50px and the height to 100px */
          .example2 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-size: 50px 100px;
          }
      </style>
      <div class="example1">
          This div is for testing...
      </div>
      <br>
      <div class="example2">
          This div is for testing...
      </div>


      This div is for testing...

      This div is for testing...

      percentage value allows you to set the width and height of the background image as a percentage of the containing element. Like the length value, the percentage value can be set as one or two values, separated by a space. If only one value is provided, it sets the width and height to the same value. For example, background-size: 50%; sets the width and height to 50% of the containing element, while background-size: 50% 75%; sets the width to 50% and the height to 75% of the containing element.

      <style>
          /* sets the width and height of the background image to 50% of the containing element */
          .example3 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-size: 50%;
          }

          /* sets the width of the background image to 50% of the containing element and the height to 75% of the containing element */
          .example4 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-size: 50% 75%;
          }
      </style>
      <div class="example3">
          This div is for testing...
      </div>
      <br>
      <div class="example4">
          This div is for testing...
      </div>


      This div is for testing...

      This div is for testing...
      cover value scales the background image to be as large as possible while still covering the entire background area. This means that the image may be cropped in order to fill the background area completely. This value is often used for full-screen background images.

      <style>
          /* scales the background image to be as large as possible while still covering the entire background area */
          .example5 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-size: cover;
          }
      </style>
      <div class="example5">
          This div is for testing...
      </div>


      This div is for testing...
      contain value scales the background image to be as small as possible while still being entirely visible. This means that there may be empty space in the background area, but the entire image will be visible. This value is often used for background images that should be fully visible but should not take up the entire background area.

      <style>
          /* scales the background image to be as small as possible while still being entirely visible */
          .example6 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-size: contain;
          }
      </style>
      <div class="example6">
          This div is for testing...
      </div>


      This div is for testing...

      background-blend-mode

      background-blend-mode is a CSS property that defines how an element's background image or color will blend with the background of the element's parent. The property accepts several values, including:
      • normal: This is the default value and it means no blending; the background image or color will be displayed as is.

      <style>
          .blendex1 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: normal;
          }
      </style>
      <div class="blendex1">
          This div is for testing...
      </div>


      This div is for testing...
      • multiply: The background image or color will be multiplied with the parent background, resulting in a darker color. This effect can be useful for creating shadows or darkening an image.

      <style>
          .blendex2 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: multiply;
          }
      </style>
      <div class="blendex2">
          This div is for testing...
      </div>


      This div is for testing...
      • screen: The background image or color will be lightened, as if it were projected onto the parent background. This effect can be useful for creating highlights or brightening an image.

      <style>
          .blendex4 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: screen;
          }
      </style>
      <div class="blendex4">
          This div is for testing...
      </div>


      This div is for testing...
      • overlay: The background image or color will be blended with the parent background, with the effect depending on the parent background's darkness. If the parent background is light, the image will be darkened, and if the parent background is dark, the image will be lightened.

      <style>
          .blendex3 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: overlay;
          }
      </style>
      <div class="blendex3">
          This div is for testing...
      </div>


      This div is for testing...
      • darken: The background image or color will be blended with the parent background, but only the darkest colors will be visible. This can be useful for creating a darkening effect on an image.

      <style>
          .blendex5 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: darken;
          }
      </style>
      <div class="blendex5">
          This div is for testing...
      </div>


      This div is for testing...
      • lighten: The background image or color will be blended with the parent background, but only the lightest colors will be visible. This can be useful for creating a lightening effect on an image.

      <style>
          .blendex6 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: lighten;
          }
      </style>
      <div class="blendex6">
          This div is for testing...
      </div>


      This div is for testing...
      • color-dodge: The background image or color will brighten the parent background. This can be useful for creating a dodge effect on an image.

      <style>
          .blendex7 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: color-dodge;
          }
      </style>
      <div class="blendex7">
          This div is for testing...
      </div>


      This div is for testing...
      • color-burn: The background image or color will darken the parent background. This can be useful for creating a burn effect on an image.

      <style>
          .blendex8 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: color-burn;
          }
      </style>
      <div class="blendex8">
          This div is for testing...
      </div>


      This div is for testing...
      • hard-light: The background image or color will be blended with the parent background, with the effect depending on the darkness of the background image or color. If the image is light, it will lighten the parent background, and if the image is dark, it will darken the parent background.

      <style>
          .blendex9 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: hard-light;
          }
      </style>
      <div class="blendex9">
          This div is for testing...
      </div>


      This div is for testing...
      • soft-light: The background image or color will be blended with the parent background, with the effect depending on the darkness of the background image or color, but in a more subtle way than hard-light. This can be useful for creating a softening effect on an image.

      <style>
          .blendex10 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: soft-light;
          }
      </style>
      <div class="blendex10">
          This div is for testing...
      </div>


      This div is for testing...
      • difference: The background image or color will be blended with the parent background, with the result being the absolute difference between the two. This can be useful for creating a difference effect on an image.

      <style>
          .blendex11 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: difference;
          }
      </style>
      <div class="blendex11">
          This div is for testing...
      </div>


      This div is for testing...
      • exclusion: The background image or color will be blended with the parent background, with the result being similar to the difference blend mode, but with less contrast.

      <style>
          .blendex12 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: exclusion;
          }
      </style>
      <div class="blendex12">
          This div is for testing...
      </div>


      This div is for testing...
      • hue: The background image or color's hue will be blended with the parent background, while the parent background's saturation and luminosity will be preserved.

      <style>
          .blendex13 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: hue;
          }
      </style>
      <div class="blendex13">
          This div is for testing...
      </div>


      This div is for testing...
      • saturation: The background image or color's saturation will be blended with the parent background, while the parent background's hue and luminosity will be preserved.

      <style>
          .blendex14 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: saturation;
          }
      </style>
      <div class="blendex14">
          This div is for testing...
      </div>


      This div is for testing...
      • color: The background image or color's hue and saturation will be blended with the parent background, while the parent background's luminosity will be preserved.

      <style>
          .blendex15 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: color;
          }
      </style>
      <div class="blendex15">
          This div is for testing...
      </div>


      This div is for testing...
      • luminosity: The background image or color's luminosity will be blended with the parent background, while the parent background's hue and saturation will be preserved.

      <style>
          .blendex16 {
              height: 200px;
              background-image: url("https://cdn.pixabay.com/photo/2020/04/03/06/35/work-4997565_960_720.png");
              background-blend-mode: luminosity;
          }
      </style>
      <div class="blendex16">
          This div is for testing...
      </div>


      This div is for testing...

      Final Lines.

      CSS background properties are widely supported by most browsers, but there may be some variations or limitations in the way different browsers interpret and display certain properties. For example, some older browsers may not support certain CSS3 properties such as background-size or background-clip. Additionally, certain properties may be implemented differently across different browsers, leading to slight variations in the way backgrounds are displayed. To ensure that your backgrounds are displayed consistently across all browsers, it is important to use cross-browser compatible CSS and to test your designs in multiple browsers.
      The CSS background property is a powerful tool for creating visually stunning and engaging web pages. It allows developers to set the background color, image, and position of elements on a web page, giving them the ability to control the overall look and feel of their website. Whether you are looking to create a simple background color or an intricate background image, the CSS background property provides the flexibility and customization you need to create a visually appealing website that stands out from the crowd.

      Posted by Mnzr chauhan
      ««« Thanks again for reading our blog. If you have any questions, feel free to ask. Your support is enough for us to share this post with your friends. And a good comment for our hard work. »»»
      Thank you ツ

      Post a Comment

      0 Comments