How to Add an Image & Background Image in HTML

To add an image and background image in HTML, you can use the <img> and background-image properties, respectively. Here are the steps to do so:


Adding an Image:

To add an image to your HTML page, use the <img> tag and set the src attribute to the file path or URL of the image you want to display. Here's an example:

Code:

<img src="image.jpg" alt="Image description">

In this example, image.jpg is the file path of the image, and alt is a description of the image that's used for accessibility purposes.


Adding a Background Image:

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

Code:

<style>

  .bg-image {

    background-image: url('background.jpg');

    background-size: cover;

    background-position: center;

  }

</style>


<div class="bg-image">

  <!-- content here -->

</div>

In this example, we're using the background-image property to set the URL of the background image, and the background-size and background-position properties to control the size and position of the image.


Note that it's generally best to use CSS to style background images, rather than adding them directly to the HTML markup. This allows for greater flexibility and easier maintenance. Also, make sure that the images you use are optimized for the web, to ensure fast page load times.

Previous Post Next Post