html - 我想知道如何使导航中的所有元素均匀分布

标签 html css

我想知道如何使导航中的所有元素均匀分布。这是一个学校元素,我正在创建一个网站,但我无法解决这个问题,我看过的所有解决方案都没有用。

<style>
    div {
      width: 1330px;
      padding: 10px;
      margin: 0; 
      background-color: #212F3D;
      text-align: center;
    }
</style>

最佳答案

您基本上有两个选择,使用 display:flex; , 或 display:inline-block; .

Flex 将确保您的元素以相同的间距端到端显示(请注意示例中的 CSS 规则:justify-content:space-between),而内联 block 可用于居中对齐您的菜单项并手动添加它们的间距和边距。

我建议使用 flex,因为它似乎更适合您的要求,here's a fiddle showing两者的区别,例子如下。

使用 Flex:

.header ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display:flex; /* This is the part to focus on */
  flex-wrap: wrap; /* This is the part to focus on */
  justify-content: space-between; /* This is the part to focus on */
  background-color: rgba(25,57,125, 0.8);
}
.header li {
  display: block;
  border: 1px solid #000000;
  padding: 2px;
}
<div class="header">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3 has a long name</li>
<li>Item 4</li>
<li>One more Item</li>
</ul>


使用内联 block

.header ul {
  list-style: none;
  padding: 0;
  margin: 0;
  text-align: center; /* This is the part to focus on */
  background-color: rgba(125,57,125, 0.8);
}
.header li {
  border: 1px solid #000000;
  padding: 2px;
  display: inline-block; /* This is the part to focus on */
  margin: 0 5px;
}
<div class="header">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3 has a long name</li>
<li>Item 4</li>
<li>One more Item</li>
</ul>

关于html - 我想知道如何使导航中的所有元素均匀分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52942284/

相关文章:

javascript - 如何使 chrome 控制台代码永久化

html - "size"属性在 Linux 上给出不同的结果

c# - Lync 2013 即时消息字体和颜色

html - CSS轮播幻灯片的z-index

CSS 悬停、焦点和 "unfocus"

php & html 表单对齐问题

html - 第 n 个类型的行为不符合预期

html - 更简洁的 CSS 来选择当前元素和子元素

javascript - 在 ul li 中查找所有 a 值

css - 无论如何让标签响应 :focus CSS