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:

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

Sample Image This is a responsive image.

Code Example: Adding Image Shadow

img {
            box-shadow: 10px 10px 20px rgba(0, 0, 0, 0.1);
        }

Output

Image with Shadow This image has a shadow effect.

Common CSS Image Properties:

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.