html - 仅使用 CSS 将容器 div 调整为低于内容总宽度时,图像不会调整大小
问题描述
当父 div 变得小于内容宽度的总和时,仅使用 CSS 如何调整图像的大小低于其原始宽度和高度。我有一个带有一些图像的 div,如果我调整窗口的大小可以说低于 400 像素,那么图像的大小不会低于其原始大小。在调整浏览器窗口大小时,有什么方法可以自动调整内容图像。我在下面附上了截断的代码:
.container {
width: 100%;
height: 100%;
display: flex;
justify-content: space-between;
max-width: 900px;
max-height: 256px;
border: thin solid silver;
}
img {
height: 100%;
max-height: 256px;
width: auto;
}
body {
margin: 5px;
}
<body>
<div class="container">
<img src="https://img.icons8.com/pastel-glyph/2x/blood-sample.png" />
<img src="https://img.icons8.com/pastel-glyph/2x/blood-sample.png" />
<img src="https://img.icons8.com/pastel-glyph/2x/blood-sample.png" />
</div>
</body>
解决方案
您可以使用vw
代表 的单位viewport width
。因此,假设我们设置为img
width
屏幕尺寸。33.33vw
img
33.33%
body {
margin: 5px;
}
.container {
width: 100%;
height: 100%;
display: flex;
justify-content: space-between;
max-width: 900px;
max-height: 256px;
border: thin solid silver;
}
img {
height: 100%;
width: 33.33vw;
}
<div class="container">
<img src="https://img.icons8.com/pastel-glyph/2x/blood-sample.png" />
<img src="https://img.icons8.com/pastel-glyph/2x/blood-sample.png" />
<img src="https://img.icons8.com/pastel-glyph/2x/blood-sample.png" />
</div>
推荐阅读
- javascript - CreateAsyncThunk 错误:操作必须是普通对象。使用自定义中间件进行异步操作
- google-apps-script - 如何使用应用程序脚本将相同的条件格式规则应用于 Google 工作表中的多个列?
- python - 在 django 模板中,如何根据正文部分中的自定义标记参数动态更新标题部分
- oauth2-playground - OAuth 2.0:“gmail.send”范围无法验证 smtp.gmail.com 服务器
- javascript - 立即填充第二个下拉值
- php - ibase_execute 更新在 PHP 7.2 中返回 false
- ruby-on-rails - 无法通过开发和生产环境发送邮件验证链接
- ios - 在 swiftUI 2 中向 MapKit 添加 MapMarker
- flutter - 如何设置 Flutter 默认海拔
- c# - 确定上传的文件是否是 asp.net 核心上的图像