我不确定为什么会这样。当我删除时:
.nav-username {
display: flex;
align-items: center;
}
元素水平对齐,但是图像和文本停止垂直对齐,因为我删除了display: flex
和align-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>
答案 0 :(得分:1)
我认为将显示类型flex放在锚标记上存在问题。我已将flex类放置在列表项上,它似乎可以正常工作:
$('.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;
vertical-align: middle;
}
@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 class='nav-username'>
<a href=''><img class='profile-picture' src=''> Username</a>
</li>
<li><a href="">Logout</a></li>
</ul>
</div>
</nav>
答案 1 :(得分:1)
您还需要添加属性justify-content:center;
。这样它就会居中垂直和水平对齐。
因此,您需要更新
.nav-username {
display: flex;
align-items: center;
}
到
.nav-username {
display: flex;
align-items: center;
justify-content: center;
}
答案 2 :(得分:1)
您的CSS代码:
.nav-username {
display: flex;
align-items: center;
}
...正在应用于锚元素:
<a class='nav-username' href=''>
<img class='profile-picture' src=''>
Username
</a>
让我们从删除 flex 时发生的事情开始。
默认情况下,anchor元素是内联级别的元素。这意味着它仅占用其内容的空间。对于内联级元素,width
property和height
property不适用。
然后考虑vertical-align
属性适用于内联级元素(和表单元元素)。它的默认值为baseline
,这意味着内联级元素以baseline alignment开头。
因此,在没有 flex布局的情况下,锚元素会退回到 block布局,并且上述设置已就位。在这种情况下,要使文本垂直居中,请尝试vertical-align: middle
。
/* .nav-username {
display: flex;
align-items: center;
} */
.nav-username > * {
vertical-align: middle;
}
启用 flex 时,其播放方式如下:
flex容器将其子元素转换为flex项目,即块级元素by definition。此转换将忽略元素类型。因此,在 block布局中为内联级别或块级别的元素在flex布局中为 block-level 。
这意味着flex项目将忽略vertical-align
属性,并与上面的解释一起解释了为什么在适当位置使用flex时垂直居中有效,而在删除flex时不能居中。 / p>