首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我一直在做水平菜单,得到的问题是导航标签不能容纳所有的ul列表

我一直在做水平菜单,得到的问题是导航标签不能容纳所有的ul列表
EN

Stack Overflow用户
提问于 2020-09-14 20:05:21
回答 3查看 43关注 0票数 0

我创建了一个有3个div的菜单: logo、导航栏和右栏。我让它们对齐-content:空格-环绕,在右边有logo,在中间有导航,在右边有右栏。此外,我还将链接设为空白: nowrap,并添加了边距:010%(仅举个例子),我的li去掉了nav标签。我怎么才能修复它?

代码链接:https://codepen.io/kartegi/pen/xxVzPLm?editors=1100

代码语言:javascript
运行
复制
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  text-decoration: none;
  list-style-type: none;
}

.menu {
  display: flex;
  align-items: center;
  justify-content: space-around;
  width: 100%;
  height: 80px;
  background: green;
}

a {
  white-space: nowrap;
  color: #fff;
}

nav {
  background: red;
}

.menu-list {
  display: flex;
  background: #000;
  margin: 0 2.5%;
}

li {
  margin: 0 10%;
}

.right-bar {
  display: flex;
}

.search {
  width: 20px;
  height: 20px;
  background: #000;
}

.language {
  width: 20px;
  height: 20px;
  background: #fff;
}
代码语言:javascript
运行
复制
<div class="menu">
  <div class="logo">
    <a href="">Click me</a>
  </div>
  <nav class="navbar">
    <ul class="menu-list">
      <li><a href="#">About me</a></li>
      <li><a href="#">My compony</a></li>
      <li><a href="#">News</a></li>
      <li><a href="#">Media</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
  <div class="right-bar">
    <div class="search">
    </div>
    <div class="language">
    </div>
  </div>
</div>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-09-14 20:25:04

在这里,您可以在ul列表中看到li。只需将"flex: 1 1 auto;“添加到您的.navbar元素中即可获得最大宽度。现在您需要管理元素的宽度和间距。

代码语言:javascript
运行
复制
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  text-decoration: none;
  list-style-type: none;
}

.menu {
  display: flex;
  align-items: center;
  justify-content: space-around;
  width: 100%;
  height: 80px;
  background: green;
}

a {
  white-space: nowrap;
  color: #fff;
}

nav {
  background: red;
}

.menu-list {
  display: flex;
  background: #000;
  margin: 0 2.5%;
}

li {
  margin: 0 10%;
}

.right-bar {
  display: flex;
}

.search {
  width: 20px;
  height: 20px;
  background: #000;
}

.language {
  width: 20px;
  height: 20px;
  background: #fff;
}
.navbar{
  flex: 1 1 auto;
}
代码语言:javascript
运行
复制
<div class="menu">
  <div class="logo">
    <a href="">Click me</a>
  </div>
  <nav class="navbar">
    <ul class="menu-list">
      <li><a href="#">About me</a></li>
      <li><a href="#">My compony</a></li>
      <li><a href="#">News</a></li>
      <li><a href="#">Media</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
  <div class="right-bar">
    <div class="search">
    </div>
    <div class="language">
    </div>
  </div>
</div>

票数 1
EN

Stack Overflow用户

发布于 2020-09-14 20:15:42

这就是你要找的吗?

在你的.menu-list上试用flex-direction: column;

flex-wrap: wrap;将元素包装在可用空间内。

票数 0
EN

Stack Overflow用户

发布于 2020-09-14 20:41:10

代码语言:javascript
运行
复制
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  text-decoration: none;
  list-style-type: none;
}

.menu {
 
  display: flex;
  align-items: center;
  justify-content: space-around;
 
  
  height: 80px;
  background: green;
}

a {
  white-space: nowrap;
  color: #fff;
}

nav {
 background: #fff;
}

.menu-list {
  display:flex;
  align-items:space-between;
  background-color:black;
}

li a{
  padding:20px; // remove margin from li and give padding on anchor tag 
  
}

.right-bar {
  display: flex;
}

.search {
 
  height: 20px;
  background: #000;
}

.language {
 
  background: #fff;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63883949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档