我想垂直对齐a
dropdown-menu.
链接1-4
在此示例中,我尝试将div
显示为table-row
,但没有任何效果。
CODE
body {
font-family: 'Arial', sans-serif;
max-width: 960px;
margin: 0 auto;
padding: 20px;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #1ebb90;
overflow: hidden;
}
li {
float: left;
}
li a,
.dropdown-btn {
display: inline-block;
padding: 18px 22px;
}
div {
display: table-row;
}
div a {
display: inline;
vertical-align: middle;
}
<ul>
<li><a href="#">Home</a>
</li>
<li><a href="#">About</a>
</li>
<li><a href="#">Blog</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-btn">Dropdown</a>
<div class="dropdown-menu">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
<a href="#">Link 4</a>
</div>
</li>
</ul>
答案 0 :(得分:1)
有不同的方法可以做到这一点,但一种简单的方法是在链接上使用display: block
。
(fiddle)
Here's a related question/answer与内联与阻止工作有关。
答案 1 :(得分:1)
[1]摆脱overflow: hidden
ul
[2]让下拉列表绝对定位:
.dropdown-menu {
position: absolute;
top: 100%;
left: 0;
visibility: hidden;
}
[3]建立li
作为父母。
li {
...
position: relative;
}
[4]在隐藏下拉菜单旁边的锚点上设置悬停规则。
.dropdown:hover .dropdown-menu {
visibility: visible;
}
演示:https://jsfiddle.net/b3gbowrn/
body {
font-family: 'Arial', sans-serif;
max-width: 960px;
margin: 0 auto;
padding: 20px;
}
ul {
list-style-type: none;
margin:0;
padding: 0;
background-color: #1ebb90;
}
li {
float: left;
position: relative;
border: 1px solid rgba(0,0,0,0.25);
border-right: none;
}
li:last-child {
border: 1px solid rgba(0,0,0,0.25);
}
.dropdown:hover .dropdown-menu {
visibility: visible;
}
.dropdown-menu {
position: absolute;
top: 100%;
left: 0;
visibility: hidden;
}
li a, .dropdown-btn {
display: inline-block;
padding: 18px 22px;
}
div {
display: table-row;
}
div a {
display: inline;
vertical-align: middle;
}
&#13;
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Blog</a></li>
<li class="dropdown">
<a href="#" class="dropdown-btn">Dropdown</a>
<div class="dropdown-menu">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
<a href="#">Link 4</a>
</div>
</li>
</ul>
&#13;
答案 2 :(得分:1)
在inline-block
li
使用下拉列表中的ul li
适用于您的下拉列position:absolute
和top:100%
relative
至li
如果您想要显示/隐藏,可以在:hover
li
body {
font-family: 'Arial', sans-serif;
max-width: 960px;
margin: 0 auto;
padding: 20px;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #1ebb90;
}
li {
display: inline-block;
vertical-align: top;
position: relative
}
li a,
.dropdown-btn {
display: block;
padding: 18px 22px;
}
li ul {
position: absolute;
top: 100%;
left: 0;
background: red;
display: block;
width: 100%;
text-align: center;
display: none
}
li:hover ul {
display: block
}
&#13;
<ul>
<li><a href="#">Home</a>
</li>
<li><a href="#">About</a>
</li>
<li><a href="#">Blog</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-btn">Dropdown</a>
<ul class="dropdown-menu">
<li> <a href="#">Link 1</a>
</li>
<li> <a href="#">Link 2</a>
</li>
<li> <a href="#">Link 3</a>
</li>
<li> <a href="#">Link 4</a>
</li>
</ul>
</li>
</ul>
&#13;