简体   繁体   English

如何摆脱 CSS 中的双垂直滚动条?

[英]How do I get rid of double vertical scroll bars in CSS?

I added a scroll effect by doing this:我通过这样做添加了滚动效果:

 section { background: #1d1c1c; height: 100vh; display: flex; align-items: center; justify-content: center; scroll-snap-align: start; } .container { scroll-snap-type: y mandatory; overflow-y: scroll; height: 100vh; }
 <div class="container"> <section class="about"> <div class="about-container"> <h1>about</h1> </div> </section> <section class="projects"> <div class="project-container "> <h1>projects</h1> </div> </section> <section class="contact"> <div class="contact-container"> <h1>contact</h1> </div> </section> </div>

it worked but a second scroll bar showed up.它起作用了,但出现了第二个滚动条。 I tried using overflow-y: scroll hidden;我尝试使用overflow-y: scroll hidden; but that caused the scroll effect to stop working.但这导致滚动效果停止工作。 I am new to coding so any help would be very much appreciated!我是编码新手,因此非常感谢任何帮助!

Add The following code to your CSS将以下代码添加到您的 CSS

.container::-webkit-scrollbar {
    width: 0!important;  /* Remove scrollbar space */
    background: transparent;  /* Optional: just make scrollbar invisible */
}

.container::-webkit-scrollbar-thumb {
    background: transparent; /* Optional: just make scrollbar thumb invisible */
}

@-moz-document url-prefix() {
  .container {
    scrollbar-width: none;
    scrollbar-color: transparent;
  }
}

Scrollbar is visible, when content is bigger than window.当内容大于窗口时,滚动条可见。 You can small decrease .container height to remove second scrollbar:您可以小幅降低 .container 高度以删除第二个滚动条:

 section { background: #1d1c1c; height: 100vh; display: flex; align-items: center; justify-content: center; scroll-snap-align: start; } .container { scroll-snap-type: y mandatory; overflow-y: scroll; height: 95vh; /* CHANGED */ }
 <div class="container"> <section class="about"> <div class="about-container"> <h1>about</h1> </div> </section> <section class="projects"> <div class="project-container"> <h1>projects</h1> </div> </section> <section class="contact"> <div class="contact-container"> <h1>contact</h1> </div> </section> </div>

If you want an item to be the maximum height of your screen, use min-height: 100vh;如果您希望某个项目成为屏幕的最大高度,请使用min-height: 100vh; instead height: 100vh;相反height: 100vh;

This will set the minimum height equals to your screen height but if the content needs more vertical space, it will let the parent element to grow enough to match the child element needs.这将设置最小高度等于您的屏幕高度,但如果内容需要更多的垂直空间,它将让父元素增长到足以匹配子元素的需要。

By the way in this case you don't need to set neither a min-height nor height to the parent element (.container), because it will grow to match the sum of its child elements (section)s height by default.顺便说一下,在这种情况下,您不需要为父元素 (.container) 设置min-heightheight ,因为默认情况下它会增长以匹配其子元素(部分)的高度总和。

 section { min-height: 100vh; display: flex; align-items: center; justify-content: center; } .about { background: chocolate; } .projects { background: burlywood; } .contact { background: bisque; }
 <div class="container"> <section class=" about "> <div class="about-container "> <h1>about</h1> </div> </section> <section class="projects "> <div class="project-container "> <h1>projects</h1> </div> </section> <section class="contact "> <div class="contact-container "> <h1>contact</h1> </div> </section> </div>

And your second scrollbar will be there if you use overflow-y: scroll;如果你使用overflow-y: scroll;你的第二个滚动条就会在那里overflow-y: scroll; because it's what it does, so I would suggest you to avoid using it unless it's necessary for design reasons such the items inside the "working with" section of this site: https://joelbonetr.com/因为它就是这样做的,所以我建议您避免使用它,除非出于设计原因,例如本网站“使用”部分中的项目: https : //joelbonetr.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM