英文:
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
Image B
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
<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
Image B
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;
}
答案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 -->
<nav>
<a href="#">Page 1</a>
<a href="#">Page 2</a>
<a class="push-right" href="#">Page 3 is longer</a>
<a href="#">Page 4</a>
</nav>
<!-- 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 > ul {
display: flex;
justify-content: space-between;
padding: 0;
}
nav > ul li {
display: inline-block;
padding: 2px 8px;
border: 1px solid blue;
}
<!-- language: lang-html -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论