简体   繁体   English

如何调整导航栏按钮的大小并使其适合屏幕宽度?

[英]How do I resize the buttons of a navigation bar and make it fit the width of the screen?

Can someone please help me with my Navigation Bar?有人可以帮我使用导航栏吗? I have been trying to research how to create one myself, but the buttons seem to end up small, and the bar itself won't stretch to the entirety of the page despite the width 100% snippet.我一直在尝试研究如何自己创建一个,但按钮似乎最终变小了,尽管片段width 100% ,但栏本身不会延伸到整个页面。 Thanks in advance.提前致谢。

 .navBar { width: 100% }.navBar ul { list-style-type: none; } /* Styling of buttons on the NavBar */.navBar ul a { float: left; background-color: #202020; color: #ffffff; padding-bottom: 12px; display: block; text-decoration-line:none; font-family: 'Montserrat', sans-serif; size: 20px; } /* Styling of NavBar buttons when mouse hovers over it */.navBar ul a:hover { background-color: #006633
 <div Class=navBar>Test <ul>test1</ul> <ul>test2</ul> <ul>test3</ul> </div>

You're looking for CSS relative units .您正在寻找CSS 相对单位

If you want to make an element match the total width of the screen (specific wording here) then you want to use width: 100vw;如果你想让一个元素匹配屏幕的总宽度(这里有具体的措辞)那么你想使用width: 100vw; . . If the element is not rendered at the left (or right) most pixel it would overflow past the screen.如果元素没有在最左边(或右边)的像素处呈现,它将溢出屏幕。 Using width: 100%;使用width: 100%; would have the same effect only it's relative to it's parent element, not relative to the total width of the screen.只有相对于它的父元素,而不是相对于屏幕的总宽度,才会产生相同的效果。

It is good practice to add CSS Resets to the top of your CSS styles.最好将 CSS 重置添加到 CSS styles 的顶部。 Web browsers have default margins and padding which can prevent your divs from extending the full width of the screen. Web 浏览器具有默认的边距和填充,可以防止您的 div 扩展屏幕的整个宽度。

Add this to the top your CSS Stylesheet.将此添加到您的 CSS 样式表的顶部。

*,
*::before,
*::after {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

This code will reset the browser default margins and padding.此代码将重置浏览器默认边距和填充。 Your navbar should then take up the full page width when you use .navBar { width: 100%;}当您使用.navBar { width: 100%;}时,您的导航栏应该占据整个页面宽度

For your buttons you can add more padding until you are satisfied.对于您的按钮,您可以添加更多填充,直到您满意为止。 For example:例如:

.navBar ul a {
  padding: 10px 30px;
}

This will give your buttons 10px padding on the top and bottom and 30px padding left and right.这将为您的按钮在顶部和底部提供 10 像素的填充,在左右提供 30 像素的填充。

As mentioned above, please select the answer that was most useful to you.如上所述,请 select 对您最有用的答案。

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

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