doubtstand doubtstand - 1 month ago 11
CSS Question

My pure CSS dropdown isn't working, anybody knows how to fix it?

I'm a CSS starter and my pure CSS dropdown isn't working.

Example in this jsfiddle: https://jsfiddle.net/uevewfsy/

I've been looking around on internet and trying to fix it myself but I have tried countless things and it still isn't fixed. Having a float: left; on the primary ul seems to fix it, but then my nav isn't centered anymore.

Hope someone can help me so I can go further with programming again ;)



* {
padding: 0;
margin: 0;
border: 0;
}

html, body {
height: 100%;
width: 100%;
font-family: 'Open Sans', sans-serif;
}

h1 {
font-weight: 100;
}

.nav {
width: 100%;
height: 10%;
background-color: #333;
text-align: center;
position: fixed;
z-index: 150;
}

.nav ul {
height: 100%;
width: 100%;
}

.nav > ul > li {
display: inline-block;
list-style: none;
margin: 0 20px 0 20px;
}

.nav > ul > li:first-child > a:after {
width: 6px;
height: 6px;
border-bottom: 1px solid white;
border-right: 1px solid white;
position: absolute;
margin-top: calc(5vh - 5px);
margin-left: 8px;
content: "";
transform: rotate(45deg);
}

.nav > ul > li > ul {
display: none;
}

.nav > ul > li > ul > li {
list-style: none;
background-color: #333;
padding: 0 15px 0 15px;
}

.nav li:hover > ul {
display: block;
}

.nav ul a {
color: #A3ABA3;
text-decoration: none;
line-height: 10vh;
}

.nav a:hover {
color: #FFF;
}

@media only screen and (max-device-width: 480px){

}

<body>
<div class="nav">
<ul>
<li>
<a href="#">PAGE</a>
<ul>
<li><a href="#">DROPDOWN</a></li>
<li><a href="#">DROPDOWN</a></li>
</ul>
</li>
<li><a href="#">PAGE</a></li>
<li><a href="#">PAGE</a></li>
<li><a href="#">PAGE</a></li>
</ul>
</div>
</body>




Answer

you are just missing vertical-align:top in your nav > ul > li,

you can add position:relative/top to your li/ul as well.

* {
  padding: 0;
  margin: 0;
  border: 0;
}
html,
body {
  height: 100%;
  width: 100%;
  font-family: 'Open Sans', sans-serif;
}
h1 {
  font-weight: 100;
}
.nav {
  width: 100%;
  height: 10%;
  background-color: #333;
  text-align: center;
  position: fixed;
  z-index: 150;
}
.nav ul {
  height: 100%;
  width: 100%;
}
.nav > ul > li {
  display: inline-block;
  list-style: none;
  padding: 0 20px;
  vertical-align: top
}
.nav > ul > li:first-child > a:after {
  width: 6px;
  height: 6px;
  border-bottom: 1px solid white;
  border-right: 1px solid white;
  position: absolute;
  margin-top: calc(5vh - 5px);
  margin-left: 8px;
  content: "";
  transform: rotate(45deg);
}
.nav > ul > li > ul {
  display: none;
}
.nav > ul > li > ul > li {
  list-style: none;
  background-color: #333;
  padding: 0 15px;
  position:relative
}
.nav li:hover > ul {
  display: block;
  position:absolute;
  top:100%;
  width:30%
    
  
}
.nav ul a {
  color: #A3ABA3;
  text-decoration: none;
  line-height: 10vh;
}
.nav a:hover {
  color: #FFF;
}
@media only screen and (max-device-width: 480px) {}
<body>
  <div class="nav">
    <ul>
      <li>
        <a href="#">PAGE</a>
        <ul>
          <li><a href="#">DROPDOWN</a>
          </li>
          <li><a href="#">DROPDOWN</a>
          </li>
        </ul>
      </li>
      <li><a href="#">PAGE</a>
      </li>
      <li><a href="#">PAGE</a>
      </li>
      <li><a href="#">PAGE</a>
      </li>
    </ul>
  </div>
</body>