微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

img 没有拉伸到包装器的高度和宽度

如何解决img 没有拉伸到包装器的高度和宽度

我想在常规背景之上添加波浪形背景,但图像并未完全伸展。它必须是绝对位置(否则它会干扰我的导航并将其向下移动 img 的高度)。 JSFiddle。我已经尝试过 object-fit: fill 也没有奏效。提前致谢

我的代码

* {
  margin: 0;
  padding: 0;
  font-family: helvetica;
}

body {
  height: 5000px;
}

#background {
  position: absolute;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: ;
  opacity: 15%;
}

#navwrapper {
  background: linear-gradient(250deg,#0061ff,#60efff);
  background-size: 400% 400%;

  -webkit-animation: AnimationName 10s ease infinite;
  -moz-animation: AnimationName 10s ease infinite;
  animation: AnimationName 10s ease infinite;
  height: 100vh;
}

@-webkit-keyframes AnimationName {
  0% {
    background-position: 0% 7%
  }

  50% {
    background-position: 100% 94%
  }

  100% {
    background-position: 0% 7%
  }
}

@-moz-keyframes AnimationName {
  0% {
    background-position: 0% 7%
  }

  50% {
    background-position: 100% 94%
  }

  100% {
    background-position: 0% 7%
  }
}

@keyframes AnimationName {
  0% {
    background-position: 0% 7%
  }

  50% {
    background-position: 100% 94%
  }

  100% {
    background-position: 0% 7%
  }
}
<nav id="navwrapper">
       <div id="background"><img src="https://i.postimg.cc/RZzwCTQz/Zeichenfla-che-92.png"></div>

</nav>

解决方法

不是将 css 应用到 #background,而是将其应用到 #background img 中,这就是您要更改对象适合属性的地方。

替换这个

#background {
  position: absolute;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: ;
  opacity: 15%;
}

有了这个

#background img{
  position: absolute;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 15%;
}

您可以查看下面的输出

* {
  margin: 0;
  padding: 0;
  font-family: helvetica;
}

body {
  height: 5000px;
}

#background img {
  position: absolute;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: ;
  opacity: 15%;
}

#navwrapper {
  background: linear-gradient(250deg,#0061ff,#60efff);
  background-size: 400% 400%;
  -webkit-animation: AnimationName 10s ease infinite;
  -moz-animation: AnimationName 10s ease infinite;
  animation: AnimationName 10s ease infinite;
  height: 100vh;
}

@-webkit-keyframes AnimationName {
  0% {
    background-position: 0% 7%
  }
  50% {
    background-position: 100% 94%
  }
  100% {
    background-position: 0% 7%
  }
}

@-moz-keyframes AnimationName {
  0% {
    background-position: 0% 7%
  }
  50% {
    background-position: 100% 94%
  }
  100% {
    background-position: 0% 7%
  }
}

@keyframes AnimationName {
  0% {
    background-position: 0% 7%
  }
  50% {
    background-position: 100% 94%
  }
  100% {
    background-position: 0% 7%
  }
}
<nav id="navwrapper">
  <div id="background"><img src="https://i.postimg.cc/RZzwCTQz/Zeichenfla-che-92.png"></div>

</nav>

,

使用此属性

#background{
      background:url(...);
      background-position: center !important;
      background-size: cover !important;
      background-repeat: no-repeat;
      height: 500px;
      width:100%
      }
,

您可以将 widthheight 设置为 100%,以便它应该包含容器的所有空间:

#background img { 
  width: 100%;
  height: 100%; 
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?