Image caption width to same as image

前端 未结 6 717
生来不讨喜
生来不讨喜 2020-12-25 13:16

How can I make image caption width same as image? Now I have the following code:

\"\" &
相关标签:
6条回答
  • 2020-12-25 13:38

    You could try to set the image div wrapper display:inline-block

    http://jsfiddle.net/steweb/98Xvr/

    If the caption is long, the only solution will be to set a fixed width, or set the .image div width by js. I'm trying to think about a pure css solution, but I think it's an hard challenge :)

    0 讨论(0)
  • 2020-12-25 13:39

    So the problem is that you don't know how wide the img will be, and the caption for the img may exceed the width of the img, in which case you want to restrict the width of the caption to the width of the img.

    In my case, I applied display:table on the parent element, and applied display:table-caption and caption-side:bottom on the caption element like this:

    <div class="image" style="display:table;">
        <img src="foo.jpg" alt="" />
        <div style="display:table-caption;caption-side:bottom;">This is the caption.</div>
    </div>
    
    0 讨论(0)
  • 2020-12-25 13:55

    The only way to do captioning properly is to enclose the image and caption in a table constructed from span elements with table, table-row and table-cell css attributes.

    Any other method (including HTML5 figure tags) either gives width mismatches or causes unwanted line breaks.

    If your method must work in a non-css3 browser, then a table is probably the best bet.

    0 讨论(0)
  • 2020-12-25 13:58

    You can apply display:table; and an arbitrary initial width, eg. width:7px; to the parent block like figure and everything will work as expected!

    Here is a live demo: http://jsfiddle.net/blaker/8snwd/

    This solution's limitation is that IE 7 and earlier do not support display:table; and IE 8 requires your doctype to be !DOCTYPE.

    Sources:

    • http://www.lifeathighroad.com/web-development/forcing-to-wraps-to-the-width-of-an-image-using-css-only/
    • W3Schools (can't post link due to stackoverflow's 2-link limit for people with less than 10 rep)
    0 讨论(0)
  • 2020-12-25 13:58

    The key is to treat the image as having a certain width some length. In the example below I checked and my image was 200px wide. And then treat the caption as an inline-block.

    HTML:

    <div style="width:200px;">
       <img src="anImage.png" alt="Image description"/>
       <div class="caption">This can be as long as you want.</div>
    </div>
    

    CSS:

    .caption {
       display: inline-block;
    }
    

    You can also replace the inline-block with text that is left justified, right, or stretched over the exact image width by replacing above css with one of the following:

    .caption {
       /* text-align: left; */
       /* text-align: right; */
       text-align: justify;
    }
    
    0 讨论(0)
  • 2020-12-25 14:03

    If the problem is the caption being too long, you can always use

    
    div.image {
        width: 200px; /* the width you want */
        max-width: 200px; /* same as above */ 
    }
    div.image div {
        width: 100%;
    }
    

    And the caption will stay static. Also, the tag name is img, not image.

    Or, if you want to detect your image width, you can use jQuery:

    
    $(document).ready(function() {
        var imgWidth = $('.image img').width();
        $('.image div').css({width: imgWidth});
    });

    That way, you're getting the image width, then you set the caption width to it.

    0 讨论(0)
提交回复
热议问题