html - 图像超出 flex 父尺寸

标签 html css flexbox

<分区>

我在制作 <img> 时遇到问题在 flex parent 内部灵活。图像开始超出父级尺寸。我想给 parent 留下 display: flex;并限制图像跨越父级的尺寸。

.full
{
  width: 400px;
  height: 200px;
  background-color: blue;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
}
.half
{
  width: 50%;
  background-color: red;
  padding: 20px;
}
img
{
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
}
<div class="full">
  <div class="half">
          <h1>title</h1>
          <img src="http://placehold.it/90x200">
  </div>
</div>

您能否解释并提供一些有用的链接来解释所显示的行为及其可能的解决方案?

我同时使用 Firefox 和 Chrome 来查看。问题在两种浏览器中都存在。


这个问题似乎与 this question 重复

最佳答案

为了让它工作 .half div 也需要是一个 flexbox 容器并且图像需要有 min-height: 0,正如解释的 here .

.full
{
  width: 400px;
  height: 200px;
  background-color: blue;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
}
.half
{
  width: 50%;
  background-color: red;
  padding: 20px;
  display: flex;
  flex-flow: column nowrap;
  justify-content: flex-start;
}
img
{
  min-height: 0;
  max-width: 100%;
  max-height: 100%;
}
<div class="full">
  <div class="half">
    <h1>title</h1>
    <img src="http://placehold.it/90x200">
  </div>
</div>

关于html - 图像超出 flex 父尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51973799/

相关文章:

html - div的溢出区域没有样式

html - 设置元素列的样式,行之间没有 akward 间距

jquery - 如何使标题高度相对于(制表符)内容高度?

html - 如何向此代码添加链接和可下载文件?

Jquery 在 h1 标签内添加 div

javascript - 制作 Web 应用程序时,如何在 Safari 中禁用复制、粘贴、选择、全选?

javascript - 隐藏基于当前幻灯片的时间轴的子部分?

html - Flexbox 滚动或拉伸(stretch)在 Firefox 中无法正常工作

html - 在 Emacs 中将语法突出显示的代码转换为 HTML

html - 将空查询字符串传递给 URL 是一种错误的形式吗?