如何使用CSS Flex设计一个导航栏,其中一组项目左对齐,另一组右对齐?

huangapple go评论52阅读模式
英文:

How can I design a navigation bar where one group of items are aligned left and another group aligned right with CSS Flex?

问题

以下是您要翻译的内容:

Code A

<nav>
  <ul>
    <li><a href="#">Page 1</a></li>
    <li><a href="#">Page 2</a></li>
    <li><a href="#">Page 3 is longer</a></li>
    <li class="push-right"><a href="#">Page 4</a></li>
  </ul>
</nav>

nav ul {
  display: flex;
  margin: 0 -10px;
}

nav li {
  margin: 0 10px;
}

.push-right {
  margin-left: auto;
}

Code B

<nav>
  <ul>
    <li><a href="#">Page 1</a></li>
    <li><a href="#">Page 2</a></li>
    <li class="push-right"><a href="#">Page 3 is longer</a></li>
    <li class="push-right"><a href="#">Page 4</a></li>
  </ul>
</nav>

// The same

Image A

如何使用CSS Flex设计一个导航栏,其中一组项目左对齐,另一组右对齐?

Image B

如何使用CSS Flex设计一个导航栏,其中一组项目左对齐,另一组右对齐?

Added Content

Thanks! Maybe the Code C is good way.

Code C

<nav>
  <ul>
    <li><a href="#">Page 1</a></li>
    <li><a href="#">Page 2</a></li>

    <li class="push-right"><a href="#">Page 3 is longer</a></li>
    <li><a href="#">Page 4</a></li>
  </ul>
</nav>

nav ul {
  display: flex;
  margin: 0 -10px;
}

nav li {
  margin: 0 10px;
}

.push-right {
  margin-left: auto;
}
英文:

The Code A is from the article.

The Image A is the result of Code A.

I hope to desgin a UI just like Image B, the two labels on the left of the bar, and the two labels on the right of the bar.

But Code B can't get the result, how can I do?

Code A

&lt;nav&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Page 1&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Page 2&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Page 3 is longer&lt;/a&gt;&lt;/li&gt;
    &lt;li class=&quot;push-right&quot;&gt;&lt;a href=&quot;#&quot;&gt;Page 4&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/nav&gt;

nav ul {
  display: flex;
  margin: 0 -10px;
}

nav li {
  margin: 0 10px;
}

.push-right {
  margin-left: auto;
}

Code B

&lt;nav&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Page 1&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Page 2&lt;/a&gt;&lt;/li&gt;
    &lt;li class=&quot;push-right&quot;&gt;&lt;a href=&quot;#&quot;&gt;Page 3 is longer&lt;/a&gt;&lt;/li&gt;
    &lt;li class=&quot;push-right&quot;&gt;&lt;a href=&quot;#&quot;&gt;Page 4&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/nav&gt;

//The same

Image A

如何使用CSS Flex设计一个导航栏,其中一组项目左对齐,另一组右对齐?

Image B

如何使用CSS Flex设计一个导航栏,其中一组项目左对齐,另一组右对齐?

Added Content

Thanks! Maybe the Code C is good way.

Code C

&lt;nav&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Page 1&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Page 2&lt;/a&gt;&lt;/li&gt;

    &lt;li class=&quot;push-right&quot;&gt;&lt;a href=&quot;#&quot;&gt;Page 3 is longer&lt;/a&gt;&lt;/li&gt;
    &lt;li &gt;&lt;a href=&quot;#&quot;&gt;Page 4&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/nav&gt;

nav ul {
  display: flex;
  margin: 0 -10px;
}

nav li {
  margin: 0 10px;
}

.push-right {
  margin-left: auto;
}

答案1

得分: 2

在第三个项目上添加“push-right”类,而不是第四个。 这将使它们都向右移动。

英文:

Put the "push-right" class on the third item, not the fourth. It then pushes both to the right.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

nav {
  display: flex;
}

a {
  margin: 0 10px;
}

.push-right {
  margin-left: auto;
}

<!-- language: lang-html -->

&lt;nav&gt;
  &lt;a href=&quot;#&quot;&gt;Page 1&lt;/a&gt;
  &lt;a href=&quot;#&quot;&gt;Page 2&lt;/a&gt;
  &lt;a class=&quot;push-right&quot; href=&quot;#&quot;&gt;Page 3 is longer&lt;/a&gt;
  &lt;a href=&quot;#&quot;&gt;Page 4&lt;/a&gt;
&lt;/nav&gt;

<!-- end snippet -->

答案2

得分: -1

以下是翻译后的代码部分:

nav > ul {
  display: flex;
  justify-content: space-between;
  padding: 0;
}

nav > ul li {
  display: inline-block;
  padding: 2px 8px;
  border: 1px solid blue;
}
<nav>
  <ul>
    <div>
      <li><a href="#">Page 1</a></li>
      <li><a href="#">Page 2</a></li>
    </div>
    <div>
      <li><a href="#">Page 3 is longer</a></li>
      <li><a href="#">Page 4</a></li>
    </div>
  </ul>
</nav>
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

nav &gt; ul {
  display: flex;
  justify-content: space-between;
  padding: 0;
}

nav &gt; ul li {
  display: inline-block;
  padding: 2px 8px;
  border: 1px solid blue;
}

<!-- language: lang-html -->

&lt;nav&gt;
  &lt;ul&gt;
    &lt;div&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Page 1&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Page 2&lt;/a&gt;&lt;/li&gt;
    &lt;/div&gt;
    &lt;div&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Page 3 is longer&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Page 4&lt;/a&gt;&lt;/li&gt;
    &lt;/div&gt;
  &lt;/ul&gt;
&lt;/nav&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月1日 08:28:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定