我有两个酒吧: 1.顶栏 - 名称,搜索栏,帐户,购物车 2.导航栏(带下拉菜单)
问题在于顶栏。我试图让所有元素都在同一条线上,右侧是Account和Cart。我尝试使用flex但现在搜索栏与其他元素不一致。活动帐户和购物车有点离线。当我不使用flex并且只是尝试将Account和Cart浮动到右边时,它们被交换(Cart首先出现,然后是Account),而且它们也没有排成一行,它们有点漂浮在顶层右。
HTML:
<div class="top">
<h1>TDX</h1>
<form>
<div>
<input type="search" id="mySearch" name="q"
placeholder="Search the site..." size="50">
<button>Search</button>
</div>
</form>
<a href="account.html"><h4>Account</h4></a>
<a href="cart.html"><h4>Cart</h4></a>
</div>
<ul class="topnav">
<li><a class="active" href="index.html">Home</a></li>
<li class="dropdown"><a href="javascript:void(0)" class="dropbtn">Products</a>
<div class="dropdown-content">
<a href="#">Computers</a>
<a href="#">Tablets</a>
<a href="#">Cell Phones</a>
<a href="#">Wearable Technologies</a>
<a href="#">Accessories</a>
</div>
</li>
<li class="dropdown"><a href="javascript:void(0)" class="dropbtn">Brands</a>
<div class="dropdown-content">
<a href="#">Apple</a>
<a href="#">Samsung</a>
<a href="#">Lenovo</a>
<a href="#">Dell</a>
<a href="#">HP</a>
<a href="#">Sony</a>
<a href="#">Panasonic</a>
<a href="#">Motorola</a>
<a href="#">HTC</a>
</div>
</li>
<li><a href="deals.html">Deals</a></li>
</ul>
CSS:
.topnav{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
margin:0 auto;
text-align:center;
}
.topnav li {
float: left;
}
.topnav li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
width:120px;
}
.topnav li a:hover, .dropdown:hover .dropbtn {
background-color: #111;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.topnav .dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.topnav .dropdown-content a:hover {background-color: #f1f1f1;}
.dropdown:hover .dropdown-content {
display: block;
}
.top{
display: flex;
align-content: space-between;
}
.top a{
margin: 0 5px;
}
.top h1, .top a{
flex: 0 1 auto;
}
.top form{
flex: 1 1 auto;
vertical-align:middle;
}
答案 0 :(得分:0)
将align-items: center;
添加到CSS中的.top
元素。
您更新的.top
CSS元素:
.top {
display: flex;
align-content: space-between;
align-items: center;
}