html - 固定和中心导航栏
问题描述
我正在尝试制作一个显示宽度为 70% 的固定中心导航栏。当我将位置更改为固定时,它总是移动到左侧大小。
body {
background-color: black;
}
.navbar,
main {
width: 70%;
margin: 0 auto;
}
.navbar {
position: fixed;
background: #1a1c1b;
padding: 5px 0;
}
<header>
<nav class="navbar">
<div class="logo-box">
<a href="index.html" class="logo">MOVIES</a>
</div>
<div class="search-box">
<form action="https://www.google.com/search" method="get">
<input id="seach-bar" name="q" type="search" placeholder="Search MOVIES" autocomplete="off">
<button id="search-button" type="submit" class="search">Search</button>
</form>
</div>
<div class="nav-links">
<ul>
<li><a href="#" class="links">Genres</a></li>
<li><a href="#" class="links">Celebs</a></li>
<li><a href="#" class="links">My List</a></li>
</ul>
</div>
</nav>
</header>
解决方案
使用弹性盒子!
将标题设置为具有属性的flexbox以在水平轴上居中对齐导航栏。justify-content
header {
display: flex;
justify-content: center;
}
推荐阅读
- html - 使flexbox中每个孩子的高度相等,无论内容少还是多
- r - 基于日期的R条形图计数数据
- pymupdf - 呈现的页面不显示对字段的更新
- javascript - 将鼠标事件添加到 DIv 中的所有子元素
- c++ - 我的 winreg 功能未被识别
- java - java.lang.arrayindexoutofboundsexception 与环境变量
- postgresql - 用于返回 null 的数字的 pgsql concat
- javascript - 使用 toFixed 问题在 javascript 中将米转换为英尺,反之亦然
- c# - 行进立方体绘图三角形统一
- video - 如何修复或恢复损坏的视频文件