我试图把一个伪元素作为背景图像,只是在文档的宽度裁剪。但是,当我将背景图像放入其中时,会不断扩大文档的宽度,使其他div变得非常宽泛。如何阻止伪元素改变其他元素的宽度?
具体来说,您可以从截图中看到,nav正在扩展其宽度以适应伪元素的背景图像。
我见过它做,但我不同的是没有什么,因为在实际的伪元素的代码是一样的。举个例子:
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;
}
}
谢谢Frits。为了澄清我自己和其他人,为什么使用html/body作为overflow-x:hidden;的CSS选择器的快速修复和矫枉过正,为什么它应该是那些选择器而不是简单的伪元素的父对象呢? – Chris
Hi @Chris。这是过度杀伤,因为如果你的身体有'overflow-x:hidden';'它应该永远不会超过'html',除非你专门有一个水平排列的网站 - 这有帮助吗?至于你的第二个问题,最好在父元素上使用'overflow-x:hidden',但只有当你的父元素不小于你试图用你的伪元素访问的区域时。通常,伪元素上的样式是以这样的方式完成的:故意突出父元素,但仍需要确认文档大小。这有帮助吗? :) – Frits
@Chris - 不要忘记看看[你的赏金问题](https://stackoverflow.com/a/44457687/6049581) - 问题似乎一样,但随时让我知道如果我遗漏了任何东西:) – Frits