html - 使图像全宽
问题描述
我正在尝试将图像放入我的标题元素中。当我运行 Live Server 时,图像不会填满容器的整个宽度。
编辑 图片尺寸为899.875X600
这是我的CSS和HTML代码:
* {
margin: 0;
padding: 0;
}
body {
width: 100%;
height: 100%;
color: white;
min-width: 100%;
min-height: 100%;
height: auto;
top: 0;
left: 0;
position: absolute;
}
header {
min-height: 100%;
min-width: 1024px;
width: auto;
height: 600px;
}
img {
max-width: 100%;
max-height: 100%;
}
<body>
<header>
<img id='backgroundImg' src="image.jpg" alt="">
</header>
</body>
解决方案
我强烈建议你不要在你的身体上有绝对位置。为此使用100vh
单位。它还取决于您希望如何在标头中进行图像处理,您的问题并不清楚。这是你想要的吗?
* {
margin: 0;
padding: 0;
}
body {
min-height: 100vh;
}
header {
width: 100%;
height: 600px;
background-color: aqua;
padding: 1rem;
}
img {
display: block;
object-fit: cover;
width: 100%;
height: 100%;
}
<body>
<header>
<img id='backgroundImg' src="https://source.unsplash.com/random" alt="image">
</header>
</body>
推荐阅读
- java - DI 将 String bean 作为 List 的单个元素注入
而不是正确的列表 豆 - excel - 有条件地取消隐藏 Excel/vba 中的工作表
- vue.js - Vue.js UI 中的插件和依赖项有什么区别?
- reactjs - 如何编写测试用例来覆盖承诺链中所有嵌套的“then”回调
- c++ - 嵌套类:具有外部类成员的内部类
- javascript - 图像选择器 Html
- facebook - Facebook 页面可以在没有用户先向页面发送消息的情况下向用户发送消息吗?
- asp.net-core-webapi - 在 Asp.Net Core 2.2 中动态更改 DBContext 连接字符串
- android - Android Spinner 在点击时不显示项目
- c# - for-each 循环中的 Dbcontext.Savechanges(),最佳实践