views:

228

answers:

3

I'm trying to center a block element (the WordPress caption box, including the image) but it won't work. I've tried:

.imagecenter {
   margin-left: auto;
   margin-right: auto;
   display: block;
}

But it just won't work. I've also tried margin-left: auto; margin-right: auto; but that won't work either. Is there anything I'm doing wrong? This is what the W3C documentation says I should do.

It looks like this in the HTML (to clarify):

<div id="content">
........post here......
<div class="wpcaption imagecenter" style="width:225px">
<img src="blah" />
Blah.
</div>
.........post here......
</div>

I have no control over the width of the element. It's set by the user. The user wants the div to be centered. It's not working. I've looked over documentation but it still won't work.

EDIT: I HAVE ALREADY TRIED MARGIN-LEFT: AUTO AND MARGIN-RIGHT: AUTO. IT DOESN'T WORK.

+2  A: 

Give it a width less than that of its parent.

.parent    { }
.imgCenter { width:320px!important; margin:auto; }

<div class="parent">
  <img src="foobar.jpg" class="imgCenter" />
</div>
Jonathan Sampson
The width is set inline.
Brandon Wang
See my updated-answer for a working example.
Jonathan Sampson
yup, you have to assign a width or no dice.
ethyreal
I don't know the width of the child; it's set inline. This is a WordPress theme. The parent should be already defined, as it's the container the content of the posts are in.
Brandon Wang
Brandon, you have to give the item you want centered an explicit width. That is how your margin:auto figures out what the margins should be.
Jonathan Sampson
I am. All the solutions you are giving I already know. The width is inlined in the html (as in <div style="width: 200px"> because it changes).
Brandon Wang
Add !important within your css.
Jonathan Sampson
If 200px is less than the parent width, it doesn't matter.
Jonathan Sampson
The width is set by the user through the WordPress write-post interface. I have no control over it. I just want it centered. I'm surprised there's no easy way to do this; I don't want to align=center things. I want to do it the CSS way. It's not working.
Brandon Wang
If there is an explicit width given, margin:auto will handle the centering...unless the width is equal-to or greater than the parent-width.
Jonathan Sampson
Then how come it won't work?
Brandon Wang
I don't know. Post a link to your page and we can help.
Jonathan Sampson
I can't. It's a WordPress theme, and I don't have the time to put up a test site yet.
Brandon Wang
There; I've updated the example.
Brandon Wang
A: 

In general, to position a div in the center, the technique is to make both the left and right margins auto and then give the div a width:

.centerDiv
{
    margin-left:auto;
    margin-right:auto;
    width: XXX px;
}
desigeek
I have to accept this because it's right, but I can't get it to work.
Brandon Wang
A: 

An easier solution might be to set margin-left: auto, margin-right: auto and text-align: center (for the caption text) on all child elements of your containing element:

.imagecenter *{
    margin-left: auto;
    margin-right: auto;
    text-align: center;
}

This means you won't have to explicitly set the width of your containing element, but has the drawback that your caption text will have a 100% width, which might look wierd.

You