在另一个flex内部对齐flex

时间:2019-03-16 03:26:05

标签: css html5 flexbox

我正在尝试创建一个移动布局,其中间的左侧导航栏中带有徽标,而人字形文字可以显示和隐藏导航栏的各项(我现在不打算执行此功能)。

但是现在我正在尝试实现以下目标:

+------+---------+---------+
| 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>

1 个答案:

答案 0 :(得分: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>