html - 更改 DIV 高度以适合图像

标签 html css

我正在尝试制作一个 DIV,它几乎是一个带边框的框,该边框具有左对齐的图像和位于图像右侧的文本。这是我的设置方式:

    <div style="padding:1%; border-style:solid; border-size:1px; width:100%;">
        <img src="http://i.imgur.com/FwgZFNn.jpg" style="float:left; max-width:30%; max-height:200px;" />
        Here is some text.
    </div>

问题是,如果图像比文本高,周围的 DIV(以及边框)会自行调整大小以适应所有文本所需的高度,但图像会溢出分区。

如何使 DIV 更改其高度以适应较高的(图像或文本),以便两者都适合边框?

谢谢。

最佳答案

display: inline-block" 添加到您的 div。

<div style="padding:1%; border-style:solid; border-size:1px; width:100%;display: inline-block">
    <img src="http://i.imgur.com/FwgZFNn.jpg" style="float:left; max-width:30%; max-height:200px;" />
    Here is some text.
</div>

关于html - 更改 DIV 高度以适合图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26911772/

相关文章:

javascript - anchor html (<a>) 仅适用于右键单击而不是左键单击

javascript - 如何在函数内显示带有超链接的 JavaScript 图像

javascript - FileReader.onprogress block 大小

javascript - 如果我在打开 chrome/opera 浏览器控制台的情况下 console.log 未处理的拒绝事件,为什么拒绝处理不会触发(被捕获)?

html - 专注于父 dom 但给子 dom 提纲

html - 如何使用 CSS 使 <DIV 使用 Bootstrap 居中?

javascript - 红圈在左上角开始前 <animateMotion>

Python 检查网站是否存在

javascript - 我的 Javascript for 循环有什么问题

html - 自动宽度的 Nav ul 和 li 问题