CSS - div width depending on image size above

后端 未结 6 696
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 14:59



        
6条回答
  •  Happy的楠姐
    2020-12-17 15:33

    @JackJoe said it well, you need to set a width to the wrapping div to match the width of the inner image.

    Here is one way of doing it: http://jsfiddle.net/audetwebdesign/bpN8x/

    Basically, I use jQuery to determine the width of the image and then set the width of the wrapper div.

    Alternatively, if you were feeding the image from a CMS, you could determine the width of the image dynamically and then set the width on the wrapper div using an inline style.

    I don't think a CSS-only is possible because of the way the box-model determines widths as content is laid out. Widths are inherited from the parent element, and are not passed up from the children elements.

提交回复
热议问题