如何从位置获取自动高度:绝对元素?

时间:2016-06-09 14:01:20

标签: html css

我有一个深度为3的菜单。我想要留下第二层,第三层放在下拉列表中。

第三层需要绝对定位,否则它将不适合第二层的左侧,但是当定位绝对时,高度不会与绝对定位的<ul>一致。它只到达第二层 - <li> - 身高。

段:

#main_navigation {
  background-color: #006600;
  position: relative;
  height: 35px;
  font-size: 10px;
}
#main_navigation ul {
  list-style-type: none;
}
#main_navigation>nav>ul>li {
  background: none;
  float: none;
  display: inline-block;
  margin-right: -3px;
  line-height: inherit;
  border-left: 1px solid #6bb36b;
  position: static;
}
#main_navigation>nav>ul>li>a {
  color: #fff;
  text-decoration: none;
  font-size: 1.5em;
  background-color: transparent;
  display: inline-block;
  float: none;
  padding: 10px;
  padding-top: 8px;
  padding-bottom: 9px;
  transition: background-color ease-in-out 0.46s;
  font-weight: bold; 
}
#main_navigation>nav>ul>li:hover>a, #main_navigation #navigation>ul>li:focus>a {
  background-color: #6bb36b;
  transition: background-color ease-in-out 0.35s;
  text-decoration: none;
  font-weight: bold;
}
#main_navigation>nav>ul>li.dropdown_parent>ul.dropdown {
  top: 300%;
  z-index: 1000;
  opacity: 0;
  visibility: hidden;
  display: block;
  width: 100%;
  margin-top: 0;
  background: none;
  background-color: #006600;
  position: absolute;
  left: 0;
  padding: 0;
  box-shadow: 0px 5px 12px 0px #333;
  border-top: 2px solid #6bb36b;
  border-bottom: 2px solid #081a08;
  transition: all ease-out 0.45s;
}
#main_navigation>nav>ul>li.dropdown_parent:hover>ul.dropdown, #main_navigation>nav>ul>li.dropdown_parent:focus>ul.dropdown {
  top: 35px;
  z-index: 1000;
  opacity: 1;
  visibility: visible;
  transition: all ease-in 0.35s;
}
#main_navigation>nav>ul>li.dropdown_parent>ul.dropdown>li.parent {
  width: 50%;
  display: inline-block;
  vertical-align: top;
  font-size: 16px;
  position: relative;
}
#main_navigation>nav>ul>li.dropdown_parent>ul.dropdown>li.parent>ul.child {
  position: absolute;
  width: 100%;
  top: 0;
  left: 100%;
  padding: 0;
}
#main_navigation>#navigation>ul>li.dropdown_parent>ul.dropdown>li.parent a:not(.childtrigger) {
  font-size: 15px;
  display: block;
  padding: 10px;
  color: #fff;
  margin-right: 50px;
  text-decoration: none;
  transition: all ease-in-out 0.3s;
}
#main_navigation>#navigation>ul>li.dropdown_parent>ul.dropdown>li.parent a:not(.childtrigger):hover, #main_navigation>#navigation>ul>li.dropdown_parent>ul.dropdown>li>li.parent a:not(.childtrigger):focus, #main_navigation>#navigation>ul>li.dropdown_parent>ul.dropdown>li.parent.active a:not(.childtrigger) {
  background-color: #6bb36b;
  transition: all ease-in-out 0.3s;
}
#main_navigation>#navigation>ul>li.dropdown_parent>ul.dropdown>li.parent a.childtrigger {
  background: rgba(3, 2, 1, 0);
  width: 50px;
  padding: 0;
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 2;
  cursor: pointer;
  transition: all ease-in-out 0.3s;
}
#main_navigation>#navigation>ul>li.dropdown_parent>ul.dropdown>li.parent a.childtrigger:after {
  content: "";
  border: 2px solid transparent;
  display: inline-block;
  width: 8px;
  height: 8px;
  margin: auto;
  position: absolute;
  top: 0;
  bottom: 0;
  -webkit-transform: rotate(-45deg);
  -moz-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  transform: rotate(-45deg);
  right: 23px;
  border-top: none;
  border-left: none;
  border-color: #fff;
  transition: all ease-in-out 0.3s;
}
#main_navigation>#navigation>ul>li.dropdown_parent>ul.dropdown>li.parent a.childtrigger:hover, #main_navigation>#navigation>ul>li.dropdown_parent>ul.dropdown>li.parent a.childtrigger:focus {
  background-color: #6bb36b;
  transition: all ease-in-out 0.3s;
}
#main_navigation>#navigation>ul>li.dropdown_parent>ul.dropdown>li.parent a.childtrigger:hover:after, #main_navigation>#navigation>ul>li.dropdown_parent>ul.dropdown>li.parent a.childtrigger:focus:after {
  border-color: #fff;
  transition: all ease-in-out 0.3s;
}
<div id="main_navigation">
  <nav id="navigation">
    <ul>
      <li><a href="#">Home</a></li>
      <li class="dropdown_parent active">
        <a href="#">Dropdown</a>
        <ul class="dropdown">
          <li class="parent">
            <a class="childtrigger"></a>
            <a href="#">Submenuparent</a>
            <ul class="child">
              <li><a href="#">Bla</a></li>
              <li><a href="#">Bla</a></li>
              <li><a href="#">Bla</a></li>
              <li><a href="#">Bla</a></li>
              <li><a href="#">Bla</a></li>
              <li><a href="#">Bla</a></li>
            </ul>
          </li>
          <li class="parent">
            <a class="childtrigger"></a>
            <a href="#">Submenuparent 2</a>
            <ul class="child">
              <li><a href="#">Bla 2</a></li>
              <li><a href="#">Bla 2</a></li>
              <li><a href="#">Bla 2</a></li>
              <li><a href="#">Bla 2</a></li>
              <li><a href="#">Bla 2</a></li>
              <li><a href="#">Bla 2</a></li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </nav>
</div>

小提琴:https://jsfiddle.net/u5gwqzfo/

它的外观如下:http://i.imgur.com/pqraNlX.jpg

非常感谢提前!

1 个答案:

答案 0 :(得分:1)

您无法使用绝对定位的.child调整.parent的大小。绝对定位会从框模型中删除该元素。

如果您绑定到此标记结构,则可以使用javascript将.dropdown的高度设置为与.child的高度相同

(我用过jquery)

类似的东西:

$(".parent").hover(function() {
    x = $(this).find(".child").height();
$(this).parent('.dropdown').css({
    'height': x + 'px'
   });
 }, function() {
  $(this).parent('.dropdown').css({
    'height': 'auto'
  });
});

我这是(一个非常笨拙的)笔来证明我的意思。 http://codepen.io/NeilWkz/pen/YWwpgJ