繁体   English   中英

如何在图片周围包裹div?

[英]How to wrap div around image?

我试图将图像包装在div中,但div被父div扩展。

<div id="parent" style="display: inline-block; position: relative; text-align:center">
    <div id="wrapper">
        <img>
    </div>
    <p>Some text that expands the wrapper div more than image width</p>
</div>

文本字段将父div扩展为比图像宽,这还将扩展包装div。 如何使包装器div与img完全相同?

img和包装器都没有边距或填充,但是包装器div比图像宽。

在#wrappper上使用display:inline-block

演示

您需要在包装器元素上设置display:inline-block

 <div id="parent" style="display: inline-block; position: relative; text-align:center">
    <div id="wrapper" style="display: inline-block;">
        <img src="https://cdn2.iconfinder.com/data/icons/picol-vector/32/source_code-128.png" />        
    </div>    
    <p>Some text that expands the wrapper div more than image width</p>
</div>

如果要保持p大小与图像相同,则需要使用一些JavaScript或jQuery

例如

 $('#parent').css({
     maxWidth: $('img').width()
 });

小提琴

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM