Dav*_*kes 22 html css css3 centering flexbox
我正在尝试使用flexbox创建这个顶部标题.
基本上我想将<div class="header-title">(机构机构1)与你看到的其他3个元素联系起来.(Institutioner,Ledere和Log ud)就像你在图像上看到的那样.
.nav {
background: #e1e1e1;
}
ol, ul {
list-style: none;
display: flex;
flex-direction: row;
align-items: center;
justify-content: flex-start;
}
.header-title {
justify-content: center;
align-self: center;
display: flex;
}
.nav ul li.logout {
margin-left: auto;
}
.nav ul li a {
text-decoration: none;
padding: 0px 20px;
font-weight: 600;
}Run Code Online (Sandbox Code Playgroud)
<div class="nav mobilenav">
<div class="header-title">
Institution institution 1
</div>
<ul>
<li><a href="/institutions/">Institutioner</a></li>
<li>
<a href="/leaders/">Ledere</a>
</li>
<li class="logout">
<a class="button-dark" href="/user/logout">Log ud</a>
</li>
</ul>
</div>Run Code Online (Sandbox Code Playgroud)
Mic*_*l_B 33
使用嵌套的flex容器和flex-grow: 1.
这允许您在导航栏上创建三个等宽部分.
然后每个部分成为(嵌套的)flex容器,允许您使用flex属性垂直和水平对齐链接.
现在左右项目固定到容器的边缘,中间项目完全居中(即使左右项目的宽度不同).
.nav {
display: flex;
height: 50px; /* optional; just for demo */
background: white;
}
.links {
flex: 1; /* shorthand for: flex-grow: 1, flex-shrink: 1, flex-basis: 0 */
display: flex;
justify-content: flex-start;
align-items: center;
border: 1px dashed red;
}
.header-title {
flex: 1;
display: flex;
justify-content: center;
align-items: center;
border: 1px dashed red;
}
.logout {
flex: 1;
display: flex;
justify-content: flex-end;
align-items: center;
border: 1px dashed red;
}
.links a {
margin: 0 5px;
text-decoration: none;
}Run Code Online (Sandbox Code Playgroud)
<div class="nav mobilenav">
<div class="links">
<a href="/institutions/">Institutioner</a>
<a href="/leaders/">Ledere</a>
</div>
<div class="header-title">Institution institution 1</div>
<div class="logout"><a class="button-dark" href="/user/logout">Log ud</a></div>
</div>Run Code Online (Sandbox Code Playgroud)
justify-content: space-between;像这样使用:
.container {
display: flex;
justify-content: space-between;
}Run Code Online (Sandbox Code Playgroud)
<div class="container">
<div>A</div>
<div>B</div>
<div>C</div>
</div>Run Code Online (Sandbox Code Playgroud)