在另一个弯曲内对齐弯曲

问题描述 投票:1回答:1

我正在尝试创建一个移动布局,在左侧导航中间有一个徽标和一个V形符号来显示和隐藏导航栏的项目(我现在对此功能不感兴趣)。

但是现在我正在努力实现这样的目标:

+------+---------+---------+
| logo |  lorem  | chevron |
+------+---------+---------+
       | ipsum   |         
       +---------+
       | dolorem |    
       +---------+

我一直试图将ul的第一项与图标对齐。

关于如何在保持html语法的同时实现这一目标的任何想法?

* {
  box-sizing: border-box;
}

body {
  font-family: "IBM Plex Mono", monospace;
  font-size: 18px;
}

.main-nav {
  font-size: 24px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  border: 1px solid red;
  height: 50px;
}

.main-nav__list {
  display: flex;
  flex: 1;
  padding: 0;
  list-style-type: none;
  border: 1px solid greenyellow;
  flex-direction: column;
  align-self: flex-start;
  position: relative;
}

.main-nav__icon {
  height: 24px;
  width: auto;
  margin: auto;
}

.main-nav__links {
  text-decoration: none;
  color: white;
}

.main-nav__list-item {
  width: 100%;
  text-align: center;
}


/*.main-nav__list-item:not(:first-child){
  display: none;
}*/

.main-nav__list-item--blue {
  background-color: #1c77c3;
}

.main-nav__list-item--green {
  background-color: #00aa00;
}

.main-nav__list-item--orange {
  background: #dd2e02;
}

.main-nav__logo {
  flex: initial;
  width: 50px;
  background-color: black;
  display: flex;
  height: 100%;
  color: white;
}
<script src="https://use.fontawesome.com/releases/v5.7.2/js/all.js"></script>
<header class="">
  <nav class="main-nav">
    <a class="main-nav__logo" href="#"><i class="far fa-user main-nav__icon" alt="logo"></i></a>
    <ul class="main-nav__list">
      <li class="main-nav__list-item main-nav__list-item--blue"><a class="main-nav__links" href="#">Lorem</a></li>
      <li class="main-nav__list-item main-nav__list-item--green"><a class="main-nav__links" href="#">Ipsum</a></li>
      <li class="main-nav__list-item main-nav__list-item--orange"><a class="main-nav__links" href="#">dolorem</a></li>
    </ul>
    <a class="main-nav__logo" href="#"><i class="fas fa-chevron-down main-nav__icon" alt="chevron down"></i></a>
  </nav>
</header>
css html5 flexbox
1个回答
1
投票

到你的.main-nav__list,添加:

/* Add margin: 0; and height: 300%; */
margin: 0;
height: 300%;

到你的.main-nav__list-item。加:

/* Add flex-grow: 1; and center items */
flex-grow: 1;
display: flex;
align-items: center;
justify-content: center;

* {
  box-sizing: border-box;
}

body {
  font-family: "IBM Plex Mono", monospace;
  font-size: 18px;
}

.main-nav {
  font-size: 24px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  border: 1px solid red;
  height: 50px;
}

.main-nav__list {
  display: flex;
  flex: 1;
  padding: 0;
  list-style-type: none;
  border: 1px solid greenyellow;
  flex-direction: column;
  align-self: flex-start;
  position: relative;
  /* Add margin: 0; and height: 300%; */
  margin: 0;
  height: 300%;
}

.main-nav__icon {
  height: 24px;
  width: auto;
  margin: auto;
}

.main-nav__links {
  text-decoration: none;
  color: white;
}

.main-nav__list-item {
  width: 100%;
  text-align: center;
  /* Add flex-grow: 1; and center items */
  flex-grow: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}


/*.main-nav__list-item:not(:first-child){
  display: none;
}*/

.main-nav__list-item--blue {
  background-color: #1c77c3;
}

.main-nav__list-item--green {
  background-color: #00aa00;
}

.main-nav__list-item--orange {
  background: #dd2e02;
}

.main-nav__logo {
  flex: initial;
  width: 50px;
  background-color: black;
  display: flex;
  height: 100%;
  color: white;
}
<script src="https://use.fontawesome.com/releases/v5.7.2/js/all.js"></script>
<header class="">
  <nav class="main-nav">
    <a class="main-nav__logo" href="#"><i class="far fa-user main-nav__icon" alt="logo"></i></a>
    <ul class="main-nav__list">
      <li class="main-nav__list-item main-nav__list-item--blue"><a class="main-nav__links" href="#">Lorem</a></li>
      <li class="main-nav__list-item main-nav__list-item--green"><a class="main-nav__links" href="#">Ipsum</a></li>
      <li class="main-nav__list-item main-nav__list-item--orange"><a class="main-nav__links" href="#">dolorem</a></li>
    </ul>
    <a class="main-nav__logo" href="#"><i class="fas fa-chevron-down main-nav__icon" alt="chevron down"></i></a>
  </nav>
</header>
© www.soinside.com 2019 - 2024. All rights reserved.