All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Image Align Center CSS

Last Updated : Mar 11, 2024

Image Align Center CSS

In this article we will show you the solution of image align center CSS, we will use the background-position property to it. We will see two different examples of it.

In the first method, we will add the background-image property in CSS into <div>. then using the background-image property to no-repeat and background-position to center to Align the image to the Center.

In the second method, we will use the CSS flexbox method. we will set the image tag to flex 1 and add the background-position to the center to Align the image to the center.

Step By Step Guide On Image Align Center CSS :-

Method 1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> image align center CSS </title>
    <style>
        body{
            background-color: rgb(51, 50, 50);
            font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
        }
        h1{
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
        h3{
            color : rgb(95, 194, 95) ;
            font-size: 20px;
        }
        .container {
            height: 500px;
            width: 500px;
            background-color: lightblue;
            background-image: url('bird.jpg');
            background-size: 100px;
            background-repeat: no-repeat;
            background-position: center;
        }
    </style>
</head>
<body>
    <center>
        <h1> TALKERSCODE </h1>
        <h3> image align center CSS </h3>
    </center>
    <div class="container">
        image
    </div>
</body>
</html>
  1. writing HTML code <!DOCTYPE html> is used to specify the HTML version that a file is written in.
  2. The <HTML> tag used to specify the root of an HTML document must then be written. Additionally, add the </html> tag at the end.
  3. The metadata for the HTML file is contained in the <head> tag, which is closed with </head> tag.
  4. The HTML document's <title> is then established using the title tag, which is followed by the < /title> tag.
  5. We'll use an external CSS file to style the HTML page.
  6. The <h1> element is used to add a heading, and the </h1> tag is used to close it.
  7. creating a <div> with the class of container.
  8. using <style> tag to add CSS
  9. for the class container, setting the height and width of the div first. Then set the background-color
  10. Now adding the background-image and add image URL into it.
  11. Lastly, adding the background-size. And set the background-repeat to no-repeat and background position to center.

Method 2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> image align center css </title>
    <style>
        body{
            background-color: rgb(51, 50, 50);
            font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
        }
        h1{
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
        h3{
            color : rgb(95, 194, 95) ;
            font-size: 20px;
        }
        .container{
            height: 300px;
            width: 300px;
            display: flex;
            margin: auto auto;
            background-position: center;
        }
        img {
            flex: 1;
        }
    </style>
</head>
<body>
    <center>
        <h1> TALKERSCODE </h1>
        <h3> image align center css </h3>
    </center>
    <div class="container">
        <img src="bird.jpg" alt="">
    </div>
</body>
</html>
  1. writing HTML code <!DOCTYPE html> is used to specify the HTML version that a file is written in.
  2. The <HTML> tag used to specify the root of an HTML document must then be written. Additionally, add the </html> tag at the end.
  3. The metadata for the HTML file is contained in the <head> tag, which is closed with </head> tag.
  4. The HTML document's <title> is then established using the title tag, which is followed by the < /title> tag.
  5. We'll use an external CSS file to style the HTML page.
  6. The <h1> element is used to add a heading, and the </h1> tag is used to close it.
  7. creating a <div> with the class of container. Adding image file using <img> tag into that div.
  8. using <style> tag to add CSS
  9. for the container class, set the display to flex. Set the Margin to auto and background-position to center.
  10. For the img tag, setting the flex to 1.

Conclusion :-

Last, here in conclusion, we can say that with this article’s help, we know How to image align center in CSS.

I hope this article on image align center CSS helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