好吧,我正在学习 flexbox,但我不明白为什么我的导航标题在链接上方。
HTML:
<style>
@import url(https://fonts.googleapis.com/css?family=Oswald:400,700);
.box {
display: flex;
}
.item {
width: 100px;
height: 100px;
background-color: red;
margin: auto;
color: #fff;
}
nav {
font-family: "Oswald", sans-serif;
display: flex;
min-width: 100%;
background-color: #181818;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
max-width: 960px
}
nav a {
display: block;
padding: 1rem;
text-decoration: none;
color: #fff;
font-weight: 400;
transition: all 0.3s ease-in-out;
}
nav a:hover {
color: #343434;
}
.title {
margin: 0 35px 0 10px;
color: #1BC;
}
</style>
<nav>
<div class="container">
<a class="title">Architect</a>
<ul>
<li><a href="#">Getting Started</a></li>
<li><a href="#">Examples</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Forum</a></li>
</ul>
</div>
</nav>
容器 CSS:
.container{
width: 100%;
margin: 0 auto:
max-width: 1200px;
}
代码笔链接:http://codepen.io/ZoidCraft/pen/XKMewy
我希望标题“Architect”与链接的左侧对齐。
最佳答案
你设置<a class="title">Architect</a>
至 display: block;
在你的CSS中。 block 级元素将占据自己的行。 display: flex;
元素也将占据自己的行。
要解决您的问题,您可以先删除 display: block;
来自你的 nav a
风格。然后更改您的 nav ul
来自 display: flex;
至 display: inline-flex;
.现在您只需要将一些填充添加回您的 nav
因为现在所有内容都是内联显示,所以添加 padding: 1em 0;
给你的nav
这是更新的 CodePen我正在谈论的内容。
关于html - Flex 导航未对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38086023/