代码之家  ›  专栏  ›  技术社区  ›  artem

引导4中多行居中的导航栏项目

  •  0
  • artem  · 技术社区  · 6 年前

    我有一个导航栏,当只有单行项目时,它可以正常工作:

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav ml-auto">
    
                    <li class="nav-item">
                        <a class="nav-link" href="#">FAQ</a>
                    </li>
    
                    <li class="nav-item">
                        <a class="nav-link profile" href="#">
                            <div class="profile-info">
                                <span class="name">Артём</span>
                            </div>
                        </a>
                    </li>
                </ul>
            </div>
    

    enter image description here

    但当项目为多行时,其他项目将失去中心垂直对齐,而改为顶端对齐:

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav ml-auto">
    
                    <li class="nav-item">
                        <a class="nav-link" href="#">FAQ</a>
                    </li>
    
                    <li class="nav-item">
                        <a class="nav-link profile" href="#">
                            <div class="profile-info">
                                <span class="name">Артём</span>
                                <span class="balance">31337 ₽</span>
                            </div>
                        </a>
                    </li>
                </ul>
            </div>
    

    enter image description here

    我的SASS代码:

    #site-header {
      background: #2d3840;
      border-bottom: solid 1px #20272d;
      height: 83px;
    
      .navbar-brand {
        color: white;
      }
    
      .nav-item {
    
        &:not(:first-child) {
          margin-left: 25px;
        }
    
        &:not(:last-child) {
          margin-right: 25px;
        }
    
        // General link
        a.nav-link {
          color: white;
          font-size: 16px;
          text-align: center;
    
          @include transition(0.5s);
          &:hover {
            @include transition(0.3s);
            color: $color-accent;
          }
    
          &.active {
            padding: 6px 21px 6px 21px;
            background: $color-accent;
            border-radius: 17.5px;
            box-shadow: 0 2px 10px 0 rgba(90, 175, 238, 0.6);
    
            &:hover {
              color: white;
              box-shadow: none;
            }
          }
    
          // Profile
          &.profile {
            .profile-info {
              display: inline-block;
    
              .name {
                display: block;
                font-weight: bold;
              }
    
              .balance {
                display: block;
                font-size: 16px;
                font-weight: bold;
                color: $color-accent;
              }
            }
          }
        }
    
      }
    }
    

    渴望的:

    enter image description here

    那么,当存在多行项目时,如何将项目居中?

    1 回复  |  直到 6 年前
        1
  •  2
  •   mahan    6 年前

    删除 profile-info 并将这些类用于 a 标签。

    1. d-flex flex
    2. flex-column -改变它 flex-direction 属性到 column
    3. text-right -将其对齐 inline 右边的孩子们

    <li class="nav-item">
      <a class="nav-link d-flex flex-column text-right" href="#">
        <span class="name">Артём</span>
        <span class="balance">31337 ₽</span>
      </a>
    </li>

    把另一个对齐 items 垂直于列表中心,使用 align-self-center 对于每一个 li 元素。

    <li class="nav-item  align-self-center">
      <a class="nav-link" href="#">FAQ</a>
    </li>

    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/js/bootstrap.js"></script>
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
      <a class="navbar-brand" href="#">Navbar</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
    
      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav ml-auto">
          <li class="nav-item active  align-self-center">
            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item  align-self-center">
            <a class="nav-link" href="#">Link</a>
          </li>
    
          <li class="nav-item align-self-center">
            <a class="nav-link disabled" href="#">Disabled</a>
          </li>
          <li class="nav-item  align-self-center">
            <a class="nav-link" href="#">FAQ</a>
          </li>
    
          <li class="nav-item ">
            <a class="nav-link d-flex flex-column text-right" href="#">
              <span class="name">Артём</span>
              <span class="balance">31337 ₽</span>
            </a>
          </li>
        </ul>
      </div>
    </nav>