2016-12-02 105 views
2

我是一名html初学者,我正在尝试开始在线投资组合工作,我已经设置了导航栏,现在我正在尝试从导航栏中创建一个下拉菜单。到目前为止,我还没有能够获得项目甚至进入下拉菜单,所以我想我没有正确设置容器。提前谢谢!HTML下拉菜单不起作用

/* nav bar */ 
 
body { 
 
    margin: 0; 
 
    padding: 0; 
 
    
 
} 
 
    
 
.nav ul { 
 
    list-style: none; 
 
    background-color: #444; 
 
    text-align: center; 
 
    padding: 15; 
 
    margin: 0; 
 
} 
 
.nav li { 
 
    font-family: 'Oswald', sans-serif; 
 
    font-size: 1.2em; 
 
    line-height: 40px; 
 
    height: 40px; 
 
    padding-right: 40px; 
 
    border-bottom: 1px solid #888; 
 
} 
 
    
 
.nav a { 
 
    text-decoration: none; 
 
    color: #fff; 
 
    display: block; 
 
    transition: .3s background-color; 
 
} 
 
    
 
.nav a:hover { 
 
    background-color: #005f5f; 
 
} 
 
    
 
.nav a.active { 
 
    background-color: #fff; 
 
    color: #444; 
 
    cursor: default; 
 
} 
 
    
 
