html - 导航栏的 Flex 顺序不正确
问题描述
我正在尝试订购我的导航栏,以便顺序是导航,然后是按钮,然后是 h1。我在这里关注一个教程,它是 youtuber 提供的第三个选项。h1 应该一直推到右侧。出于某种原因,弹性订单将无法在标题中工作并以正确的方式对齐。
*{
box-sizing: border-box;
padding:0;
margin:0;
}
body{
background-color: rgb(54, 54, 54);
font-family: sans-serif;
color: white;
}
li, a, button{
font-weight: 500;
font-size: 16px;
font-family: sans-serif;
color: rgb(255, 255, 255);
text-decoration: none;
}
header{
display: flex;
justify-content: space-between;
align-items: center;
height: 15vh;
padding: 30px 10%;
}
h1{
cursor: pointer;
margin-left: auto;
order: 3;
}
nav{
order: 1;
}
ul{
list-style: none;
}
ul li{
display: inline-block;
padding: 0 20px;
}
ul li a{
transition: all 0.3s ease;
}
ul li a:hover{
color: pink;
}
.cta{
order: 2;
}
button{
padding: 9px 25px;
background-color: white;
color: rgb(54, 54, 54);
border: none;
border-radius: 50px;
cursor: pointer;
transition: all 0.3s ease;
}
button:hover{
background-color: pink;
color: white;
}
<header>
<a href="/index.html"><h1>Loonieville</h1></a>
<nav>
<ul class='navlinks'>
<li><a href="/">Main St</a></li>
<li><a href="/">Town Hall</a></li>
<li><a href="/">Downtown</a></li>
</ul>
</nav>
<a href="" class='cta'><button>
Contact
</button></a>
</header>
解决方案
您使用 h1 选择器来订购 h1,但 h1 元素不是弹性项目。只有弹性容器的直接子级是弹性项目。在这种情况下, h1 元素不是弹性项目,而是包裹在它周围的锚标记。
选择具有某个类的 a 标签,然后对其进行排序。像这样,
HTML
<a href="#" class="logo"><h1>Loonieville</h1></a>
CSS
.logo{
order: 3;
}
建议:如果您想单独设置弹性项目的样式,请给它们单独的类。
推荐阅读
- c++ - 在新的 MPI_Isend 之前使用 MPI_Wait
- java - 打印语句替换用户输入
- google-maps - Infowindow 仅适用于 1 条路线,不适用于谷歌地图中的其他 3 条路线
- php - 用于重定向页面的 PHP 会话变量
- c# - ASP.NET 核心标识种子只创建最后一个用户
- node.js - NGINX 随机停止工作,需要手动重启
- c - 使用 malloc() 的局部变量需要多大的内存大小?
- c# - SignalR 在 Microsoft.ApplicationInsights.dll 中触发 System.InvalidOperationException
- node.js - 如何增加快递应用程序中的请求文件大小
- reactjs - 在 Material-UI v1 中使 Input 的 InputAdornment 无法聚焦