Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS HTML Drop down navigation elements appear inline instead of below

Tags:

html

css

Below is a JSFiddle link of some of my code. I was trying to achieve a slick looking pure html/css drop-down menu without the use of bootstrap or other plug-in sorts. However I can't seem to get the 'Creative' drop-down elements to appear below the navigation bar, they instead appear in-line and I have tried to change other parts of the code to make it work but I can't seem to do it without compromising the rest of the nav-bar.

Please if someone could give it a look at get it so when you hover 'Creative' it's children list elements appear below it. Preferably without just styling padding and margins.

https://jsfiddle.net/nytnfvmq/

HTML:

<!DOCTYPE html>
<html lang="en">

  <head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Udemy Project</title>

    <link type="text/css" href="styles.css" rel="stylesheet">

    <script type="text/javascript" src="javascript.js"></script>
    <script type="text/javascript" src="jquery.min.js"></script>
    <!--<script type="text/javascript" src="jquery-ui.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>-->


  </head>

  <body>

    <nav>
      <ul>

        <li>
          <a href="">Home</a>
        </li>

        <li>
          <a href="">Development</a>
        </li>

        <li>
          <a href="">Creative</a>

          <ul>

            <li>
              <a href="">Film</a>
            </li>

            <li>
              <a href="">Design</a>
            </li>

            <li>
              <a href="">Music</a>
            </li>

          </ul>

        </li>

        <li>
          <a href="">Information</a>
        </li>

        <li>
          <a href="">Contact Me</a>
        </li>
      </ul>
    </nav>

    <div id="banner">
      <img src="images/banner.png" alt="Banner image did not load." ;>
    </div>

  </body>

</html>

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

body {
  margin: 0;
  padding: 0;
  font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue",     Helvetica, Arial, "Lucida Grande", sans-serif;
  font-weight: 300;
}

nav {
  width: 100%;
  height: 40px;
  margin: 0 auto;
  background-color: #ffffff;
  box-shadow: 0px 2px 5px #6E6E6E;
  position: fixed;
}

nav ul {
  width: 1200px;
  margin: 0 auto;
  position: relative;
  list-style: none;
  color: #00b6ed;
}

nav ul li a {
  width: 20%;
  display: inline;
  text-align: center;
  float: left;
  padding-top: 11px;
  padding-bottom: 11px;
  color: #00b6ed;
  text-decoration: none;
}

nav ul li a:hover {
  background-color: #00b6ed;
  color: #ffffff;
}

nav ul li ul {
  display: none;
  position: relative;
}

nav ul li:hover ul {
  display: block;
  position: relative;
}

#banner {
  width: 100%;
  height: 400px;
  background-color: #00b6ed;
  float: left;
  text-align: center;
}

#banner img {
  margin: 0 auto;
  background-color: #00b6ed;
}

nav ul li ul li a {
  background-color: red;
  color: green;
}
like image 728
Alexander Wines Avatar asked Oct 31 '22 11:10

Alexander Wines


1 Answers

You have a few things wrong. Don't float and assign widths to the anchor tags. Instead float the list items. You'll also need to add position: relative to the li and then add position: absolute; left: 0; top: 100%; to the child ul. That should about do it I think.

* {
  margin: 0;
  padding: 0;
}

body {
  margin: 0;
  padding: 0;
  font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
  font-weight: 300;
}

nav {
  width: 100%;
  height: 40px;
  margin: 0 auto;
  background-color: #ffffff;
  box-shadow: 0px 2px 5px #6E6E6E;
  position: fixed;
}

nav ul {
  width: 1200px;
  margin: 0 auto;
  position: relative;
  list-style: none;
  color: #00b6ed;
}

nav ul li {
  width: 20%;
  float: left;
  position: relative;
}

nav ul li a {
  display: block;
  text-align: center;
  padding-top: 11px;
  padding-bottom: 11px;
  color: #00b6ed;
  text-decoration: none;
}

nav ul li a:hover {
  background-color: #00b6ed;
  color: #ffffff;
}

nav ul li ul {
  display: none;
  position: absolute;
  left: 0;
  top: 100%;
}

nav ul li:hover ul {
  display: block;
  position: relative;
}

#banner {
  width: 100%;
  height: 400px;
  background-color: #00b6ed;
  float: left;
  text-align: center;
}

#banner img {
  margin: 0 auto;
  background-color: #00b6ed;
}

nav ul li ul li {
  float: none;
}

nav ul li ul li a {
  background-color: red;
  color: green;
}
<!DOCTYPE html>
<html lang="en">

  <head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Udemy Project</title>

    <link type="text/css" href="styles.css" rel="stylesheet">

    <script type="text/javascript" src="javascript.js"></script>
    <script type="text/javascript" src="jquery.min.js"></script>
    <!--<script type="text/javascript" src="jquery-ui.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>-->


  </head>

  <body>

    <nav>
      <ul>

        <li>
          <a href="">Home</a>
        </li>

        <li>
          <a href="">Development</a>
        </li>

        <li>
          <a href="">Creative</a>

          <ul>

            <li>
              <a href="">Film</a>
            </li>

            <li>
              <a href="">Design</a>
            </li>

            <li>
              <a href="">Music</a>
            </li>

          </ul>

        </li>

        <li>
          <a href="">Information</a>
        </li>

        <li>
          <a href="">Contact Me</a>
        </li>
      </ul>
    </nav>

    <div id="banner">
      <img src="images/banner.png" alt="Banner image did not load." ;>
    </div>

  </body>

</html>
like image 198
Joseph Marikle Avatar answered Nov 15 '22 06:11

Joseph Marikle