image captions that wrap within width of image

Discussion in 'CSS' started by ardarvin, Mar 18, 2008.

  1. #1
    Hey, I'm trying to make a float image with captions:

    div.image {
    float: left;
    padding: 0 4px 0 0;
    }

    div.image p {
    margin: 0;
    text-align: center;
    font-size: smaller;
    }

    And using it as such:

    <div class="image">
    <img src="http://www.myimage.com/myimage.jpg" alt="pic"/>
    <p>This is my caption, which is very very very very very very very very very very very very longing. (Yes I typed each of those very's).</p>
    </div>

    However, I'd like the caption to wrap within the bounds of the image width...which is not fixed or known.

    How would I go about this?
     
    ardarvin, Mar 18, 2008 IP
  2. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here's a guess:
    If you set no width at all, the float is supposed to shrink wrap. However, I think they don't in FF or IE. Only Opera and Safari seem to follow this spec. For IE7 and FF you might be able to get away with a max-width setting (find the largest image and use that as a benchmark). Max-width is not considered a width declaration so the float should still shrinkwrap.
     
    Stomme poes, Mar 19, 2008 IP
  3. ardarvin

    ardarvin Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, the max-width works great. Just put:

    div.image p {

    max-width: 99%;

    }

    and it doesn't matter what the size of the image is, the text will wrap accordingly.
     
    ardarvin, Mar 19, 2008 IP
  4. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Add a width for IE6 because it doesn't understand anything other than width:

    * html div.image p {
    width: 99%;
    }

    Duh I should have thought of the p as a child cause then of course it can't go wider than its parent whatever the width... usually the most-obvious answers are the ones I don't see for the longest time.
     
    Stomme poes, Mar 20, 2008 IP