简体   繁体   English

CSS 网格 - 如何均匀对齐我的菜单项?

[英]CSS Grid - How can I align my menu items evenly?

So, I noticed that my menu items looked weird because some of the links are closers together than others due to the nav menu item having more letters.所以,我注意到我的菜单项看起来很奇怪,因为导航菜单项有更多字母,因此一些链接比其他链接更接近。

I'm not sure what CSS I could add to ensure that they are all perfectly taking up the same space, while maintaining an even look.我不确定我可以添加什么 CSS 以确保它们都完美地占用相同的空间,同时保持均匀的外观。 I'll include a picture below of what the problem looks like我将在下面附上一张图片,说明问题的样子

在此处输入图像描述

Here is my code so far and as you can see the grid-template-columns perfectly space them 1fr each, but if you see "Home" is perfectly centered, but "about us" and "contact us" have much less space to work with and ends up making the menu items look like they aren't aligned properly.到目前为止,这是我的代码,您可以看到网格模板列完美地将它们间隔 1fr,但是如果您看到“主页”完全居中,但“关于我们”和“联系我们”的工作空间要少得多并最终使菜单项看起来没有正确对齐。

 .nav-menu {
display: grid;
grid-template-columns: repeat(4, 1fr);
list-style: none;
text-align: center;

}

.nav-links {
color: white;
text-decoration: none;
transition: color 0.3s ease-out;
}

Is there a way to fix this?有没有办法来解决这个问题?

You may use justify-content and auto instead 1fr .您可以使用justify-contentauto代替1fr

example:例子:

 ul, li { margin: 0; padding: 0; } ul { border: solid; display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; width: 356.84px; margin-bottom:1em; } /* Possible Update */ ul+ul { grid-template-columns: repeat(4, auto); justify-content: space-around; } li { list-style: none; box-shadow: 0 0 0 1px; } a { background: lightblue; }
 <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">Service</a></li> <li><a href="#">About us</a></li> <li><a href="#">Contact Us</a></li> </ul> <ul> <li><a href="#">Home</a></li> <li><a href="#">Service</a></li> <li><a href="#">About us</a></li> <li><a href="#">Contact Us</a></li> </ul> </nav>

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

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