CSS下拉菜单显示为水平而不是垂直

时间:2020-06-29 01:32:57

标签: html css

我正在尝试使用django开发一个网站,我想添加一个导航栏下拉菜单,但是由于某种原因,它一直显示为水平而不是垂直。

我正在关注W3 Schools在其网站https://www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_button上的教程

尽管我做了所有努力,但仍然没有任何效果,我尝试查看其他问题,但是大多数问题似乎是使用列表的其他方法,或者使用诸如react这样的框架。

我将项目移到了jsfiddle.net上,这似乎使问题更加严重,因为现在下拉列表中的第二个列表项根本没有显示。

这是我正在使用的代码http://jsfiddle.net/iggy12345/ao04gfne/9/

这是下面粘贴的代码:

我的html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<div class="header">
    <a class="active" href="{% url 'home' %}">Home</a>
    <div class="dropdown">
        <a href="/">Profile</a>
        <div class="dropdown-content">
            <a href="{% url 'logout' %}">Logout</a>
            <a href="/">Customize Profile</a>
        </div>
    </div>
</div>
</body>
</html>

我的css文件:

.dropdown {
    float: left;
    height: 55px;
    display: inline-block
}

/* Dropdown content (hidden by default) */
.dropdown-content {
  display: none;
  position: relative;
  top: 55px;
  background-color: #f9f9f9;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

/* Links inside the dropdown */
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
  background-color: #ddd;
}

/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
  display: block;
}

.header {
    background-color: #350752;
    overflow-x: hidden;
    overflow-y: visible;
}

.header > a, .dropdown > a {
    float: left;
    color: #dcb0f7;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
}

.header a:hover {
    background-color: #15bccf;
    color: white;
}

.header a:active {
    background-color: #c7860e
    color: white;
}

编辑

我将css重塑为看起来像Chaska的答案,但仍然无法正常工作,我不得不进行一些调整以使其保持原始外观,但是现在每当我将鼠标悬停在上方时,它都会添加一个滚动条个人资料

基本上,根据w3教程,下拉列表应显示在配置文件框下的下方,但是,每当我尝试执行此操作时,在配置文件按钮上,将其掩盖起来,然后最重要的是,它们继续水平而不是垂直

2 个答案:

答案 0 :(得分:1)

对CSS进行了一些修订。请阅读相关评论:

.dropdown {
  float: left;
  display: inline-block;
  height: 55px; /* overflow: hidden will hide the dropdown menu, use fixed height instead */
}

/* Dropdown content (hidden by default) */
.dropdown-content {
  display: none;
  position: absolute;
  top: 55px; /* must specify the top position */
  background-color: #f9f9f9;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

/* Links inside the dropdown */
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.header {
  background-color: #350752;
  overflow: visible; /* overflow: hidden will hide the dropdown menu */
}

.header > a, /* use > to select the direct child <a> instead of all of <a> child */
.dropdown > a {
  float: left;
  color: #dcb0f7;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.header a:hover {
  background-color: #15bccf;
  color: white;
}

.header a:active {
  background-color: #c7860e;
  color: white;
}

/* Add a grey background color to dropdown links on hover */
.dropdown-content a:hover {
  background-color: #ddd;
}

/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
  display: block;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<div class="header">
    <a class="active" href="{% url 'home' %}">Home</a>
    <div class="dropdown">
        <a href="/">Profile</a>
        <div class="dropdown-content">
            <a href="{% url 'logout' %}">Logout</a>
            <a href="/">Customize Profile</a>
        </div>
    </div>
</div>
</body>
</html>

答案 1 :(得分:-1)

.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);
  z-index: 1;
}

.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;
}

.header{
  background-color: #350752;
  
}
.header a:hover {
  background-color: #15bccf;
  color: white;
}
<!DOCTYPE html>
<html>
<head>

</head>
<body>
<div class="header"> 
 <a class="active" href="{% url 'home' %}">Home</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown</button>
      <div class="dropdown-content">
        <a href="#">Link 1</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
      </div>
  </div>
<div> 
</body>
</html>

在CSS中尝试使用此代码。