html - 防止 Div 移动

标签 html css

我正在使用一个在标题 div 中带有 CSS 下拉菜单的网站。当下拉菜单出现时,它会调整标题 div 的大小,从而向下推内容 div。

在我的 index.php 的正文中:

<div class="top"></div>
<div class="container">
<?php include_once("header.php"); ?>
<div class="content"></div>
</div>

header.php

<div class="header">
<div style="display: table-cell; width: 250px; text-align: center;">
LOGO
</div>
<div style="display: table-cell;">
<br><br><br>
<ul>
<li><a href="#">Link 1</a>
<ul>
<li><a href="#">Monkey 1</a></li>
<li><a href="#">Monkey 2</a></li>
<li><a href="#">Monkey 3</a></li>
<li><a href="#">Monkey 4</a></li>
<li><a href="#">Monkey 5</a></li>
<li><a href="#">Monkey 6</a></li>
<li><a href="#">Monkey 7</a></li>
</ul>
</li>
<li><a href="#">Link 2</a>
<ul>
<li><a href="#">Monkey 1</a></li>
<li><a href="#">Monkey 2</a></li>
</ul>
</li>
<li>
<a href="#">Link 3</a>
<ul>
<li><a href="#">Monkey 1</a></li>
<li><a href="#">Monkey 2</a></li>
<li><a href="#">Monkey 3</a></li>
</ul>
</li>
<li><a href="#">Link 4</a></li>
<li><a href="#">Link 5</a></li>
<li><a href="#">Link 6</a></li>
</ul>
</div>
</div>

和 main.css

ul{
    padding: 0;
    list-style: none;
}
ul li{
    float: left;
    width: 100px;
    text-align: center;
}
ul li a{
    display: block;
    padding: 5px 10px;
    color: #333;
    background: #f2f2f2;
    text-decoration: none;
}
ul li a:hover{
    color: #fff;
    background: #939393;
}
ul li ul{
    display: none;
}
ul li:hover ul{
    display: block; /* display the dropdown */
}
.top{
    background: #1b2d3c;
    width: 100%;
    height: 40px;
}
.container{
    width: 1100px;
    height: 1000px;
}
.header{
    display: table-row;
    width: 1100px;
    height: 130px;
}
.content{
    position: absolute;
    background: #fff;
    width: 1100px;
    height: 500px;
}
* {
    background: #87a0b4;
    margin: 0px;
    padding: 0px;
    margin-left: auto ;
    margin-right: auto ;
}

对于冗长的帖子,我深表歉意。我怎样才能防止我遇到的问题?在我深入到元素中之前,我想确保我做的事情是正确的。

最佳答案

您需要将 ul li:hover ulposition 更改为 absolute,并添加一些其他属性,例如 JSFiddel ( Source )

ul li:hover ul {
    display: block;
    position: absolute;
    width: 100px;
    z-index: 100;
}

希望这对你有帮助..

关于html - 防止 Div 移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24479074/

相关文章:

css3 z-index 问题,不工作?

css 结合 id 和 class

css - 为什么将鼠标悬停在绝对/相对 div 上时菜单会变得透明?

javascript - 最大高度不起作用

html - 如何将内容保留在容器中,但在 CSS 中扩展它的背景全宽?

php - GROUP CONCAT 和 Anchor URL 问题

javascript - 如何用jquery平滑移动模拟类的秒针

javascript - 使 div 在某个点后出现并在另一个特定点后消失

javascript - 如何通过knockout js启用/禁用可编辑div上的按钮

css - 为什么我的 CSS 宽度百分比在某些页面上会发生变化?