当我将鼠标悬停在" 下拉菜单> "时,下拉菜单会显示在顶部栏下方的左侧。如何在" 下拉>&#;"
下方对齐它?请注意,我只尝试使用CSS和HTML执行此操作。
我的代码:
* {
margin: 0;
padding: 0;
}
header {
position: fixed;
height: 35px;
width: 100%;
background: black;
}
.top-container {
margin-top: 7px;
}
/* Code for drop-down list */
.dropdown {
margin-left: 100px;
display: inline;
color: #FFF;
}
.dropdown_list {
list-style: none;
display: none;
position: absolute;
color: red;
}
.dropdown_list li {
background: yellow;
}
.dropdown:hover {
background: #333;
}
.dropdown:hover .dropdown_list,
.dropdown_list:hover {
display: block;
top: 100%;
}

<body>
<header>
<div class="top-container">
<div class="dropdown">Dropdown ❱
<ul class="dropdown_list">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</div>
</div>
</header>
</body>
&#13;
答案 0 :(得分:6)
将.dropdown
从inline
更改为inline-block
:
.dropdown {
display: inline-block;
}
<强>段:强>
* {
margin: 0;
padding: 0;
}
header {
position: fixed;
height: 35px;
width: 100%;
background: black;
}
.top-container {
margin-top: 7px;
}
/* Code for drop-down list */
.dropdown {
margin-left: 100px;
display: inline-block;
color: #FFF;
}
.dropdown_list {
list-style: none;
display: none;
position: absolute;
color: red;
}
.dropdown_list li {
background: yellow;
}
.dropdown:hover {
background: #333;
}
.dropdown:hover .dropdown_list,
.dropdown_list:hover {
display: block;
top: 100%;
}
&#13;
<body>
<header>
<div class="top-container">
<div class="dropdown">Dropdown ❱
<ul class="dropdown_list">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</div>
</div>
</header>
</body>
&#13;
答案 1 :(得分:1)
试试这个
将position:relative
添加到课程.dropdown
,现在您的.dropdown_list
与.dropdown
相对,您可以使用top left right bottom
将.dropdown_list
作为您想。
* {
margin: 0;
padding: 0;
}
header {
position: fixed;
height: 35px;
width: 100%;
background: black;
}
.top-container {
margin-top: 7px;
}
/* Code for drop-down list */
.dropdown {
margin-left: 100px;
display: inline;
color: #FFF;
position: relative;
}
.dropdown_list {
list-style: none;
display: none;
position: absolute;
color: red;
top: 0;
left: 0;
}
.dropdown_list li {
background: yellow;
}
.dropdown:hover {
background: #333;
}
.dropdown:hover .dropdown_list,
.dropdown_list:hover {
display: block;
top: 100%;
}
&#13;
<body>
<header>
<div class="top-container">
<div class="dropdown">Dropdown ❱
<ul class="dropdown_list">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</div>
</div>
</header>
</body>
&#13;
答案 2 :(得分:0)
将position: relative
添加到.dropdown
,将left:0
添加到.dropdown_list
.dropdown {
margin-left: 100px;
display: inline;
position: relative;/*add this*/
color: #FFF;
}
.dropdown_list {
list-style: none;
display: none;
position: absolute;
color: red;
left: 0; /*add this*/
}
答案 3 :(得分:0)
将left-margin
应用于容器,并为弹出菜单设置position: relative
。