Lesson 20 of 29
In Progress

Using Images in CSS

In CSS, you can use images to add visual interest and context to your web design. You can use images as backgrounds, icons, and other design elements in your web pages. In this chapter, we’ll explore the different ways you can use images in CSS and how to optimize them for the web.

Adding Images as Backgrounds

One of the most common ways to use images in CSS is as backgrounds. You can use images as backgrounds for elements such as the body, headers, and sections to add visual interest and context to your web pages.

To add an image as a background, you can use the background-image property and specify the URL of the image file. You can also use the background-size, background-position, and background-repeat properties to control the size, position, and repeat behavior of the background image.

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

Adding Images as Inline Elements

In addition to using images as backgrounds, you can also use images as inline elements in your HTML content. To add an image as an inline element, you can use the <img> tag and specify the URL of the image file using the src attribute. You can also use the alt attribute to provide a text alternative for the image for users who are unable to see the image.

<img src="/path/to/image.jpg" 
alt="A description of the image">

Using Images as Icons

In CSS, you can also use images as icons to add visual interest and context to your web pages. To use an image as an icon, you can add the image as a background to an element and use the background-size and background-position properties to control the size and position of the icon.

.icon {
  width: 32px;
  height: 32px;
  background-image: url(/path/to/icon.png);
  background-size: cover;
  background-position: center;
}

Optimizing Images for the Web

To ensure that your images load quickly and effectively on the web, it’s important to optimize them for the web. Here are a few tips for optimizing images for the web:

  • Use appropriate image formats: Different image formats have different capabilities and file sizes. To ensure that your images are optimized for the web, use appropriate image formats such as JPEG, PNG, or GIF depending on your needs. For example, use JPEG for photographs, PNG for graphics with transparent backgrounds, and GIF for simple animations.
  • Compress images: To reduce the file size of your images, you can use image compression tools such as Adobe Photoshop or online tools such as TinyPNG or Kraken.io to compress your images without losing quality.
  • Use responsive images: To ensure that your images look and function properly on different devices and screen sizes, use responsive images that adapt to the size and resolution of the user’s device. You can use the srcset and sizes attributes to specify multiple versions of an image and let the browser choose the most appropriate version based on the user’s device.
<img src="/path/to/image.jpg" 
srcset="/path/to/image-small.jpg 300w, 
/path/to/image-medium.jpg 600w, 
/path/to/image-large.jpg 900w"
sizes="(max-width: 400px) 300px, 
(max-width: 800px) 600px, 900px" 
alt="A description of the image">

Using Sprites for Icons

To reduce the number of HTTP requests and improve the performance of your website, you can use sprites to combine multiple icons into a single image file. To use sprites, you can create a single image file that contains all of your icons and use the `background-position` property to position the icon in the sprite.

.icon {
  width: 32px;
  height: 32px;
  background-image: url(/path/to/sprite.png);
  background-size: cover;
  background-position: 0 0;
}

.icon-2 {
  width: 32px;
  height: 32px;
  background-image: url(/path/to/sprite.png);
  background-size: cover;
  background-position: -32px 0;
}

Conclusion

In conclusion, images are an important part of any web design. By using images as backgrounds, inline elements, and icons, and by optimizing them for the web, you can add visual interest and context to your web pages and improve their performance.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

Write a CSS rule to add an image as the background of an element with the ID “header” and set the size of the image to “cover” and the position to “center”.

#header {
  background-image: url(/path/to/image.jpg);
  background-size: cover;
  background-position: center;
}

Write an HTML tag to add an image as an inline element with a src attribute of “/path/to/image.jpg” and an alt attribute of “A description of the image”.

<img src="/path/to/image.jpg" 
alt="A description of the image">

Write a CSS rule to use an image as an icon with a width and height of 32 pixels and a background image of “/path/to/icon.png”.

.icon {
  width: 32px;
  height: 32px;
  background-image: url(/path/to/icon.png);
}

Write an HTML tag to use responsive images with a src attribute of “/path/to/image.jpg”, a srcset attribute with three different versions of the image, and a sizes attribute with the appropriate sizes for each version.

<img src="/path/to/image.jpg" 
srcset="/path/to/image-small.jpg 300w, /path/to/image-medium.jpg 600w, 
/path/to/image-large.jpg 900w"
sizes="(max-width: 400px) 300px, 
(max-width: 800px) 600px, 900px" 
alt="A description of the image">

Write a CSS rule to use an image sprite with a width and height of 32 pixels and a background image of “/path/to/sprite.png”. Set the position of the icon to the first icon in the sprite.

.icon {
  width: 32px;
  height: 32px;
  background-image: url(/path/to/sprite.png);
  background-position: 0 0;
}