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="aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9QNU4zQS5qcGc=" />
<div>text1</div>
</div>
</body>
</html>
这篇关于img 设置 flex-grow 填充 flex 容器剩余空间,导致 flex 内部溢出 flex 容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:img 设置 flex-grow 填充 flex 容器剩余空间,导致 flex 内部溢出 flex 容器
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Flexslider 箭头未正确显示 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01