gpt4 book ai didi

html - 菜单对齐居中和 float 问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:53:41 25 4
gpt4 key购买 nike

我的站点标题和导航功能如我所愿。但链接和 Logo 的布局未对齐。

它应该如下所示。

link--link--link-----logo-----link--link--dropdown

但是显示如下

link--link--logo--------------dropdown--link--link

Logo 不会居中居中..并且下拉列表显示在右侧的链接之前。

#siteHeader {
max-width: 1600px;
padding-top: 15px;
height: 93px;
font-size: 12px;
text-transform: uppercase;
background-color: #000;
margin-left: auto;
margin-right: auto;
border-top: 1px solid #d7d5d5;
}
#siteHeader a {
text-decoration: none;
}
.logo {
display: block;
height: 55px;
width: auto;
float: left;
}
.left {
float: left;
}
.right {
float: right;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
width: auto;
display: block;
float: left;
clear: right;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 6px 16px;
text-decoration: none;
float: left;
}
li a:hover {
background-color: #000;
}
.dropbtn {
background-color: #000;
color: white;
padding-top: 6px;
padding-bottom: 25px;
border: none;
cursor: pointer;
min-width: 100px;
text-transform: uppercase;
}
.dropdown {
position: relative;
display: inline-block;
float: right;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #434343;
min-width: 185px;
right: 0;
}
.dropdown-content a {
color: white;
padding: 13px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #000000
}
.dropdown:hover .dropdown-content {
display: block;
float: left;
}
.dropdown:hover .dropbtn {
background-color: #434343;
color: white;
}
<div id="siteHeader">
<div class="container-fluid normalize">
<div class="nav">
<ul class="left">
<li><a href="#home">Link</a>
</li>
<li><a href="#news">Link</a>
</li>
<li><a href="#news">Link</a>
</li>
</ul>
<img src="content/images/logo.png" class="logo">
<ul class="right">
<li><a href="#home">Link</a>
</li>
<li><a href="#news">Link</a>
</li>
</ul>
<div class="dropdown">
<button class="dropbtn">Dropdown <span>+</span>
</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
</div>
</div>
</div>

最佳答案

Flexbox 看起来是个合理的解决方案。我删除了所有 float 属性,添加了 div 容器来包装出现在右侧的元素,并将 flexbox 属性添加到 nav 元素。

#siteHeader {
max-width: 1600px;
padding-top: 15px;
height: 93px;
font-size: 12px;
text-transform: uppercase;
background-color: #000;
margin-left: auto;
margin-right: auto;
border-top: 1px solid #d7d5d5;
}
#siteHeader a {
text-decoration: none;
}
.logo {
display: block;
height: 55px;
width: auto;
float: left;
}

.nav {
display: flex;
flex-direction: row;
justify-content: space-between;
}

.right, .left{
display: inline-flex;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
width: auto;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 6px 16px;
text-decoration: none;
float: left;
}
li a:hover {
background-color: #000;
}
.dropbtn {
background-color: #000;
color: white;
padding-top: 6px;
padding-bottom: 25px;
border: none;
cursor: pointer;
min-width: 100px;
text-transform: uppercase;
}
.dropdown {
}
.dropdown-content {
display: none;
position: absolute;
background-color: #434343;
min-width: 185px;
right: 9px;
}
.dropdown-content a {
color: white;
padding: 13px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #000000
}
.dropdown:hover .dropdown-content {
display: block;
float: left;
}
.dropdown:hover .dropbtn {
background-color: #434343;
color: white;
}
<div id="siteHeader">
<div class="container-fluid normalize">
<div class="nav">
<ul class="left">
<li><a href="#home">Link</a>
</li>
<li><a href="#news">Link</a>
</li>
<li><a href="#news">Link</a>
</li>
</ul>
<img src="content/images/logo.png" class="logo">
<div class="right">
<ul>
<li><a href="#home">Link</a>
</li>
<li><a href="#news">Link</a>
</li>
</ul>
<div class="dropdown">
<button class="dropbtn">Dropdown <span>+</span>
</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
</div>

</div>
</div>
</div>

关于html - 菜单对齐居中和 float 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38905808/

25 4 0