How To Add Background Image in CSS

To add a background image in CSS, you can use the "background-image" property. Here's an example of how to do it:

The background-image property is a CSS property used to set the background image of an element. It allows you to specify an image that will be displayed as the background of an HTML element, such as a <div> or the entire <body> of a webpage.

Here's the basic syntax for the background-image property:


selector {
  background-image: url("image_url");
}

The selector represents the HTML element or elements to which you want to apply the background image. The url("image_url") is the path or URL to the image file you want to use as the background.

Method 1: Inline CSS

<div style="background-image: url('path/image.jpg');">
  <!-- Content Goes Here -->
</div>

See the Pen How To Add Background Image in CSS by W3 Code Labs (@w3codelabs) on CodePen.

In this approach, you add the style attribute directly to the HTML element you want to apply the background image to. Make sure to replace 'path/image.jpg' with the actual path to your image file.

Method 2: External CSS

<div class="section-background">
  <h1>Title Goes Here</h1>
  <p>Content Goes Here</p>
</div>

.section-background {
  background-image: url(https://cdn.pixabay.com/photo/2016/06/08/15/45/lemon-1444025_1280.jpg);
  background-postion: center center;
  background-size: conver;
  background-repeat: no-repeat;
}

See the Pen How to add background image Using External CSS by W3 Code Labs (@w3codelabs) on CodePen.

In this approach, you define a CSS class in an external CSS file or within a <style> tag in the <head> section of your HTML document. Then, you apply the class to the desired HTML element. Again, replace 'path/image.jpg' with the actual path to your image file.

It's important to note that the background-image property is often used in combination with other related properties, such as background-repeat, background-position, and background-size, to control how the background image is displayed, repeated, positioned, and sized within the element.

Here's an example that sets a background image to cover the entire body of a webpage and centers it:


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

See the Pen sets a background image to cover the entire body of a webpage and centers it by W3 Code Labs (@w3codelabs) on CodePen.

In this example, the background-image property sets the image to be used as the background, background-repeat prevents the image from repeating, background-position centers the image horizontally and vertically, and background-size ensures the image covers the entire body of the webpage while maintaining its aspect ratio.

The background-image property provides a flexible way to enhance the visual appearance of elements on a webpage by adding images as backgrounds.

Remember to provide the correct path to the image file relative to the HTML or CSS file's location.