tags:

views:

2257

answers:

2

I've searched other questions and, while this problem seems similar to a couple of others, nothing I've seen so far seems to address the issue that I'm having.

I have a div which contains a number of other divs, each of which is floated left. These divs each contain a photo and a caption. All I want is for the group of photos to be centered within the containing div.

As you can see from the code below, I've tried using both overflow:hidden and margin:x auto on the parent divs, and I've also added a clear:both (as suggested in another topic) after the photos. Nothing seems to make a difference.

Thank you. I appreciate any suggestions.

(For context, you can refer to the live site.)

<div style="position: relative; margin: 0 auto; overflow: hidden; text-align: center;">
    <h4>Section Header</h4>

    <div style="margin: 2em auto;">

        <div style="float: left; margin: auto 1.5em;">
            <img src="photo1.jpg" /><br />
             Photo Caption
        </div>
        <div style="float: left; margin: auto 1.5em;">
            <img src="photo2.jpg" /><br />
             Photo Caption
        </div>
        <div style="float: left; margin: auto 1.5em;">
            <img src="photo3.jpg" /><br />
             Photo Caption
        </div>

        <div style="clear: both; height: 0; overflow: hidden;"> </div>

    </div>

</div>
+3  A: 

Text-align:center on the main outer div. And for the inner divs, display:inline or display:inline-block.

Just tested it - display:inline-block on the inner divs works. Might also be wise to give them explicit widths too.


<div style="margin: auto 1.5em; display: inline-block;">
  <img title="Nadia Bjorlin" alt="Nadia Bjorlin" src="headshot.nadia.png"/>
  <br/>
  Nadia Bjorlin
</div>
Jonathan Sampson
The main outer div already uses text-align: center and it doesn't seem to do anything. I just added the display: inline-block for each photo and, again, no difference.Just for the sake of argument, I added an explicit width to each photo's div. Absolutely no difference.I'd be curious to see how you just tested it and it works, and I just did it and it didn't make any difference.
Darren
@Darren: did you stop floating when you tried? You won't be able to accomplish what you want as long as you're floating /unless/ you set a fixed width on the container of the floats.
Ken Browning
No, I didn't. I'm still floating the images. For what I want -- the caption centered directly below the image -- I think I have to float the div. But if I float the div, I can't center it in relation to the outer div?
Darren
Oh... I'm wrong. Removing the float looks like it accomplishes exactly what I want. I'm not sure that I understand why, but... Thank you very much.
Darren
@Darren, notice in my code example I didn't include the floating ;) Read more carefully next time, it'll save you some frustration :) Glad you got it figured out though. Keep up the good work, and welcome to SO.
Jonathan Sampson
Thanks. All I can figure is that I'd been looking at that block of code on my own for two days straight and I just didn't clue into the fact that you omitted the float. Next time I will try to pay closer attention. Again, thank you.
Darren
A: 

What if you cannot remove the floats? How can you center an object that contians floated items in it?