【问题标题】:How to stop a pseudo element from changing the width of other elements?如何阻止伪元素改变其他元素的宽度?
【发布时间】:2017-11-09 14:15:26
【问题描述】:

我正在尝试将伪元素作为背景图像,它只是以文档的宽度进行裁剪。但是,当我将背景图片放入其中时,它会不断扩大文档的宽度,使其他一些 div 变得非常宽。

具体来说,您可以从屏幕截图中看到导航正在扩展其宽度以容纳伪元素的背景图像。

我的尝试:

我已经看到它完成了,但不是什么区别,因为实际伪元素中的代码是相同的。一个例子:

HTML:

    <nav class="navbar navbar-default navbar-fixed-top navbar-custom">
        <div class="container nav-down">
            <div class="navbar-header page-scroll">
                <a class="navbar-brand" href="/"><img src="/static/ScoopsLogo.png" alt="${out.global.siteName} Logo"></a>
                <p class="navbar-blog"><a href="https://blog.scoops.io/">Blog</a></p>
            </div>
        </div>
        <div id="scroll-progress">
            <div id="scroll-progress-bar"></div>
        </div>
    </nav>
    <section id="home">
        <div class="home-1">
          <div class="home1-1">
            <h1>Sound smart at the dinner table</h1>
            <h5>Learning made easy & fun</h5>
          </div>
        </div>
        <div class="home-lessons fade-in-up-static delay-2">
            <!-- List of Lessons -->
            <h5 class="text-center">NEW SCOOPS</h5>
            <div class="lessons-list">
            </div>
        </div>
     </section>

CSS:

.navbar {
    min-height: 50px;
    margin-bottom: 20px;
}
.navbar-default {
    background-color: #f8f8f8;
    border-color: #e7e7e7;
}
.navbar-fixed-top {
    top: 0;
    border-width: 0 0 1px;
}
.container {
    max-width: 600px;
}
.navbar-custom {
    .navbar-header {
      width: inherit;
      max-width: inherit;
    }
    .navbar-brand {
        img {
          width: auto;
          height: 100%;
        }
    }
    .navbar-blog {
      float: right;
      padding: 10px;
      margin: 3px 10px 0 0;
    }
}
.home-lessons {
    width: 100%;
    padding: 3%;
    @media (min-width: $screen-md-min) {
      margin-bottom: 20px;
      padding: 20px 40px;
    }
    h5 {
      margin-top: -100px;
    }
    &::before {
      content: '';
      background-image: url(/static/Scoops-Icons.png);
      background-size: cover;
      width: 500px;
      height: 500px;
      display: block;
      position: absolute;
      margin-left: 200px;
      margin-top: -200px;
    }
  }

【问题讨论】:

    标签: html css background-image css-position pseudo-element


    【解决方案1】:

    我刚刚回答了一个与you put a bounty on over here 非常相似的问题。在这种情况下,我认为您最好的选择是对带有背景的伪元素强制设置最大宽度,或者在文档中删除 overflow-x

    下面是你的 CSS 的截图,并修复了:

    .home-lessons {
        width: 100%;
        padding: 3%;
        @media (min-width: $screen-md-min) {
          margin-bottom: 20px;
          padding: 20px 40px;
        }
        h5 {
          margin-top: -100px;
        }
        &::before {
          content: '';
          background-image: url(/static/Scoops-Icons.png);
          background-size: cover;
          width: 500px;
          height: 500px;
          display: block;
          position: absolute;
          margin-left: 200px;
          margin-top: -200px;
          max-width: calc(100% - 200px); /* HERE IS THE FIX */
        }
    }
    

    如果这不起作用,快速解决办法是这样做:

    html {
        overflow-x: hidden;
    }
    

    我看到有些人使用 html, body 作为他们的 CSS 选择器稍微有点过头了,这取决于你。

    【讨论】:

    • 谢谢弗里茨。为了澄清我自己和其他人,为什么使用 html/body 作为 overflow-x:hidden; 的 CSS 选择器是一种快速修复和过度杀伤力,为什么它应该是那些选择器而不是伪元素的父级?
    • 嗨@Chris。这是矫枉过正,因为如果你的身体有overflow-x: hidden;,它不应该超过html,除非你特别有一个水平排列的网站——这有帮助吗?至于您的第二个问题,最好在父元素上使用overflow-x:hidden,但前提是您的父元素不小于您尝试使用伪元素到达的区域。通常情况下,伪元素的样式会故意超出父元素,但仍需要确认文档大小。这有帮助吗? :)
    • @Chris - 不要忘记查看your bounty question - 问题似乎相同,但如果我遗漏了任何内容,请随时告诉我:)
    【解决方案2】:

    父元素应使用position: relative,而子元素应使用top: 0left: 0

    像这样:

    .parent {
        position: relative;
    }
    .child {
        top: 0;
        left: 0;
    }
    

    【讨论】:

    • 试过了 - position:relative 在元素上,top: 0, left: 0 on pseudo。仍在扩展页面的宽度。溢出:隐藏;有效,但是它也会掩盖一个我不想掩盖的不同子 div。
    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多