background properties - CSS: Cascading Style Sheets

Introduction to CSS Backgrounds: Here's How To Use It “Beginner's Guide”

CSS Backgrounds

{tocify} $title={Table of Contents}

introduction to CSS Background Properties

CSS background properties are a group of CSS properties that allow you to style the background of an element, such as a page, a section, or a div. 

These properties can be used to set a background color, add a background image, control how an image is repeated, position an image, resize an image, and more.

The background properties in CSS are versatile and can be used to create a variety of design effects, from simple solid colors to complex patterns and images. 

By using these properties, you can customize the background of your elements, add visual interest to your website, and create unique designs that help your website stand out.

Some of the most commonly used background properties in CSS include background-color, background-image, background-repeat, background-position, and background-size.

These properties can be combined and adjusted to create a wide range of effects and styles.

Overall, understanding and using CSS background properties is an essential skill for web designers and developers who want to create visually appealing and engaging websites.

There are several background properties in CSS that you can use to create beautiful designs. In this post, we'll explore some of the most commonly used CSS background properties and how to use them.

Here is a list of all the CSS background properties:

  1. background-color
  2. background-image
  3. background-repeat
  4. background-position
  5. background-size
  6. background-origin
  7. background-clip
  8. background-attachment
  9. background-blend-mode
  10. backdrop-filter

Let’s understand each CSS background properties line by line.

1. CSS: background-color

CSS “background-color” property is used to set the background color of an element. It takes a color value as its parameter, and can be set to any valid CSS color value, including named colors, hexadecimal values, RGB or RGBA values, HSL or HSLA values, and more.

Here is an example of how to set the background color of an element to red:

<!DOCTYPE html>
<html>
<head>
<title>Background Color Example</title>

<style>
body {
background-color: red;
}
</style>
</head>
<body>
<h1>Welcome to my website!</h1>

<p>This is an example of how to set the background color of the body element using CSS.</p>

</body>
</html>{codeBox}

Here's another example of how to set the background color to a hexadecimal value:

background-color: #F1F1F1;{codeBox}

You can also use the “rgba()” function to set the background color with an alpha channel for transparency. The following code will set the background color to a semi-transparent blue:

background-color: rgba(0, 0, 255, 0.5);{codeBox}

In addition, the background-color property can be used with other background properties such as background-image, background-repeat, background-position, and background-size to create complex background effects.

2. CSS: background-image

The background-image property in CSS is used to set an image as the background of an HTML element. 

It takes a URL value as its parameter and can be set to any valid image URL. Multiple images can be specified as a comma-separated list, and the browser will display the first image it can load.

Examples:

a. Set background image to a local image file:

background-image: url("background.jpg");{codeBox}

b. Set background image to a remote image file:

background-image: url("https://example.com/images/background.jpg");{codeBox}

c. Set multiple background images:

background-image: url("background1.jpg"), url("background2.jpg");{codeBox}

d. Set a gradient as the background image:

