gpt4 book ai didi

css - bootstrap 4 中的多行居中导航栏元素

转载 作者:太空宇宙 更新时间:2023-11-04 01:14:13 24 4
gpt4 key购买 nike

我有一个导航栏,当只有单行元素时它工作正常:

<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

但是当 item 是多行时,其他 item 会失去垂直居中对齐并改为顶部对齐:

<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

那么,多行元素如何居中呢?

最佳答案

删除 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,请对每个 li 元素使用 align-self-center .

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

关于css - bootstrap 4 中的多行居中导航栏元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50557550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com