img set flex-grow to fill flex container rest space, it cause flex inner overflow flex container(img 设置 flex-grow 填充 flex 容器剩余空间,导致 flex 内部溢出 flex 容器)
问题描述
以下是我的代码,text1";溢出 flex 容器,我希望 flex 容器中的 img + text 和 img 填充 flex 容器休息
following is my code, "text1" overflow the flex container, I expect img + text in flex container and img fill flex container rest
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!--mobile friendly-->
<meta name="viewport" content="width=device-width, user-scalable=yes">
<style>
.c {
display: flex;
flex-direction: column;
width: 100px;
height: 100px;
border: 5px solid lightblue;
}
.c img {
flex-grow: 1;
object-fit: contain;
}
</style>
</head>
<body>
<div class="c">
<img src="./img.jpg"/>
<div>text1</div>
</div>
</body>
</html>
结果是:
如果元素不是 img 并且是 div,flex-grow: 1 将填充 flex rest 空间,我很困惑为什么 img 会导致内部溢出
if element is not img and is div, flex-grow: 1 will fill flex rest space, I'm confused why img cause inner overflow
以下是我的img.jpg"
following is my "img.jpg"
推荐答案
/* CSS */
.c {
width: 100px;
height: 100px;
border: 5px solid lightblue;
position: relative;
text-align: center;
}
.c img {
width: 100%;
}
.c div {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: red;
}
<!-- HTML -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=yes">
</head>
<body>
<div class="c">
<img src="https://i.stack.imgur.com/P5N3A.jpg" />
<div>text1</div>
</div>
</body>
</html>
这篇关于img 设置 flex-grow 填充 flex 容器剩余空间,导致 flex 内部溢出 flex 容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:img 设置 flex-grow 填充 flex 容器剩余空间,导致 flex 内部溢出 flex 容器


基础教程推荐
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01