【问题标题】:Trying to get my banner image to span the width of the page试图让我的横幅图像跨越页面的宽度
【发布时间】:2018-10-17 10:30:54
【问题描述】:

我正在创建一个博客,并试图在导航栏下方获取一张图片。我有四个使用 css 和 js 滑动的图像。但是,无论我如何尝试,我都无法让图像跨越页面的宽度。我已经尝试了 100% 的宽度、vw 等。没有什么能按我想要的方式工作。这是html和css(记住我一直在玩css)

 #stage {
margin: 1em auto;
width: 100%;
height: 292px;
margin-bottom: 40px;
}

#stage a {
    position: absolute;
  }
  #stage a img {
    padding: 10px;
    border: 1px solid #ccc;
    background: #fff;
  }

  #stage a:nth-of-type(1) {
    animation-name: fader;
    animation-delay: 4s;
    animation-duration: 1s;
    z-index: 20;
  }
  #stage a:nth-of-type(2) {
    z-index: 10;
  }
  #stage a:nth-of-type(n+3) {
    display: none;
  }

  @keyframes fader {
    from { opacity: 1.0; }
    to   { opacity: 0.0; }
  }

这里是html

<div id="stage">
<a><%= image_tag "2.jpeg" %></a>
<a><%= image_tag "1.jpeg" %></a>
<a><%= image_tag "3.png" %></a>
<a><%= image_tag "4.jpeg" %></a>
</div>

【问题讨论】:

  • 将图像设置为div的背景,然后使用background-size: cover将其拉伸到div的宽度。您也可以使用background-position: center 将其居中。

标签: html css ruby-on-rails


【解决方案1】:

希望这会有所帮助。我已经记录了 CSS 代码中的更改。请注意,根据实际图像,它可能会被夸大。

#stage {
  margin: 1em auto;
  width: 100%;
  height: 292px;
  margin-bottom: 40px;
}

#stage a {
  position: absolute;
  width: inherit; /* Added */
  height: inherit; /* Added */
}

#stage a img {
  padding: 10px;
  border: 1px solid #ccc;
  background: #fff;
  width: inherit; /* Added */
  height: inherit; /* Added */
}

#stage a:nth-of-type(1) {
  animation-name: fader;
  animation-delay: 4s;
  animation-duration: 1s;
  z-index: 20;
}

#stage a:nth-of-type(2) {
  z-index: 10;
}

#stage a:nth-of-type(n+3) {
  display: none;
}

@keyframes fader {
  from {
    opacity: 1.0;
  }
  to {
    opacity: 0.0;
  }
}
<div id="stage">
  <a>
    <img src="https://via.placeholder.com/100x100/000000">
  </a>
  <a>
    <img src="https://via.placeholder.com/100x100/ff0000">
  </a>
  <a>
    <img src="https://via.placeholder.com/100x100/00ff00">
  </a>
  <a>
    <img src="https://via.placeholder.com/100x100/0000ff">
  </a>
</div>

【讨论】:

  • 非常感谢您花时间回答。也许图像的大小是问题。您建议使用什么尺寸(我知道其中一个超过 7mb),并且在第一张图片将第二张、第三张和第四张稍微向右滑动之后。你知道这是为什么吗?
  • 没关系。我把最后一部分整理好了。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-29
相关资源
最近更新 更多