使图像全宽



我正在尝试将图像放入我的头元素中。当我运行Live Server时,图像不会填充容器的整个宽度。

编辑 图像的尺寸为899.875X600

这是我的CSSHTML代码:

* {
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>

* {
margin: 0;
padding: 0;
}
body {

color: white;    
height: calc(100vh - 4px);  
border:solid 2px red;
z-index:5;
}
header {    

height:30vh;
background: url('https://via.placeholder.com/100');
background-repeat: no-repeat;
background-size: cover;
}
<body>
<header>

</header>
</body>

最新更新