The Wayback Machine - https://web.archive.org/web/20250109040523/https://www.geeksforgeeks.org/how-to-add-image-in-css/
Open In App

How to Add Image in CSS?

Last Updated : 10 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Images are an essential part of web design, bringing the visual appeal and providing the context to content. In web development, images can be added to the websites using HTML and styled or manipulated using CSS. CSS can offer various ways to incorporate images into web pages, allowing the developers to control how images are displayed, their size, and even how they respond to the different devices.

These are the following ways to add images:

Background Images

Background images are used to set the image as the background of an HTML element, such as the div, section, or body. The image can be styled to cover the entire element, repeat, or stay in the fixed position.

Syntax:

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

Example: In this example, the div with the class image container has the background image. The image can be set to cover the entire div, is centered, and does not repeat.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <style>
        .image-container {
            width: 500px;
            height: 300px;
            background-image: url('gfg.jpeg');
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
        }
    </style>
</head>

<body>
    <div class="image-container">
        <h1>Welcome to the Page</h1>
    </div>
</body>

</html>

Output:

cssimage1
Output

Image as Content

The most straightforward way to add the image to a web page is through the <img> HTML tag. However, CSS can be used to style the image, such as the adjusting its size, adding borders, and applying the effects like shadows.

Example: This example can shows the image with the width of the 300px, rounded corners, and a shadow effect. CSS can be used to control the size, and appearance of the image.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <style>
        .image-style {
            width: 300px;
            height: auto;
            border-radius: 15px;
            box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.3);
        }
    </style>
</head>

<body>
    <h1 style="color: green;">GeeksforGeeks</h1>
    <img src="gfg.jpeg" alt="Profile Picture" class="image-style">
</body>

</html>

Output:

cssimage2
Output

Image with the content Property

We can use the CSS pseudo-elements (::before or ::after) to insert the images without adding them directly in the HTML. The content property can allows you to embed the images before or after the elements content.

Syntax:

.element::before {
content: url('icon.png');
display: inline-block;
margin-right: 10px;
width: 20px;
height: auto;
}

Example: In this example, the icon can added before the paragraph text using the ::before pseudo-element. the image can appear inline with the text and enhancing its visual appeal.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <style>
        .decorated-text::before {
            content: url('gfg.jpeg');
            display: inline-block;
            margin-right: 10px;
            width: 30px;
            height: auto;
        }
    </style>
</head>

<body>
    <p class="decorated-text">This text has an icon before it.</p>
</body>

</html>

Output:

cssimage5
Output

Conclusion

CSS offers the multiple ways and style images on the web pages, allowing for the great flexibility and control. Whether you are using the background images for the design, content images for the media, or decorative images with pseudo-elements, each approach can serves the specific purposes. The examples can provided here give you the foundation to start adding the images to the CSS projects in the various ways.


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg