html - 自动调整父 div 的大小

标签 html css

我想将调整大小的子图像放在父 div 中。我如何让这个父 div 与子图像相同,而不是更大?有关示例,请参见下面的代码段。谢谢。

div {
  border: 1px solid blue;
  width: auto;
  height: auto;
  display: inline-block;
  overflow: auto;
}

img {
  background-color: orange;
  width: 80%;
  height: 80%;
  overflow: auto;
}
<div>
  <img src="https://www.python.org/static/opengraph-icon-200x200.png" />
</div>

最佳答案

在这种情况下,您需要让 imgwidth100% 并且 width code> 或 80% 的东西:

div {
  border: 1px solid blue;
  width: auto;
  height: auto;
  display: inline-block;
  overflow: auto;
}

img {
  background-color: orange;
  display: block;
}
<div>
  <img src="https://www.python.org/static/opengraph-icon-200x200.png" />
</div>

预览

preview

关于html - 自动调整父 div 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47243741/

相关文章:

html - 使包含普通文本的 td 与粗体一样宽

javascript - 第二次 .html() 渲染后事件未触发

javascript - 当您在 iframe 中单击时,如何在 iframe 的父 div 中添加类?

css - 隐藏页面上的文本但在 window.print 上显示

html - th 宽度不起作用?

html - CSS 元素在某些 div 中失去绝对位置

html - 复制时自动添加网址

html - CSS li a 'cursor:pointer' 在 div 中不工作

javascript - 为 d3.js 可折叠树设置背景

html - 3 CSS中的盒子布局