I'm building a website, and I've run into the problem that for some reason, putting margin-left: auto and margin-right: auto will not center images like it's supposed to, and I was wondering if someone could tell me what I'm doing wrong.
I've discovered that by manually adding style = "margin-left: auto; margin-right: auto;" it causes the image to be centered. But because I'm using class = "center" (and I defined .center in my CSS file), it doesn't work.
The really weird thing is that I've added borders and stuff to verify that the .center class is being loaded. And I've also scanned the rest of my CSS file to make sure there's nothing that would be overriding the margins. AND Firebird also doesn't list anything that would be overriding them. And yet the image STILL won't center, and I've tested this in both IE and FF.
And after some more pulling out of my hair, I've discovered this line was the problem:
Code:
#main_content img {
margin: 0.5em;
}
main_content is the div where I keep each page's main content (the stuff that's that "page" as opposed to the navigation bars that appear on every page, etc.).
So now, I understand that setting a margin automatically applies it to all four margin-top margin-bottom margin-left and margin-right properties. But two things:
1. I placed my .centered class after the #main_content img section, and I thought CSS was supposed to overwrite previous values. So why was this an issue?
2. Does anyone have a solution for how I can specify that I want all my images to have a margin of 0.5em, while still being able to use my .centered class to specifically center images I want centered? I can't use padding, because some of my images have borders, and that would cause the padding to appear inside the border, making them look weird.
margins are the property of the block-level elements, and they can not be applied upon inline element. IMG is considered an inline element.
So, either you nest that image within a DIV (with the same height and width) and give that DIV margins auto, or you give that image a display block and give it the margins auto. The former is recommended.
There is also a third possibility. Make that image as being a background-image for the element inside you want it to be centered, and position the background to be in center.
Ah, thanks. That's what I was afraid of. My website is already designed, so adding margins will mean I have to edit every page to place every image inside its own div. I had no idea img was an inline element. The W3C people should really create a way to control the layout of your imgs without having to put them in a div.
If I can solve my question #1 of why main_content img {margin: 0.5em} is overpowering my lower margin-left: auto and margin-right: auto, I may be able to make this work without having to edit every page. Any ideas why that line is overpowering the lower line? I thought lower lines were supposed to take precedence. Particularly if they're specified as part of a class, and not just a standard img element.
My advice is to use pixels, not em whenever it comes about size, margins, padding, etc. em is a relative measurement unit, and the reference is from parent to child, thus 0.5 em might look different in different parts of you document, or if you make later a change in the DOM structure.
Use em only for font-size, if you think you need that. And even so, take care to give a firm font-size in pixels for the ultimate parent, usually the body.
Bookmarks