@media screen and (min-width: 600px) { 
 
    .nav li { 
 
    width: 150px; 
 
    border-bottom: none; 
 
    height: 50px; 
 
    line-height: 50px; 
 
    font-size: 1.4em; 
 
    } 
 
    
 
    /* Option 1 - Display Inline */ 
 
    .nav li { 
 
    display: inline-block; 
 
    margin-right: -4px; 
 
    } 
 
    
 
    /* Options 2 - Float 
 
    .nav li { 
 
    float: left; 
 
    } 
 
    .nav ul { 
 
    overflow: auto; 
 
    width: 600px; 
 
    margin: 0 auto; 
 
    } 
 
    .nav { 
 
    background-color: #444; 
 
    } 
 
    */ 
 
    
 
    /*****************************************************************/ 
 
    /*Dropdown for portfolio tab */ 
 
    /*****************************************************************/ 
 
    /* Dropdown Button */ 
 

 
.dropbtn { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
    padding: 16px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

 
.dropdown { 
 
    position: relative; 
 
    display: inline-block; 
 
} 
 

 
.dropdown-content { 
 
    display: none; 
 
    position: absolute; 
 
    background-color: #f9f9f9; 
 
    min-width: 160px; 
 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
 
} 
 

 
.dropdown-content a { 
 
    color: black; 
 
    padding: 12px 16px; 
 
    text-decoration: none; 
 
    display: block; 
 
} 
 

 
.dropdown-content a:hover {background-color: #f1f1f1} 
 

 
.dropdown:hover + .dropdown-content { 
 
    display: block; 
 
} 
 

 
.dropdown:hover .dropbtn { 
 
    background-color: #3e8e41; 
 
} 
 
    
 
    
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <meta charset="UTF-8"> 
 
\t <META name="viewport" content="width=device-width, initial-scale=1"> 
 
<title> Will's Portfolio </title> 
 
<link rel="stylesheet" href="dropdown.css"> 
 
<link href='http://fonts.gooleapis.com/css?family=Oswald' 
 
rel='stylesheet' type='text/css'> 
 

 

 
</head> 
 
<body background = "http://2.bp.blogspot.com/-Xmo26BMqg5Q/UihlqVwTwgI/AAAAAAAAAv0/V-Rrgm0V6oo/s1600/Top+10+best+Simple+Awesome+Background+Images+for+Your+Website+or+Blog3.jpg"> 
 
<body class="About Me"> 
 
\t <header> 
 
\t \t <div class="nav"> 
 
\t \t \t <ul> 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t \t <li><a href="#">About Me</a></li> \t \t \t \t 
 
\t \t \t \t \t \t 
 
\t \t \t \t <li> 
 
\t \t \t \t <div class="dropdown"> 
 
\t \t \t \t <button class="dropbtn">Portfolio</button> 
 
\t \t \t \t <div class="dropdown-content"> 
 
\t \t \t \t \t <a href="#">Graphics</a> 
 
\t \t \t \t \t <a href="#">Other</a> 
 
\t \t \t \t \t 
 
\t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t \t </li> 
 
\t \t \t \t <li><a href="#"><nobr>Future Work</nobr></a></li> 
 
\t \t \t </ul> 
 
\t \t </div> \t 
 
\t \t 
 

 
\t </header> 
 
</div> 
 
\t \t 
 
\t \t 
 

 

 
</body> 
 
</html>

回答

0

你的问题是这个CSS选择器:

.dropdown:hover + .dropdown-content { 
    display: block; 
} 

它应该是:

.dropdown:hover .dropdown-content { 
    display: block; 
} 

/* nav bar */ 
 
body { 
 
    margin: 0; 
 
    padding: 0; 
 
    
 
} 
 
    
 
.nav ul { 
 
    list-style: none; 
 
    background-color: #444; 
 
    text-align: center; 
 
    padding: 15; 
 
    margin: 0; 
 
} 
 
.nav li { 
 
    font-family: 'Oswald', sans-serif; 
 
    font-size: 1.2em; 
 
    line-height: 40px; 
 
    height: 40px; 
 
    padding-right: 40px; 
 
    border-bottom: 1px solid #888; 
 
} 
 
    
 
.nav a { 
 
    text-decoration: none; 
 
    color: #fff; 
 
    display: block; 
 
    transition: .3s background-color; 
 
} 
 
    
 
.nav a:hover { 
 
    background-color: #005f5f; 
 
} 
 
    
 
.nav a.active { 
 
    background-color: #fff; 
 
    color: #444; 
 
    cursor: default; 
 
} 
 
    
 
@media screen and (min-width: 600px) { 
 
    .nav li { 
 
    width: 150px; 
 
    border-bottom: none; 
 
    height: 50px; 
 
    line-height: 50px; 
 
    font-size: 1.4em; 
 
    } 
 
    
 
    /* Option 1 - Display Inline */ 
 
    .nav li { 
 
    display: inline-block; 
 
    margin-right: -4px; 
 
    } 
 
    
 
    /* Options 2 - Float 
 
    .nav li { 
 
    float: left; 
 
    } 
 
    .nav ul { 
 
    overflow: auto; 
 
    width: 600px; 
 
    margin: 0 auto; 
 
    } 
 
    .nav { 
 
    background-color: #444; 
 
    } 
 
    */ 
 
    
 
    /*****************************************************************/ 
 
    /*Dropdown for portfolio tab */ 
 
    /*****************************************************************/ 
 
    /* Dropdown Button */ 
 

 
.dropbtn { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
    padding: 16px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

 
.dropdown { 
 
    position: relative; 
 
    display: inline-block; 
 
} 
 

 
.dropdown-content { 
 
    display: none; 
 
    position: absolute; 
 
    background-color: #f9f9f9; 
 
    min-width: 160px; 
 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
 
} 
 

 
.dropdown-content a { 
 
    color: black; 
 
    padding: 12px 16px; 
 
    text-decoration: none; 
 
    display: block; 
 
} 
 

 
.dropdown-content a:hover {background-color: #f1f1f1} 
 

 
.dropdown:hover .dropdown-content { 
 
    display: block; 
 
} 
 

 
.dropdown:hover .dropbtn { 
 
    background-color: #3e8e41; 
 
} 
 
    
 
    
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <meta charset="UTF-8"> 
 
\t <META name="viewport" content="width=device-width, initial-scale=1"> 
 
<title> Will's Portfolio </title> 
 
<link rel="stylesheet" href="dropdown.css"> 
 
<link href='http://fonts.gooleapis.com/css?family=Oswald' 
 
rel='stylesheet' type='text/css'> 
 

 

 
</head> 
 
<body background = "http://2.bp.blogspot.com/-Xmo26BMqg5Q/UihlqVwTwgI/AAAAAAAAAv0/V-Rrgm0V6oo/s1600/Top+10+best+Simple+Awesome+Background+Images+for+Your+Website+or+Blog3.jpg"> 
 
<body class="About Me"> 
 
\t <header> 
 
\t \t <div class="nav"> 
 
\t \t \t <ul> 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t \t <li><a href="#">About Me</a></li> \t \t \t \t 
 
\t \t \t \t \t \t 
 
\t \t \t \t <li> 
 
\t \t \t \t <div class="dropdown"> 
 
\t \t \t \t <button class="dropbtn">Portfolio</button> 
 
\t \t \t \t <div class="dropdown-content"> 
 
\t \t \t \t \t <a href="#">Graphics</a> 
 
\t \t \t \t \t <a href="#">Other</a> 
 
\t \t \t \t \t 
 
\t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t \t </li> 
 
\t \t \t \t <li><a href="#"><nobr>Future Work</nobr></a></li> 
 
\t \t \t </ul> 
 
\t \t </div> \t 
 
\t \t 
 

 
\t </header> 
 
</div> 
 
\t \t 
 
\t \t 
 

 

 
</body> 
 
</html>

3

dropdown-content类元素与dropdown类里面的元素,所以你要找的实际上是.dropdown:hover .dropdown-content

您当前的代码(+字符)告诉浏览器检查相邻兄弟姐妹(而不是孩子元素),这不是你的情况。

这是你的代码的更新:

/* nav bar */ 
 
body { 
 
    margin: 0; 
 
    padding: 0; 
 
    
 
} 
 
    
 
.nav ul { 
 
    list-style: none; 
 
    background-color: #444; 
 
    text-align: center; 
 
    padding: 15; 
 
    margin: 0; 
 
} 
 
.nav li { 
 
    font-family: 'Oswald', sans-serif; 
 
    font-size: 1.2em; 
 
    line-height: 40px; 
 
    height: 40px; 
 
    padding-right: 40px; 
 
    border-bottom: 1px solid #888; 
 
} 
 
    
 
.nav a { 
 
    text-decoration: none; 
 
    color: #fff; 
 
    display: block; 
 
    transition: .3s background-color; 
 
} 
 
    
 
.nav a:hover { 
 
    background-color: #005f5f; 
 
} 
 
    
 
.nav a.active { 
 
    background-color: #fff; 
 
    color: #444; 
 
    cursor: default; 
 
} 
 
    
 
@media screen and (min-width: 600px) { 
 
    .nav li { 
 
    width: 150px; 
 
    border-bottom: none; 
 
    height: 50px; 
 
    line-height: 50px; 
 
    font-size: 1.4em; 
 
    } 
 
    
 
    /* Option 1 - Display Inline */ 
 
    .nav li { 
 
    display: inline-block; 
 
    margin-right: -4px; 
 
    } 
 
    
 
    /* Options 2 - Float 
 
    .nav li { 
 
    float: left; 
 
    } 
 
    .nav ul { 
 
    overflow: auto; 
 
    width: 600px; 
 
    margin: 0 auto; 
 
    } 
 
    .nav { 
 
    background-color: #444; 
 
    } 
 
    */ 
 
    
 
    /*****************************************************************/ 
 
    /*Dropdown for portfolio tab */ 
 
    /*****************************************************************/ 
 
    /* Dropdown Button */ 
 

 
.dropbtn { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
    padding: 16px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

 
.dropdown { 
 
    position: relative; 
 
    display: inline-block; 
 
} 
 

 
.dropdown-content { 
 
    display: none; 
 
    position: absolute; 
 
    background-color: #f9f9f9; 
 
    min-width: 160px; 
 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
 
} 
 

 
.dropdown-content a { 
 
    color: black; 
 
    padding: 12px 16px; 
 
    text-decoration: none; 
 
    display: block; 
 
} 
 

 
.dropdown-content a:hover {background-color: #f1f1f1} 
 

 
.dropdown:hover .dropdown-content { 
 
    display: block; 
 
} 
 

 
.dropdown:hover .dropbtn { 
 
    background-color: #3e8e41; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <meta charset="UTF-8"> 
 
\t <META name="viewport" content="width=device-width, initial-scale=1"> 
 
<title> Will's Portfolio </title> 
 
<link rel="stylesheet" href="dropdown.css"> 
 
<link href='http://fonts.gooleapis.com/css?family=Oswald' 
 
rel='stylesheet' type='text/css'> 
 

 

 
</head> 
 
<body background = "http://2.bp.blogspot.com/-Xmo26BMqg5Q/UihlqVwTwgI/AAAAAAAAAv0/V-Rrgm0V6oo/s1600/Top+10+best+Simple+Awesome+Background+Images+for+Your+Website+or+Blog3.jpg"> 
 
<body class="About Me"> 
 
\t <header> 
 
\t \t <div class="nav"> 
 
\t \t \t <ul> 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t \t <li><a href="#">About Me</a></li> \t \t \t \t 
 
\t \t \t \t \t \t 
 
\t \t \t \t <li> 
 
\t \t \t \t <div class="dropdown"> 
 
\t \t \t \t <button class="dropbtn">Portfolio</button> 
 
\t \t \t \t <div class="dropdown-content"> 
 
\t \t \t \t \t <a href="#">Graphics</a> 
 
\t \t \t \t \t <a href="#">Other</a> 
 
\t \t \t \t \t 
 
\t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t \t </li> 
 
\t \t \t \t <li><a href="#"><nobr>Future Work</nobr></a></li> 
 
\t \t \t </ul> 
 
\t \t </div> \t 
 
\t \t 
 

 
\t </header> 
 
</div> 
 
\t \t 
 
\t \t 
 

 

 
</body> 
 
</html>