【问题标题】:Absolute positioned span automatically gets pushed off the webpage绝对定位跨度自动被推离网页
【发布时间】:2021-10-19 16:25:13
【问题描述】:

这是单页网站的第三部分,每个部分都占据视口的总高度和宽度。 div 'gun' 中的 span 元素在 Firefox 浏览器中显示为下推,创建了一个空白,而在 Edge 中看起来一切都很好。我认为这是因为相对和绝对定位(这是在回答几个空白问题时提到的),但我不知道如何解决它。

这里只是页面的那部分的 HTML('end' div 是网站整个第三部分的包装):

* {
  margin: 0;
  padding: 0;
  border: 0;
  outline: 0;
  box-sizing: border-box;
  /* Hide scrollbar for IE, Edge and Firefox */
  -ms-overflow-style: none;
  /* IE and Edge */
  scrollbar-width: none;
  /* Firefox */
}


/* Hide scrollbar for Chrome, Safari and Opera */

body::-webkit-scrollbar {
  display: none;
}


/*Ending */

.end {
  background-color: pink;
  width: 100%;
  height: 100vh;
  position: relative;
}


/* Gun and company */

.gun img {
  position: absolute;
  width: 38%;
  height: auto;
  object-fit: contain;
  transform: rotate(75deg) translate(20rem, -24.5rem);
  filter: sepia(0.8);
}

.gun-shadow {
  position: absolute;
  top: 32rem;
  left: 46rem;
  width: 100px;
  /*80 px, probably*/
  height: 370px;
  /*385px*/
  background-image: linear-gradient(-90deg, #ffbebe, #fffaf0);
  border-radius: 100%;
  transform: rotate(90deg);
}

.gun-shadow2 {
  position: absolute;
  top: 34.3rem;
  left: 46.5rem;
  width: 80px;
  /*60px*/
  height: 285px;
  /*300px*/
  background-color: #202b3a;
  border-radius: 100%;
  transform: rotate(90deg);
}

.spotlight {
  position: absolute;
  top: 5.8rem;
  left: 32.75rem;
  width: 530px;
  height: 600px;
  background-image: linear-gradient(-0deg, #202b3a, transparent);
  clip-path: polygon(0 0, 100% 0, 63.85% 150%, 33.35% 150%);
  /* polygon(0 0, 100% 0, 80% 100%, 20% 100%) */
  opacity: 0.4;
}

.gun-shadow2::after {
  width: 80px;
  height: 285px;
  background: fuchsia;
  border-radius: 50%;
}
<div class="end">
  <div class="gun">
    <img src="https://cdn.pixabay.com/photo/2013/07/13/11/52/pistol-158868_960_720.png" alt="the gun">
    <span class="gun-shadow"></span>
    <span class="gun-shadow2"></span>
    <span class="spotlight"></span>
  </div>
  <div class="footer">
    <div class="social-icons1">
      <ul>
        <li>
          <a href="#" class="fas fa-at"></a><span class=" content content1">Click to Email</span>
        </li>
        <li>
          <a href="#" class="fas fa-envelope-open"></a><span class=" content content2">We do have a mailbox</span>
        </li>
        <li>
          <a href="#" class="fas fa-phone"></a><span class=" content content3">Call us maybe</span>
        </li>
      </ul>
    </div>

代码可能有几个问题。如果您需要任何其他信息来了解情况,请告诉我。谢谢。

边缘版本:

火狐版本:

【问题讨论】:

  • 我看到您使用相对单位来定位绝对元素。当您执行此操作时,您通常会遇到位于正常文档流之外的元素的意外行为和副作用。如果我需要将一个元素定位在包含元素的右下角,我会使用绝对测量单位 (px) 并将它们定位为 rightbottom 而不是 topleft。您的元素将始终以这种方式相对于包含元素进行定位和响应。为了更好地衡量,我也会考虑在包含元素上添加溢出。
  • 你能不能把它应该看起来像我在 Edge 和 FF 上看到的(几乎)一样的图像,虽然必须在 Edge 上全屏 F11 才能看到任何东西底端。而且我看不到紫红色。
  • @UncaughtTypeError 谢谢你的解释。我对如何在包含元素上添加溢出有点困惑。如果可能的话,你能在我的代码中指出它吗?不管怎样,再次感谢!
  • @AHaworth 我为浏览器添加了两个屏幕截图。而我的“紫红色”实际上是背景颜色的名称。我添加它们是为了方便使用而不是十六进制代码(因为那时我必须查找十六进制代码),所以是的。感谢您查看此内容。

标签: html css firefox css-position removing-whitespace


【解决方案1】:

最快的解决方法是在 .gun-shadow.gun-shadow2.spotlight 中将 2rem 属性从 top 减少。

另一种方法是将top 属性更改为bottom 属性并为其指定一个数字以将其放置在您想要的任何位置。 试试这种风格:

.gun-shadow {
    position: absolute;
    bottom: -128px;
    left: 46rem;
    width: 100px;
    /*80 px, probably*/
    height: 370px;
    /*385px*/
    background-image: linear-gradient(-90deg, #ffbebe, #fffaf0);
    border-radius: 100%;
    transform: rotate(90deg);
}

.gun-shadow2 {
    position: absolute;
    bottom: -85px;
    left: 46.5rem;
    width: 80px;
    /*60px*/
    height: 285px;
    /*300px*/
    background-color: #202b3a;
    border-radius: 100%;
    transform: rotate(90deg);
}

.spotlight {
    position: absolute;
    bottom: 61px;
    left: 32.75rem;
    width: 530px;
    height: 600px;
    background-image: linear-gradient(-0deg, #202b3a, transparent);
    clip-path: polygon(0 0, 100% 0, 63.85% 150%, 33.35% 150%);
    /* polygon(0 0, 100% 0, 80% 100%, 20% 100%) */
    opacity: 0.4;
}

一个非常重要的事情是使用像 px 这样的绝对单位而不是 rem

【讨论】:

  • 好的,这真的很顺利!非常感谢——我只是用你的代码替换了我的代码。至于 rem 和 px,我想我在 YT 视频中看到,rem 比 px 更灵活,我猜是测量。这就是为什么我一直在到处使用它。也感谢您的提示和回答!
  • 很高兴为您提供帮助,感谢您的客气话。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多