【问题标题】:Need help putting button on top of carousel img需要帮助将按钮放在轮播 img 顶部
【发布时间】:2021-11-14 16:22:09
【问题描述】:

我在轮播中的图像当前覆盖了我的按钮。任何有关让按钮位于图像顶部的帮助将不胜感激。

carousel.component.html

<div id="carouselExampleIndicators" class="carousel slide" data-bs-ride="carousel">
  <div class="carousel-indicators">
    <button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button>
    <button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="1" aria-label="Slide 2"></button>
    <button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="2" aria-label="Slide 3"></button>
  </div>
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="assets/myimg8.jpg" class="d-block w-100 myImg" alt="...">
      <button type="button" class="btn btn-dark myButton">Dark</button>
    </div>
    <div class="carousel-item">
      <img src="assets/myimg.jpg" class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item">
      <img src="assets/myimg6.jpg" class="d-block w-100" alt="...">
    </div>
  </div>
  <button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="visually-hidden">Previous</span>
  </button>
  <button class="carousel-control-next" type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="visually-hidden">Next</span>
  </button>
</div>

carouel.component.scss

.carousel-item{
  position: relative;
}

.carousel-item button {
  position: absolute;
}

【问题讨论】:

  • 在按钮上使用大于image 或文档中任何z-index 值的z-index
  • 我尝试在按钮高于图像的位置添加 z-index,但这也不起作用
  • 如果您提到position: absolute,您必须使用top, bottom, left, right 中的任一属性。试试给bottom: 0
  • 这对 Shashank Gb 有效。非常感谢。

标签: html css sass


【解决方案1】:

实际上,您的问题不在于 z-index 属性。 替换

position: absolute;

position: relative;

你会再次看到它。

现在,一旦使用了相对值,您就可以使用以下属性之一:topleftbottomright

如果您希望按钮位于图像顶部,解决方案可能是这样的:

.carousel-inner button {
  position: relative;
  top: -7em;
}

【讨论】:

  • 如果我这样做,它只是将按钮放在图像下方而不是在图像顶部
  • 我测试了你的代码,它对我有用。尝试使用 z-index 作为按钮...例如 z-index: 1000;或更高的值。
  • 这确实有效并将按钮放在图像的顶部,但它会从按钮创建一整行空白。你对如何摆脱它有什么建议吗?我是开发新手,非常感谢您的帮助。
  • 我当然会帮助你。请给我发一张你在说什么的截图,告诉我你现在到底想要什么。
猜你喜欢
  • 2018-04-29
  • 1970-01-01
  • 2015-10-25
  • 2015-09-23
  • 1970-01-01
  • 2020-11-12
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多