简体   繁体   English

在 CSS 中忽略容器的滚动条宽度

[英]Ignore scrollbar width for container in CSS

I want to create a fixed navigation menu at the top of the page with the same width as the other containers.我想在页面顶部创建一个与其他容器具有相同宽度的固定导航菜单。 I can't figure out why the nav container ignores the width of the scrollbar.我无法弄清楚为什么导航容器会忽略滚动条的宽度。 I don't want to permanently enable the bar and I'm afraid adding margins will look weird on different sized viewports.我不想永久启用该栏,而且我担心在不同大小的视口上添加边距会看起来很奇怪。 Here is the code:这是代码:

 :root { --foreground-color: grey; } header { background-color: var(--foreground-color); } nav { background-color: grey; position: fixed; top: 0%; width: 100%; padding: 1em 0% 1em 0%; justify-content: right; display: grid; grid-template-columns: 10% 10% 10% 10%; border-bottom: solid; } .navlink { background-color: dimgrey; } body { background-image: linear-gradient(45deg, black, darkolivegreen); } .title{ margin: 50px 0px 0px 0px; text-align: center; } main { background-color: var(--foreground-color); } h2 { text-align: center; } .center-content-x{ position: fixed; right: 50%; transform: translateX(50%); }
 <header> <nav> <a class="navlink" href="index.html" name="Home">Home</a> <a class="navlink" href="about.html" name="About">About</a> <a class="navlink" href="gallery.html" name="Gallery">Gallery</a> <a class="navlink" href="contact.html" name="Contact Us">Contact Us</a> </nav> <div class="title"> <h1>Sierra Custom Doors</h1> <p>"Your Reputation Is In Our Doors"</p> </div> </header>

I know it's very sloppy but, I'm fairly new to this.我知道这很草率,但是,我对此很陌生。 Any other criticism would be appreciated.任何其他批评将不胜感激。 在此处输入图片说明

I'm not sure if I understand the question correctly but try this我不确定我是否正确理解了这个问题,但试试这个

nav {
background-color: grey;
position: fixed;
top: 0%;
width: calc(100% - 16px); /* <----- THIS LINE */
padding: 1em 0% 1em 0%;
justify-content: right;
display: grid;
grid-template-columns: 10% 10% 10% 10%;
border-bottom: solid;}

Try like this:像这样尝试:

 :root { --foreground-color: grey; } header { background-color: var(--foreground-color); } nav { background-color: grey; position: fixed; top: 0%; left:0.5em; right:0.5em; padding: 1em 0% 1em 0%; justify-content: right; display: grid; grid-template-columns: 10% 10% 10% 10%; border-bottom: solid; } .navlink { background-color: dimgrey; } body { background-image: linear-gradient(45deg, black, darkolivegreen); } .title{ margin: 50px 0px 0px 0px; text-align: center; } main { background-color: var(--foreground-color); } h2 { text-align: center; } .center-content-x{ position: fixed; right: 50%; transform: translateX(50%); }
 <header> <nav> <a class="navlink" href="index.html" name="Home">Home</a> <a class="navlink" href="about.html" name="About">About</a> <a class="navlink" href="gallery.html" name="Gallery">Gallery</a> <a class="navlink" href="contact.html" name="Contact Us">Contact Us</a> </nav> <div class="title"> <h1>Sierra Custom Doors</h1> <p>"Your Reputation Is In Our Doors"</p> </div> </header>

For the nav element, width: 100%;对于nav元素, width: 100%; has been replaced with left: 0.5em; right: 0.5em;已被替换为left: 0.5em; right: 0.5em; left: 0.5em; right: 0.5em; (obviously, just change the values as required). (显然,只需根据需要更改值)。

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

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