Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS

Tushar Arora
Tushar Arora
1,760 Points

Margin: 0 auto ; Does not center the image on page.

I want to center one and only image in the body on this page. I tried using margin: 0 auto; but it did not worked. Why is it so ?

This is my simple html code.

<! DOCTYPE html>
<html>
    <head>
        <title>Tushar Arora | Software Developer</title>
        <meta charset="utf-8"/>
        <link href="css/normalize.css" rel="stylesheet"/>
        <link href="css/main.css" rel="stylesheet"/>
    </head>

    <body>

             <img src="img/earth.gif" id="earth"/>

    </body>
</html>

This is simple Css code

#earth {

    margin: 0 auto;
}

2 Answers

Inge L
Inge L
30,058 Points

There is one great article about centering elements: https://css-tricks.com/centering-css-complete-guide/

<! DOCTYPE html>
<html>
    <head>
        <title>Tushar Arora | Software Developer</title>
        <meta charset="utf-8"/>
        <link href="css/main.css" rel="stylesheet"/>
    </head>

    <body>
        <div id="earth">
        <!-- <div class="earth"> -->
            <img src="img/earth.gif" alt="" >
        <!-- </div> -->
        </div>
    </body>
</html>
/* inline - center horizontally 
#earth {
    text-align: center;
}
*/

/* block - center horizontally */
#earth {
    margin: 0 auto;
        /* width of the image */
    width: 297px;
}

/* block - center horizontally and vertically

 .earth {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
} 

*/
Tushar Arora
Tushar Arora
1,760 Points

Thanks for the good article but even if I use the below code then also the image will not come to center

<div id="earth">    
    <img src="img/earth.gif"/>
</div>
#earth {
    margin: 0 auto;
}

I dont know why in tutorials it has been told many times that if you want to center an element use margin : 0 auto;

Inge L
Inge L
30,058 Points

Use width and put how much pixels image is wide.

"You can set the margin property to auto to horizontally center the element within its container. The element will then take up the specified width, and the remaining space will be split equally between the left and right margins: " - http://www.w3schools.com/css/css_margin.asp

Jonathan Selwall
Jonathan Selwall
12,528 Points

Hey! It's because images are Inline-blocks, if you do this it should work:

img{
  margin: 0 auto;
  display: block;
}

If you want to align inline elements you can just put text-align: center on the container.