无法让菜单正常工作

时间:2015-02-03 09:25:09

标签: html css

我正在尝试使用子菜单创建菜单。事情就是当我将鼠标悬停在一个类别上时,整个div都会增加。我希望子菜单扩展到div以下,并使类别保持与彼此内联。 该网站是挪威语。



html{
    margin:0px;
    padding: 0px;
}
body{
    margin: 0px;
    padding:0px;
}
#wrapper{
    width: 100%;
    height: auto;   
}
#menu{
    width: 100%;
    height: auto;
    background-color:#B13436;
    margin: 0px;
    top:0;
    text-align: center;
}
#menu li{
    display: inline-block;
    margin: 10px;
    background-color:#327222;
    padding-left: 10px;
    padding-right: 10px;
    padding-top: 3px;
    padding-bottom: 3px;
    border-radius: 10px;
    min-width: 150px;
}
#menu ul{
    margin:0px;
}
#menu ul li a{
    text-decoration:none;
    color: #FFFFFF;
    font-size:24px;
}
#menu ul li ul{
    padding-top: 5px;
    padding-left: 0px;
    margin-top: 0px;
    display: none;
}
#menu ul li ul li{
    margin: 0px;
    padding-left: 0px;
    border-radius: 0px;
    display: block;
}
#menu ul li ul li a{
    font-size: 12px;
}
#menu ul li:hover ul{
    display: block;
}

<div id="wrapper">

    <div id="menu">
    <ul>
        <li> <a href="./">Home</a> </li>
    <li> <a href="./">Mat</a>
        <ul>
            <li><a href="./">Oppskrifter</a></li>
            <li><a href="./">Handlelister</a></li>
        </ul>
    </li>
    <li> <a href="./">Design&kunst </a> 
        <ul>
            <li><a href="./">Grafisk Design</a></li>
            <li><a href="./">Kunst</a></li>
        </ul>
    </li>
    <li> <a href="./">Sex&Samliv </a> </li>
        <li> <a href="./">Sport</a></li>
        <li> <a href="./">Spill</a> </li>
    </ul>
</div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

position: absolute;top: 100%;z-index: 222;left: 0;width: 100%;添加到#menu ul li ul,将position: relative添加到#menu li

html{
    margin:0px;
    padding: 0px;
}
body{
    margin: 0px;
    padding:0px;
}
#wrapper{
    width: 100%;
    height: auto;   
}
#menu{
    width: 100%;
    height: auto;
    background-color:#B13436;
    margin: 0px;
    top:0;
    text-align: center;
}
#menu li{
    display: inline-block;
    margin: 10px;
    background-color:#327222;
    padding-left: 10px;
    padding-right: 10px;
    padding-top: 3px;
    padding-bottom: 3px;
    border-radius: 10px;
    min-width: 150px;
    position: relative;
}
#menu ul{
    margin:0px;
}
#menu ul li a{
    text-decoration:none;
    color: #FFFFFF;
    font-size:24px;
}
#menu ul li ul{
    padding-top: 5px;
    padding-left: 0px;
    margin-top: 0px;
    display: none;
    position: absolute;
    top: 100%;
    z-index: 222;
    left: 0;
    width: 100%;
}
#menu ul li ul li{
    margin: 0px;
    padding-left: 0px;
    border-radius: 0px;
    display: block;
}
#menu ul li ul li a{
    font-size: 12px;
}
#menu ul li:hover ul{
    display: block;
}
<!doctype html>
<html lang="nb">
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="./main.css"/>
<title> Forum </title>
<!--<meta http-equiv="refresh" content="3"/>-->
</head>

<body>
<div id="wrapper">

    <div id="menu">
    <ul>
        <li> <a href="./">Home</a> </li>
    <li> <a href="./">Mat</a>
        <ul>
            <li><a href="./">Oppskrifter</a></li>
            <li><a href="./">Handlelister</a></li>
        </ul>
    </li>
    <li> <a href="./">Design&kunst </a> 
        <ul>
            <li><a href="./">Grafisk Design</a></li>
            <li><a href="./">Kunst</a></li>
        </ul>
    </li>
    <li> <a href="./">Sex&Samliv </a> </li>
        <li> <a href="./">Sport</a></li>
        <li> <a href="./">Spill</a> </li>
    </ul>
</div>
</div>
</body>
</html>

答案 1 :(得分:0)

你去......

将ul li ul设置为绝对位置将允许它移动而不影响包含的li。然后通过在下拉列表中添加z-index,可以将其放在下面的li上面。

同样将背景(绿色)应用于ul li ul允许颜色通过下拉菜单继续保留。

希望这有帮助。

&#13;
&#13;
html{
    margin:0px;
    padding: 0px;
}
body{
    margin: 0px;
    padding:0px;
}
#wrapper{
    width: 100%;
    height: auto;   
}
#menu{
    width: 100%;
    height: auto;
    background-color:#B13436;
    margin: 0px;
    top:0;
    text-align: center;
}
#menu li{
    display: inline-block;
    margin: 10px;
    background-color:#327222;
    padding-left: 10px;
    padding-right: 10px;
    padding-top: 3px;
    padding-bottom: 3px;
    border-radius: 10px;
    min-width: 150px;
/* ADD POSITION RELATIVE TO CONTAIN THE ABSOLUTE POSITIONING ON THE DROPDOWN */
	position: relative;
}
#menu ul{
    margin:0px;
}
#menu ul li a{
    text-decoration:none;
    color: #FFFFFF;
    font-size:24px;
	display: block;
}
#menu ul li ul{
    padding-top: 5px;
    padding-left: 0px;
    margin-top: 0px;
    display: none;
/* BACKGROUND COLOUR AND ABSOLUTE POSITION APPLIED */
    background-color:#327222;
	position: absolute;
	top: 100%;
	left: 0;
	width: 100%;
	z-index: 2;
}
#menu ul li ul li{
    margin: 0px;
    padding-left: 0px;
    border-radius: 0px;
    display: block;
}
#menu ul li ul li a{
    font-size: 12px;
}
#menu ul li:hover ul{
    display: block;
}
/* REMOVE THE BORDER RADIUS ON THE DROPDOWNS */
#menu li.dropdown:hover,
#menu li.dropdown > ul:hover {
border-radius: 10px 10px 0 0;
}
&#13;
<div id="wrapper">

    <div id="menu">
    <ul>
        <li> <a href="./">Home</a> </li>
    <li class="dropdown"> <a href="./">Mat</a>
        <ul>
            <li><a href="./">Oppskrifter</a></li>
            <li><a href="./">Handlelister</a></li>
        </ul>
    </li>
    <li class="dropdown"> <a href="./">Design&kunst </a> 
        <ul>
            <li><a href="./">Grafisk Design</a></li>
            <li><a href="./">Kunst</a></li>
        </ul>
    </li>
    <li> <a href="./">Sex&Samliv </a> </li>
        <li> <a href="./">Sport</a></li>
        <li> <a href="./">Spill</a> </li>
    </ul>
</div>
</div>
&#13;
&#13;
&#13;