background-image: linear-gradient(to bottom, #ffffff, #cccccc);{codeBox}

In this example, a linear gradient is used as the background image. The “to bottom” parameter specifies the direction of the gradient, and the color stops “#ffffff” and “#cccccc” defines the colors used in the gradient.

use CSS: background-image with HTML

<!DOCTYPE html>
<html>
<head>
<title>Background Image Example</title>
          <style>
body {
background-image: url("background.jpg");
background-repeat: no-repeat;
background-size: cover;
}
</style>

</head>
<body>

<h1>Welcome to my website!</h1>

<p>This is an example of how to set the background image of the body element using CSS.</p>

</body>
</html>{codeBox}

In this example, the “background-image” property is applied to the “body” element using CSS. The value url("background.jpg") sets the background image to a local image file named "background.jpg". 

The “background-repeat” property is used to prevent the image from repeating, and the “background-size” property is used to ensure that the image covers the entire background.

When you open this HTML file in a web browser, you will see that the background of the entire page is set to the specified image. You can replace the URL with the URL of any valid image file to set the background image to the image of your choice.

3. CSS: background-repeat

The background-repeat property in CSS is used to specify whether a background image should repeat horizontally, vertically, or not at all. 

The default value is repeat, which means that the background image will repeat both horizontally and vertically.

Syntax:

background-repeat: repeat | repeat-x | repeat-y | no-repeat | initial | inherit;{codeBox}

  • repeat: the background image will repeat both horizontally and vertically
  • repeat-x: the background image will repeat only horizontally
  • repeat-y: the background image will repeat only vertically
  • no-repeat: the background image will not repeat at all
  • initial: sets the property to its default value
  • inherit: inherits the property from its parent element

Example:

<!DOCTYPE html>
<html>
<head>
<title>Background Repeat Example</title>
    
         <style>
body {
background-image: url("background.jpg");
background-repeat: no-repeat;
background-size: cover;
}
</style>
</head>
<body>

<h1>Welcome to my website!</h1>
<p>This is an example of how to use the background-repeat property in CSS.</p>

</body>
</html>{codeBox}

In this example, the “background-repeat” property is used to prevent the background image from repeating. The value “no-repeat” ensures that the image is displayed only once, rather than being tiled across the background. 

The “background-size” property is used to ensure that the image covers the entire background.

You can experiment with different values for the “background-repeat” property to see how they affect the appearance of the background image. 

For example, setting it to “repeat-x” will repeat the image horizontally, while setting it to “repeat-y” will repeat it vertically.

4. CSS: background-position

The background-position property in CSS is used to specify the starting position of the background image. It defines the location where the top left corner of the background image should be placed relative to the top left corner of the element's padding box.

Syntax:

background-position: x-axis | y-axis;{codeBox}

The “x-axis” and “y-axis” values can be expressed in different units such as pixels, percentages, or keywords such as left, center, or right for the horizontal position, and top, center, or bottom for the vertical position.

Example:

<!DOCTYPE html>
<html>
<head>
<title>Background Position Example</title>

<style>
body {
background-image: url("background.jpg");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
</style>

</head>
<body>
            <h1>Welcome to my website!</h1>
<p>This is an example of how to use the background-position property in CSS.</p>

</body>
</html>{codeBox}

In this example, the “background-position” property is used to center the background image both horizontally and vertically. The “center” keyword specifies the x-axis and y-axis values for the background position.

You can experiment with different values for the “background-position” property to see how they affect the placement of the background image. 

For example, setting it to “left top” will align the top left corner of the image with the top left corner of the element, while setting it to “right bottom” will align the bottom right corner of the image with the bottom right corner of the element.

5. CSS: background-size

The “background-size” property in CSS is used to specify the size of the background image. 

It defines the width and height of the background image as a percentage of the element's width and height or as an absolute value in pixels.

Syntax:

background-size: width & height | cover | contain | initial | inherit;{codeBox}

  • width & height - Specifies the width and height of the background image in pixels or percentages.
  • cover - The background image is resized to cover the entire element, while maintaining its aspect ratio.
  • contain - The background image is resized to fit inside the element, while maintaining its aspect ratio.
  • initial - Sets the property to its default value.
  • inherit - Inherits the property from its parent element.

Example:

<!DOCTYPE html>
<html>
<head>
<title>Background Size Example</title>

<style>
body {
background-image: url("background.jpg");
background-repeat: no-repeat;
background-size: cover;
}
</style>

</head>
<body>

<h1>Welcome to my website!</h1>
<p>This is an example of how to use the background-size property in CSS.</p>

</body>
</html>{codeBox}

In this example, the “background-size” property is used to make the background image cover the entire background while maintaining its aspect ratio. 

The “cover” keyword ensures that the entire background area is filled with the image, even if it means that some parts of the image will be cropped.

You can experiment with different values for the “background-size” property to see how they affect the size and appearance of the background image. 

For example, setting it to “contain” will resize the image to fit inside the element without cropping any parts of the image.

6. CSS: background-origin

The background-origin property in CSS is used to specify the positioning area of the background image.

It defines the background positioning area relative to the padding box, border box, or content box of an element.

Syntax:

background-origin: padding-box | border-box | content-box | initial | inherit;{codeBox}

  • padding-box - The background image is positioned relative to the padding box of the element. This is the default value.
  • border-box - The background image is positioned relative to the border box of the element.
  • content-box - The background image is positioned relative to the content box of the element.
  • initial - Sets the property to its default value.
  • inherit - Inherits the property from its parent element.

Example:

<!DOCTYPE html>
<html>
<head>
<title>Background Origin Example</title>

<style>
body {
background-image: url("background.jpg");
background-repeat: no-repeat;
background-size: cover;
background-origin: content-box;
padding: 50px;
border: 10px solid black;
width: 500px;
height: 500px;
}
</style>

</head>
<body>
 
           <h1>Welcome to my website!</h1>
<p>This is an example of how to use the background-origin property in CSS.</p>

</body>
</html>{codeBox}

In this example, the “background-origin” property is used to position the background image relative to the content box of the element. The “content-box” value ensures that the background image is positioned relative to the content area of the element, excluding any padding or borders.

You can experiment with different values for the background-origin property to see how they affect the positioning of the background image. 

For example, setting it to “border-box” will position the background image relative to the border box of the element, including any padding but excluding the margin.

7. CSS: background-clip

The CSS background-clip property determines the area of the element's background that will be painted. 

It takes the following values:

  • border-box: The background will be painted within the border box of the element.
  • padding-box: The background will be painted within the padding box of the element.
  • content-box: The background will be painted within the content box of the element.

Here are some examples:

Example 1:

div {
  background-image: url('example.jpg');
  background-clip: border-box;
}{codeBox}

In this example, the background image will be painted within the border box of the “div” element.

Example 2:

div {
  background-image: url('example.jpg');
  background-clip: padding-box;
}{codeBox}

In this example, the background image will be painted within the padding box of the “div” element.

Example 3:

div {
  background-image: url('example.jpg');
  background-clip: padding-box;
}{codeBox}

In this example, the background image will be painted within the padding box of the “div” element.

8. CSS: background-attachment

The CSS background-attachment property specifies whether the background image is fixed or scrolls along with the content when the user scrolls. 

It has the following values:

  • scroll: This value specifies that the background image scrolls along with the content when the user scrolls the page. This is the default value.
  • fixed: This value specifies that the background image is fixed with respect to the viewport and doesn't move when the user scrolls the page.
  • local: This value specifies that the background image is fixed with respect to the element's content box and doesn't move when the user scrolls the content.

Here are some examples of using the background-attachment property:

Example 1: Background image scrolls with content

div {
  background-image: url('image.jpg');
  background-attachment: scroll;
}{codeBox}

In Example 1, the background image scrolls along with the content when the user scrolls the page.

Example 2: Background image fixed to viewport

div {
  background-image: url('image.jpg');
  background-attachment: fixed;
}{codeBox}

In Example 2, the background image is fixed with respect to the viewport and doesn't move when the user scrolls the page. 

This creates a parallax effect, where the background image appears to move at a slower rate than the content.

Example 3: Background image fixed to content box

div {
  background-image: url('image.jpg');
  background-attachment: local;
}{codeBox}

In Example 3, the background image is fixed with respect to the element's content box and doesn't move when the user scrolls the content. 

This creates a fixed background effect within the element's content area.

9. CSS: background-blend-mode

The CSS background-blend-mode property specifies how the background image should blend with the background color or other background images. 

It has the following values:

  • normal: This value specifies that the background image is displayed as-is, without any blending.
  • multiply: This value multiplies the colors of the background image with the colors of the background underneath, creating a darker overall appearance.
  • screen: This value combines the colors of the background image and the background underneath, creating a lighter overall appearance.
  • overlay: This value combines the background image and the background underneath, while preserving the highlights and shadows of the image.
  • darken: This value keeps the darker pixels of the background image and the background underneath, creating a darker overall appearance.
  • lighten: This value keeps the lighter pixels of the background image and the background underneath, creating a lighter overall appearance.

Here are some examples of using the background-blend-mode property:

Example 1: Blending two images using multiply

div {
  background-image: url('image1.jpg'), url('image2.jpg');
  background-blend-mode: multiply;
}{codeBox}

In Example 1, the two background images are blended using the multiply blend mode, creating a darker overall appearance. 

Example 2: Overlaying a color on top of an image

div {
  background-image: url('image.jpg');
  background-color: #ff0000;
  background-blend-mode: overlay;
}{codeBox}

In Example 2, the red color is overlaid on top of the background image using the overlay blend mode, while preserving the highlights and shadows of the image. 

Example 3: Darkening the background using a color

div {
  background-color: #000000;
  background-blend-mode: darken;
}{codeBox}

In Example 3, the black color is darkened using the darken blend mode, creating an even darker background color.

10. CSS: backdrop-filter

The CSS backdrop-filter property applies a visual effect to the background of an element, which can be used to blur or tint the background, or apply other visual effects. 

It is similar to the “filter” property, but instead of applying the filter to the element itself, it applies the filter to the area behind the element, creating a "backdrop".

Here are some examples of using the backdrop-filter property:

Example 1: Blurring the background

div {
  backdrop-filter: blur(5px);
}{codeBox}

In Example 1, the backdrop-filter property is used to apply a blur effect to the background of the element, with a blur radius of 5 pixels. This creates a blurred effect behind the element.

Example 2: Tinting the background

div {
  backdrop-filter: hue-rotate(90deg);
}{codeBox}

In Example 2, the backdrop-filter property is used to apply a hue-rotate effect to the background of the element, which rotates the hue of the background color by 90 degrees. This creates a tinted effect behind the element.

Example 3: Applying multiple filters to the background

div {
  backdrop-filter: blur(5px) brightness(0.5);
}{codeBox}

In Example 3, the backdrop-filter property is used to apply multiple filters to the background of the element, including a blur effect with a radius of 5 pixels and a brightness effect with a value of 0.5. This creates a blurred and darkened effect behind the element.

Note: that the “backdrop-filter property” is not supported by all browsers, and may have limited support on older browsers.{alertError}

Conclusion:

In summary, there are eight commonly used CSS background properties that can be used to style the background of an HTML element. 

These properties include: 

  • background-color: sets the background color of an element.
  • background-image: sets the background image of an element.
  • background-repeat: specifies how the background image should be repeated.
  • background-position: sets the position of the background image within the element.
  • background-attachment: specifies whether the background image should scroll with the rest of the page or remain fixed.
  • background-size: specifies the size of the background image.
  • background-origin: specifies the position of the background image relative to the padding, border, or content box of the element.
  • background-clip: specifies how far the background should extend within the element (i.e. within the padding, border, or content box).

By using these properties, web designers and developers can create visually appealing backgrounds for their web pages.

Small Note:

Friends, according to my expertise, I have written complete information to help you with “CSS Backgrounds” If this post is favourable for you or not, please tell me by commenting.

If you liked this post, do not forget to share it with your friends so they can get information about it.

You can ask us through comments if you still have questions or doubts, I will answer all your questions, and you can contact us for more information.

Please tell us through the comment section if you think we miss anything.

To be published, comments must be reviewed by the administrator.*

Previous Post Next Post