将子菜单(下拉)方向从垂直更改为水平

时间:2018-09-05 07:30:00

标签: html css navbar

现在我在垂直方向上有这样的下拉菜单:

enter image description here 我想更改下拉菜单,使它们在水平方向上出现,最好在菜单项的中央:

enter image description here 我正在使用div(.dropdown,.dropdown-content)

.dropdown {
    float: left;
    overflow: hidden;
}

.dropdown .dropbtn {
    font-size: 16px;    
    outline: none;
    background-image: linear-gradient(#F1F1F1, #E5E5E5);
    font-family: inherit;
    margin: 0;
    width: 150px;
    height: 160px;
    border: 1px solid white;
    border-bottom-left-radius: 5px;
    border-bottom-right-radius: 5px;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-image: linear-gradient(#F1F1F1, #E5E5E5);
    border: 1px solid white;
    width: 150px;
    height: auto;
    border-bottom-left-radius: 5px;
    border-bottom-right-radius: 5px;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
    padding-bottom: 10px;
}

如何更改方向和对齐方式?

<div class="dropdown">
    <button class="dropbtn" data-showdiv="CO">
        <div class="titleh2">Title</div>
        <img class="orga" src="picture.png"> 
        <div class="titleh1">Name</div>
        <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
        <a href="#" class="firstImage">
            <img class="orga" src="\untitled.png" />
        </a>
        <div class="titleh2">Title</div><br>
        <div class="titleh3">Name</div>
        <hr class="horizontal">
        <a href="#">
            <img class="orga" src="\untitled.png">
        </a>
        <div class="titleh2">Title</div><br>
        <div class="titleh3">Name</div>
        <hr class="horizontal">
        <a href="#">
            <img class="orga" src="\untitled.png">
        </a>
        <div class="titleh2">Title</div><br>
        <div class="titleh3">Name</div>
    </div>
</div> 

1 个答案:

答案 0 :(得分:1)

您可以使用FlexBox获得所需的结果。请记住,flexbox是最新推出的,较旧的浏览器不支持。

我做了什么:

dropdown-content的显示设置更改为display:flex;,并添加了flex-direction: row以使其水平对齐。

然后,我用div包裹了所有dropdown-elements,以便在需要时为dropdown的每个元素赋予一些样式。

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  font-size: 16px;
  outline: none;
  background-image: linear-gradient(#F1F1F1, #E5E5E5);
  font-family: inherit;
  margin: 0;
  width: 150px;
  height: 160px;
  border: 1px solid white;
  border-radius: 5px;

}

.dropdown-content {
  /*display: none; //Change this to display flex, with the flex-direction to align the items horizontally*/
  display: flex;
  flex-direction: row;
  position: absolute;
  background-image: linear-gradient(#F1F1F1, #E5E5E5);
  border: 1px solid white;
  height: auto;
  border-radius: 5px;
  padding-bottom: 10px;
}

.dropdown-content-element{
  margin: 2px 5px;
  padding: 5px;
}
<div class="dropdown">
  <button class="dropbtn" data-showdiv="CO">
        <div class="titleh2">Title</div>
        <img class="orga" src="picture.png"> 
        <div class="titleh1">Name</div>
        <i class="fa fa-caret-down"></i>
    </button>
  <div class="dropdown-content">
    <div class="dropdown-content-element">
      <a href="#" class="firstImage">
        <img class="orga" src="\untitled.png" />
      </a>
      <div class="titleh2">Title</div>
      <div class="titleh3">Name</div>
    </div>
    <div class="dropdown-content-element">
      <a href="#">
        <img class="orga" src="\untitled.png">
      </a>
      <div class="titleh2">Title</div>
      <div class="titleh3">Name</div>
    </div>
    <div class="dropdown-content-element">
      <a href="#">
        <img class="orga" src="\untitled.png">
      </a>
      <div class="titleh2">Title</div>
      <div class="titleh3">Name</div>
    </div>
  </div>
</div>

以下是一些小技巧,可帮助您开始使用get_compiled_select

编辑: 这可行,但不以所选超级菜单项为中心。但是您会自己实现的,我相信您!

哦,顺便说一句:如果所有的半径半径规则都相同,则可以用border-radius: 5px;总结边界半径规则。节省空间并保持更好的可读性。

编辑2:

以下是使用ulli获得相似结果的示例。 @Moose在他的评论之一中建议了这种方法。

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  font-size: 16px;
  outline: none;
  background-image: linear-gradient(#F1F1F1, #E5E5E5);
  font-family: inherit;
  margin: 0;
  width: 150px;
  height: 160px;
  border: 1px solid white;
  border-radius: 5px;
}

.dropdown-content {
  /*display: none;*/
  position: absolute;
  background-image: linear-gradient(#F1F1F1, #E5E5E5);
  border: 1px solid white;
  height: auto;
  border-radius: 5px;
  padding-bottom: 10px;
}

.dropdown-list {
  list-style: none;
}

.dropdown-list>li {
  display: inline-block;
}
<div class="dropdown">
  <button class="dropbtn" data-showdiv="CO">
        <div class="titleh2">Title</div>
        <img class="orga" src="picture.png"> 
        <div class="titleh1">Name</div>
        <i class="fa fa-caret-down"></i>
    </button>
  <div class="dropdown-content">
    <ul class="dropdown-list">
      <li>
        <a href="#" class="firstImage">
          <img class="orga" src="\untitled.png" />
        </a>
        <div class="titleh2">Title</div>
        <div class="titleh3">Name</div>
      </li>
      <li>
        <a href="#">
          <img class="orga" src="\untitled.png">
        </a>
        <div class="titleh2">Title</div>
        <div class="titleh3">Name</div>
      </li>
      <li>
        <a href="#">
          <img class="orga" src="\untitled.png">
        </a>
        <div class="titleh2">Title</div>
        <div class="titleh3">Name</div>
      </li>
    </ul>
  </div>
</div>