gpt4 book ai didi

html - 删除 Flexbox 时垂直居中失败

转载 作者:太空宇宙 更新时间:2023-11-03 22:22:10 25 4
gpt4 key购买 nike

我不确定为什么会这样。当我删除时:

.nav-username {
display: flex;
align-items: center;
}

元素水平对齐,但是,图像和文本不再垂直对齐,因为我删除了 display: flexalign-items: center

$('.dropdown').on('click', function() {
$('.navigation-ul li:not(:first-child)').toggleClass('active');
});
*,
*:after,
*:before {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
box-sizing: border-box;
text-decoration: none;
color: white;
}

a {
color: #f1f1f1;
}

html {
font-size: 16px;
}

body {
background-color: #fafafa;
font-family: 'Roboto', sans-serif;
height: 100%;
width: 100%;
}

.navigation {
background-color: #171717;
}

.navigation-ul {
display: flex;
align-items: center;
}

.navigation-ul>li:not(.dropdown) {
padding: 10px 15px 10px 15px;
font-weight: 500;
text-align: center;
font-size: 1em;
color: white;
background-color: #151719;
list-style-type: none;
}

.navigation-ul li:nth-last-child(2) {
margin-left: auto;
}

.nav-username {
display: flex;
align-items: center;
}

.profile-picture {
display: inline-block;
border-radius: 50px;
height: 30px;
width: 30px;
margin-right: 10px;
}

@media all and (min-width: 0px) and (max-width: 1200px) {
.navigation-ul {
flex-wrap: wrap;
}
.navigation-ul>li {
flex: 1 1 100%;
}
}

@media screen and (max-width: 1200px) {
.navigation-ul li:first-child {
display: block;
}
.navigation-ul li {
display: none;
}
.navigation-ul li.active {
display: block;
}
.navigation-ul .dropdown {
display: block;
cursor: pointer;
}
.home-container {
columns: 2;
}
.dropdown {
padding: 7px 0 7px 0;
text-align: center;
}
.specific-image-flexbox {
flex-direction: column;
}
}

@media screen and (max-width: 600px) {
.home-container {
columns: 1;
}
.category-container-element {
width: 100%;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class='navigation'>
<div class="wrapper">
<ul class="navigation-ul">
<li class='dropdown'>Dropdown</li>
<li><a href="">Images</a></li>
<li><a href="">Categories</a></li>
<li><a href="">Upload</a></li>
<li>
<a class='nav-username' href=''>
<img class='profile-picture' src=''> Username
</a>
</li>
<li><a href="">Logout</a></li>
</ul>
</div>
</nav>

最佳答案

您还需要添加属性 justify-content:center;。因此它将在垂直和水平方向上居中对齐。

所以,你需要更新

.nav-username {
display: flex;
align-items: center;
}

.nav-username {
display: flex;
align-items: center;
justify-content: center;
}

关于html - 删除 Flexbox 时垂直居中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53055426/

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