html - 随着显示变小,我的背景图像在标题中缩小

标签 html css

我的问题是随着显示变小,我的背景图像在标题中缩小,我希望我的图像填充(而不是拉伸(stretch))我的整个标题。

enter image description here

enter image description here

正如你在两张图片中看到的那样,我的代码运行完美,直到我将浏览器大小减小到手机大小,我的图片开始缩小

enter image description here

我希望我的图像覆盖(填充而不是拉伸(stretch))整个标题

enter image description here

这是我的代码:-

html:-

  <header>
        <img src="images/index/header/header.jpg">
  </header>

CSS:-

header {
    width:100%; 
    height:320px; 
    position:relative;
    overflow:hidden; 
    z-index:-1;
    border:1px solid lightgreen;
    object-fit: fill;
    background-position: center center;
    display: flex;


}

header img {
    width:100%;
    background-size: cover;
    margin: 0 auto;
}

最佳答案

在图像标签中使用 position:absolute 和 height:100%,例如:-

header img {
    position:absolute;
    width:100%;
    height:100%;
    background-size: cover;
    margin: 0 auto;
}

您也可以在背景中使用图像而不是标题。

header {
    width:100%; 
    height:320px; 
    position:relative;
    overflow:hidden; 
    z-index:-1;
    border:1px solid lightgreen;
    background-position: center center;
    display: flex;
    background-image:url("images/index/header/header.jpg");
    background-size: cover;


}

关于html - 随着显示变小,我的背景图像在标题中缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48801227/

相关文章:

php - 修剪面包屑

html - 为什么一级子菜单和二级菜单之间有间隙?

html - 单击按钮时 UIWebView 打开相机和图像

用于 WP8 (Lumia 920) 设备的 jQuery/HTML5/gwt 应用程序 : vertical css scroll fix

html - 如何控制我的 Chart.JS 饼图图例的位置及其外观?

javascript - 针对滚动起源滚动图像 - 如何?

html - 当 div 穿过特定位置时触发动画

html - 让图像以移动页面 View 为中心

html - IE 始终以 TD 居中文本

javascript - Bootstrap 3方 block 展示