如何在图像上重叠菜单列表

时间:2013-08-01 23:36:25

标签: css

单击菜单“产品”或鼠标悬停时,会出现另一个菜单列表..但菜单栏下方的图像块会移离该位置。如果我使用css [position:absolute;],那么图像框保持静态,产品的子菜单与图像块重叠,这就是我想要的。但图像块宽度和宽度高度设置急剧变化,从而破坏对齐。  请查看jsFiddle

中的编码

.home_menu {
  border: 1px solid black;
  width: 98%;
  height: 3.3%;
  margin-right: auto;
  margin-left: auto;
}
div#menuDemo {
  clear: both;
  //border:1px solid black;
  height: 78%;
  width: 100%;
  position: relative;
  left: 0;
  top: 0;
  background-color: #A55927;
  /*Remove this next one in production - Used for demo purpose only*/
  margin-bottom: 0.1%;
  padding-top: 0.7%;
  z-index: 4;
}
div#menuDemo ul {
  list-style: none;
  margin: 0;
  padding: 0;
  background-color: #A55927;
}
div#menuDemo > ul > li {
  float: left;
  text-align: center;
}
div#menuDemo ul li {
  width: 25%;
  //border: 5px solid purple;

}
div#menuDemo ul li a {
  text-decoration: none;
  font-weight: bolder;
  text-align: center;
}
div#menuDemo > ul > li > ul {
  display: none;
  text-align: center;
}
div#menuDemo > ul > li:hover > ul {
  display: block;
  text-align: center;
}
.sub1 {
  width: 100%;
  //border:1px solid green;

}
.colouring {
  color: black;
  font-weight: bolder;
}
.colour {
  //border:1px solid blue;
  color: black;
  text-align: center;
  //width:100%;

}
.wrapper {
  border: 5px solid pink;
  width: 98.8%;
  height: 82%;
  margin-top: 1%;
  z-index: 2;
}
.uniform_block {
  border: 5px solid green;
  width: 100%;
  height: 40%;
  cursor: pointer;
}
.uniform_block img {
  width: 100%;
  height: 100%;
}
<body>
  <div class="home_menu">
    <div id="menuDemo">
      <ul>
        <li id="homeMenu"><a href="#menuHome" class="colour">About Us</a>
        </li>

        <!--  <li ><a href="#menuHome" class="colour">About Us</a></li> -->

        <li><a href="#" class="colour" id="expand">Products</a>
          <ul class="sub1">
            <li> <a href="#" class="colouring"> Uniforms </a>
              <ul>
                <li> <a href="#" class="colour"> &nbsp;&nbsp </a>
                </li>
                <li> <a href="#" class="colour"> Automobile&nbsp;Industry&nbsp;Uniforms </a>
                </li>
                <li> <a href="#" class="colour"> Pharmaceutical&nbsp;Uniforms </a>
                </li>
                <li> <a href="#" class="colour"> Food&nbsp;Industry&nbsp;Uniforms </a>
                </li>
                <li> <a href="#" class="colour"> Government&nbsp;Sector&nbsp;Uniforms </a>
                </li>
                <li> <a href="#" class="colour"> School/College&nbsp;Uniforms </a>
                </li>
                <li> <a href="#" class="colour"> &nbsp;&nbsp </a>
                </li>
              </ul>
            </li>
            <li><a href="#" class="colouring">Shoes</a>
              <ul>
                <li> <a href="#" class="colour"> &nbsp;&nbsp </a>
                </li>
                <li> <a href="#" class="colour"> Industrial&nbsp;Shoes </a>
                </li>
                <li> <a href="#" class="colour"> Safety&nbsp;&&nbsp;Security&nbsp;Shoes </a>
                </li>
                <li> <a href="#" class="colour"> Executive&nbsp;Shoes</a>
                </li>
                <li> <a href="#" class="colour"> &nbsp;&nbsp </a>
                </li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href="#" class="colour">Contact Us</a> 
        </li>
      </ul>
    </div>
  </div>
  <div class="wrapper">
    <div class="uniform_block">
      <img src=" http://t0.gstatic.com/images?q=tbn:ANd9GcSH-kRi3rkVciPcH_c6dDJJI6C1ntzwcKl9MoVQIyuKk8F7unpf" />
    </div>
    <div class="home_footer">
      <div class="footer_contents"></div>
    </div>
  </div>
</body>

亲切的帮助。我的要求是,当我将鼠标悬停在“产品菜单”上时,应在菜单栏下方的图像块上方查看下拉菜单。

1 个答案:

答案 0 :(得分:2)

position:absolute 添加到ul菜单的css(在您的情况下为sub1类),并删除宽度:100%,以便它可以继承其父级的默认宽度。绝对定位将阻止您的浏览器尝试将ul元素放在页面上的上一个元素之后。

ul.sub1 {
    position:absolute;
}

http://jsfiddle.net/C2YXp/2/