简体   繁体   English

如何使我的垂直导航栏水平

[英]How can I make my vertical navigation bar horizontal

I recently made a vertical navigation bar and was wondering how I could make it horizontal and make it display in the same sized boxes equally spaced apart我最近制作了一个垂直导航栏,想知道如何让它水平并使其显示在相同大小的盒子中等距

I have tried to float text to the left but that doesn't work as well as followed W3 School tutorial on creating a horizontal navigation bar.我曾尝试将文本向左浮动,但这并不像 W3 School 教程中关于创建水平导航栏的那样有效。 W3 School isn't useful. W3 学校没有用。 I am a HTML and CSS newbie.我是 HTML 和 CSS 新手。

 ul { list-style-type: none; margin: 0px; padding: 0px; overflow: hidden; width: 100px; border: 1px solid; } li a { display: block; color: #000000; padding: 8px 16px; text-decoration: none; text-align: center; border: 1px; border-bottom: 1px solid; } li:last-child a { border-bottom: none; } li a:hover { background-color:red; color:white; }
 <ul> <li><a href="index.html">Home</a></li> <li><a href="about.html">About</a></li> <li><a href="services.html">Services</a></li> <li><a href="contact.html">Contact</a></li> <li><a href="#">LinkedIn</a></li> </ul>

I expect a horizontal navigation bar that is centred and equally spaced apart in the same sized boxes that appear.我希望有一个水平导航栏,它在出现的相同大小的框中居中且间隔相等。

Start changing display property and add it to li element like the following snippet.开始更改 display 属性并将其添加到li元素,如下面的代码片段所示。 You must play with the values and the properties to achieve what you need.您必须使用值和属性来实现您的需要。

 ul { font-size: 0; list-style-type: none; margin: 0px; padding: 0px; overflow: hidden; border: 1px solid; } ul li { display: inline-block; list-style-type: none; width: 20%; font-size: 1rem; } li a { color: #000000; padding: 8px 16px; text-decoration: none; text-align: center; border: 1px; border-bottom: 1px solid; } li:last-child a { border-bottom: none; } li a:hover { background-color:red; color:white; }
 <ul> <li><a href="index.html">Home</a></li> <li><a href="about.html">About</a></li> <li><a href="services.html">Services</a></li> <li><a href="contact.html">Contact</a></li> <li><a href="#">LinkedIn</a></li> </ul>

Use flex .使用flex You can read more about it here .您可以在此处阅读更多相关信息。

 ul { list-style-type: none; display: flex; align-items: center; justify-content: center; margin: 0px; padding: 0px; width: 400px; border: 1px solid; } li a { display: block; color: #000000; padding: 8px 16px; text-decoration: none; text-align: center; border: 1px; border-bottom: 1px solid; } li:last-child a { border-bottom: none; } li a:hover { background-color: red; color: white; }
 <ul> <li><a href="index.html">Home</a></li> <li><a href="about.html">About</a></li> <li><a href="services.html">Services</a></li> <li><a href="contact.html">Contact</a></li> <li><a href="#">LinkedIn</a></li> </ul>

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

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