如何在该代码中垂直对齐li中的元素?

时间:2016-05-17 23:09:21

标签: html css html5 css3

我想垂直对齐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>

3 个答案:

答案 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/

&#13;
&#13;
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;
&#13;
&#13;

答案 2 :(得分:1)

  • inline-block

  • 中使用li
  • 使用下拉列表中的ul li

  • 将您的HTML语义更正为菜单
  • 适用于您的下拉列position:absolutetop:100% relativeli

  • 如果您想要显示/隐藏,可以在:hover

  • 中使用li

&#13;
&#13;
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;
&#13;
&#13;