我正在制作一个下拉导航菜单。宽度超过 1100 像素的徽标和列表项位于 Flexbox 中,徽标位于左侧,列表位于右侧。当宽度低于 1100px 时,徽标应该保持在右侧,并且列表项会变成带有下拉列表的菜单图标。然而,徽标只是停留在中心,我无法将其移动到左侧。
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: Arial, Helvetica, sans-serif;
}
header {
display: flex;
text-align: center;
justify-content: space-between;
align-items: center;
width: 100%;
padding: 0 60px;
background-color: #212223;
position: relative;
}
.logo {
float: left;
}
.logo img {
width: 190px;
}
nav {
float: right;
}
nav ul li {
display: inline-block;
padding: 5px 25px;
}
nav ul li a {
display: inline-block;
font-weight: bold;
font-size: 20px;
list-style-type: none;
}
nav ul li a:hover {
color: rgb(174, 17, 174);
}
nav ul li a {
text-decoration: none;
display: inline-block;
color: white;
font-weight: 600;
}
.toggle-button {
width: 35px;
position: absolute;
right: 80px;
top: 35px;
display: none;
cursor: pointer;
}
.toggle-button span {
display: inline-block;
width: 100%;
height: 2px;
background-color: white;
float: left;
margin-bottom: 8px;
}
.bookatable {
color: #fff;
background-color: #555556;
border-radius: 6px;
text-transform: uppercase;
font-weight: 600;
font-size: 18px;
line-height: 1;
text-align: center;
padding: 9px 6px 8px 8px;
transition-duration: 0.6s;
transition-timing-function: ease-in-out;
letter-spacing: -.3px;
cursor: pointer;
}
.bookatable:hover {
cursor: pointer;
background-color: #909092;
color: white;
}
/* ---- BREAKPOINTS ---- */
@media (max-width: 1100px) {
header {
display: flex;
flex-direction: column;
text-align: center;
position: relative;
justify-content: space-between;
}
.toggle-button {
display: block;
}
nav {
display: none;
width: 100%;
border-top: 1px solid white;
padding-top: 20px;
margin-top: 30px;
padding-bottom: 20px;
}
nav ul li {
padding: 15px 0px;
width: 100%;
}
nav ul {
text-align: center;
}
nav.show {
display: flex;
flex-direction: column;
flex-wrap: nowrap;
}
}
<link href='https://unpkg.com/[email protected]/css/boxicons.min.css' rel='stylesheet'>
<header class="header">
<div class="toggle-button" onclick="myFunction();">
<span></span>
<span></span>
<span></span>
</div>
<div class="logo">
<a href="index.html"><img src="https://i.stack.imgur.com/y0nIA.png" alt=""></a>
</div>
<nav id="nav">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Menu</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">Contact Us</a></li>
<li><a href="#" class="bookatable">Book A Table</a></li>
</ul>
</nav>
</header>
<script type="text/javascript">
function myFunction() {
var navbar = document.getElementById('nav');
navbar.classList.toggle('show');
}
</script>
如果您在媒体查询中添加以下样式,图像将与 Flex 标头的最右侧对齐
.logo {
align-self: flex-start;
}
或者,您可以通过将以下内容添加到媒体查询中来使用边距来完成此操作
.logo {
margin-right: auto;
}
您生成的媒体查询将是
@media (max-width: 1100px) {
header {
display: flex;
flex-direction: column;
text-align: center;
position: relative;
justify-content: space-between;
}
.toggle-button {
display: block;
}
nav {
display: none;
width: 100%;
border-top: 1px solid white;
padding-top: 20px;
margin-top: 30px;
padding-bottom: 20px;
}
nav ul li {
padding: 15px 0px;
width: 100%;
}
nav ul {
text-align: center;
}
nav.show {
display: flex;
flex-direction: column;
flex-wrap: nowrap;
}
.logo {
margin-right: auto;
}
}
或
@media (max-width: 1100px) {
header {
display: flex;
flex-direction: column;
text-align: center;
position: relative;
justify-content: space-between;
}
.toggle-button {
display: block;
}
nav {
display: none;
width: 100%;
border-top: 1px solid white;
padding-top: 20px;
margin-top: 30px;
padding-bottom: 20px;
}
nav ul li {
padding: 15px 0px;
width: 100%;
}
nav ul {
text-align: center;
}
nav.show {
display: flex;
flex-direction: column;
flex-wrap: nowrap;
}
.logo {
align-self: flex-start;
}
}