CSS Images
The CSS Images properties are used to control the display and appearance of images in a web page. You can style images to create responsive layouts, set image sizes, add effects, and more. Proper use of CSS images ensures that your web design is attractive and functional.
Key Points on CSS Images:
- The
background-image
property is used to set an image as the background of an element. - The
object-fit
property helps control how an image fits within its container, especially useful for responsive designs. - The
border-radius
property can be used to create rounded corners for images. - Images can be made responsive using the
max-width
andheight
properties. - Use
box-shadow
to add shadow effects around images. - The
filter
property allows you to apply visual effects like grayscale, sepia, blur, and more to images.
Syntax for CSS Images Properties:
Syntax Example
/* Example for setting a background image */
background-image: url('image.jpg');
/* Example for controlling image size */
img {
width: 100%;
height: auto;
}
/* Example for responsive image */
img {
max-width: 100%;
height: auto;
}
Examples of CSS Image Properties:
The following examples demonstrate the usage of different image-related properties:
Code Example: Setting Background Image
div {
background-image: url('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSzg5N686jvlIDt6ZXHyk1yvDPAYQ48Kjrxsw&s');
background-size: cover;
background-position: center;
height: 200px;
}
Output
This div has a background image.
Code Example: Responsive Image
img {
max-width: 100%;
height: auto;
}
Output

Code Example: Adding Image Shadow
img {
box-shadow: 10px 10px 20px rgba(0, 0, 0, 0.1);
}
Output

Common CSS Image Properties:
- background-image: Sets an image as the background of an element.
- object-fit: Specifies how the content of an image should fit within the container (values:
cover
,contain
,fill
). - border-radius: Used to create rounded corners for images.
- max-width: Ensures that the image does not exceed the specified width.
- height: Specifies the height of the image.
- box-shadow: Adds a shadow effect around an image.
- filter: Applies visual effects like blur, grayscale, etc., to images.
To optimize the use of images in your website, consider using responsive images, background images for decorative purposes, and applying CSS effects for visual enhancements. Always ensure that images are optimized for faster page load times, especially on mobile devices.