Open In App

How to Align Images in CSS?

Last Updated : 12 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Aligning images properly is essential for creating visually appealing and well-structured web pages. CSS can provide several methods to achieve this, each suited for the different scenarios. we are going to discuss how can we align images using CSS.

Below are the following approaches to align images in CSS:

Using Text Alignment

The text-align property is primarily used for aligning the inline content, such as the text, within block-level containers. Since images are inline elements by default, text-align can work effectively to horizontally align the images as well. This approach can be simple and is mainly used for the basic horizontal alignment (left, center, right) within the container.

Syntax:

.container {
text-align: center;
/* Options: left, right, center, justify */
}

Example: In this example, the text-align: center; property on the .container class centers the image horizontally within its container.

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>Text Alignment Example</title>
    <style>
        .container {
            text-align: center;
        }

        img {
            width: 45%;
            height: auto;
            margin: 5px;
        }
    </style>
</head>

<body>
    <div class="container">
        <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240908192235/geekforgeek.png" 
             alt="Image 1">
        <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240908202351/gfg.jpeg" 
             alt="Image 2">
    </div>
</body>

</html>

Output:

Using Float property

The float property was originally intended for the text wrapping around the images, but it can also be used to place the elements side by side. When the image can be floated, it is taken out of the normal document flow and allowing the other content (such as text or images) to wrap around it.

Syntax:

.image-left {
float: left;
/* Options: left, right */
margin: 10px;
/* Optional: Adds space around the image */
}

.image-right {
float: right;
}

Example: In this example, the image with the class .image-left can be floated to the left and allowing the surronding the text to wrap around it.

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>Float Example</title>
    <style>
        .image-left {
            float: left;
            margin: 10px;
            width: 45%;
            height: auto;
        }

        .image-right {
            float: right;
            margin: 10px;
            width: 45%;
            height: auto;
        }

        .container::after {
            content: "";
            display: table;
            clear: both;
        }
    </style>
</head>

<body>
    <div class="container">
        <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240908202351/gfg.jpeg" 
             alt="Image 1" class="image-left">
        <img src="
https://media.geeksforgeeks.org/wp-content/uploads/20240908192235/geekforgeek.png" 
             alt="Image 2" class="image-right">
    </div>
</body>

</html>

Output:

Using Flexbox Model

Flexbox is the one-dimensional layout model that allows for the flexible and precise alignment of the elements along both the horizontally and vertical axes. Flexbox container distributes the space among the items and provide the alignment capabilities that make it perfect for the responsive layouts.

Syntax:

.container {
display: flex;
justify-content: center;
/* Options: flex-start, center, flex-end, space-between, space-around */
align-items: center;
/* Options: flex-start, center, flex-end, baseline, stretch */
}

.image {
align-self: center;
/* Options: auto, flex-start, center, flex-end, baseline, stretch */
}

Example: In this example, the flex container center the image both horizontally and vertically withinh the viewport using the jusfity-content and align-items.

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>Flexbox Example</title>
    <style>
        .container {
            display: flex;
            justify-content: space-between;
            /* Adjust as needed: flex-start, center, space-between, space-around */
            align-items: center;
            height: 100vh;
            /* Full viewport height */
        }

        img {
            width: 45%;
            height: auto;
        }
    </style>
</head>

<body>
    <div class="container">
        <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240908192235/geekforgeek.png" 
             alt="Image 1">
        <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240908202351/gfg.jpeg" 
             alt="Image 2">
    </div>
</body>

</html>

Output:

Using Grid Layout

CSS Grid is the two-dimensional layout system that gives you fine-grained control over the both rows and columns. We can create the complex layouts by defining the grid lines, tracks, and areas. Each grid item can be precisely placed into the specific column and row, making it very flexible for the layout design.

Syntax:

.container {
display: grid;
grid-template-columns: 1fr 1fr;
/* Creates two equal columns */
}

.image {
grid-column: 2;
/* Positions the image in the second column */
justify-self: center;
/* Options: start, center, end, stretch */
}

Example: In this example, the image can be placed in the second column of the grid with two columns, and is centered within that the column using justify-self.

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>Grid Example</title>
    <style>
        .container {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
            /* Adds space between grid items */
        }

        img {
            grid-column: span 1;
            justify-self: center;
            /* Centers image within its grid cell */
            width: 100%;
            height: auto;
        }
    </style>
</head>

<body>
    <div class="container">
        <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240908202351/gfg.jpeg" 
             alt="Image 1">
        <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240908192235/geekforgeek.png" 
             alt="Image 2">
    </div>
</body>

</html>

Output:

Using Positioning Property

The position property in CSS can allows you to control the position of the elements based on their containing block. There are several values for the position:

Syntax:

.container {
position: relative;
/* Establishes a containing block for absolutely positioned elements */
}

.image {
position: absolute;
top: 10px;
left: 20px;
}

Example: In this example, the image can be positioned 20px from the top and 30px from the left of the .container using the absolute positioning.

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>Positioning Example</title>
    <style>
        .container {
            position: relative;
            height: 300px;
            border: 1px solid #ccc;
        }

        .image {
            position: absolute;
            width: 45%;
            height: auto;
        }

        .image1 {
            top: 10px;
            left: 10px;
        }

        .image2 {
            top: 10px;
            right: 10px;
        }
    </style>
</head>

<body>
    <div class="container">
        <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240908192235/geekforgeek.png" 
             alt="Image 1"
             class="image image1">
        <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240908202351/gfg.jpeg" 
             alt="Image 2"
             class="image image2">
    </div>
</body>

</html>

Output:

Using Margin Property

Using the margin property, we can align the images by adjusting the space around them. Specifically, setting the margin-left: auto and margin-right: auto allows for the horizontally centering of the block-level elements. This method can be particularly useful when you want to center the image inside a container.

Syntax:

.image {
display: block;
margin-left: auto;
margin-right: auto;
/* Centers the image horizontally */
}

Example: In this example, the images are centered horizontally within the .container by setting the margin-left and margin-right to auto and ensuring the display: block for the images.

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>Margin Example</title>
    <style>
        .container {
            width: 80%;
            margin: 0 auto;
            border: 1px solid #ccc;
            text-align: center;
            /* Aligns images horizontally within the container */
        }

        img {
            display: block;
            margin: 10px auto;
            width: 45%;
            height: auto;
        }
    </style>
</head>

<body>
    <div class="container">
        <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240908192235/geekforgeek.png" 
             alt="Image 1">
        <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240908202351/gfg.jpeg" 
             alt="Image 2">
    </div>
</body>

</html>

Output:


Next Article
Article Tags :

Similar Reads