0

I want to do some dropdown menu on this navigation menu, but it's not working and aswell I would like to center it. I tried to use display:inline; command, but it didnt help.

https://jsfiddle.net/fLdasLv4/

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333;
 position: absolute;
 left: 0%;
 top: 0%;
 width: 100%;
 height: 8%;
 
 
}

li {
    float: left;
}

li a {
    display: block;
    color: white;
    text-align: center;
    padding: 20px 25px;
 text-decoration: none;
 font-size: 100%;
 font-family:Lucida Sans Unicode;

 
}

li a:hover {
    background-color: #111;
}
li a:active{
    background-color: grey;
}

ul li:hover > ul
{
 display:block
}
<ul>
  <li><a class="active" href="#home" font size="16">Home</a></li>
  <li><a class="kaires" href="#news">Dropd</a></li>
      <ul>
      <li><a href="#">1</a></li>
      <li><a href="#">2</a></li>
      <li><a href="#">3</a></li>
      <li><a href="#">4</a></li>
   </ul>
  
  
  
  <li><a  href="#about">Something</a></li>
  <li><a  href="#about">Contact us</a></li>
</ul>
XYZ
  • 4,450
  • 2
  • 15
  • 31
Jozikas
  • 9
  • 3

2 Answers2

0

Could do something like this?

use flexbox to center it, then hide sub menu first use

ul li ul {
  display: none;
}

On hover the menu display the submenu use:

/* Sub menu item */
ul li ul li {
  width: 100%;
  display: block;
}

/* Show Sub menu on hover */
ul li:hover > ul {
  position: absolute;
  display: block;
  background: green;
  width: 30%; /* Sub menu width */
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #333;
  width: 100%;
}

li {
  float: left;
}

li a {
  display: block;
  color: white;
  text-align: center;
  padding: 20px 25px;
  text-decoration: none;
  font-size: 100%;
  font-family: Lucida Sans Unicode;
}

li a:hover {
  background-color: #111;
}

li a:active {
  background-color: grey;
}

ul li ul {
  display: none;
}

/* Sub menu item */
ul li ul li {
  width: 100%;
  display: block;
}

/* Show Sub menu on hover */
ul li:hover > ul {
  position: absolute;
  display: block;
  background: green;
  width: 30%; /* Sub menu width */
}

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}
<ul class="container">
  <li><a class="active" href="#home" font size="16">Home</a></li>
  <li><a class="kaires" href="#news">Dropd</a>
    <ul>
      <li><a href="#">1</a></li>
      <li><a href="#">2</a></li>
      <li><a href="#">3</a></li>
      <li><a href="#">4</a></li>
    </ul>
  </li>
  <li><a href="#about">Something</a></li>
  <li><a href="#about">Contact us</a></li>
</ul>
Dalin Huang
  • 11,212
  • 5
  • 32
  • 49
0

Firstly your html markup for the sub-menu was not correctly done. Also you can't put an overflow on the main container if you want the sub-menus to show. You can use flebox to center the nav without changing markup or writing more codes. See sample below..

nav {}
ul {
    display: flex;
    justify-content: center;
    list-style-type: none;
    margin: 0;
    padding: 0;
    /** overflow: hidden; Removed this(this wouldn't allow the submenu show) **/
    background-color: #333;
   position: absolute;
   /**left: 0%; **/
   top: 0%;
   width: 100%;
   /** height: 8%;Removed This**/
}
ul ul {
    top: 100%;
    display: block;
    height: auto;
    visibility: hidden;
}
li {
   /** float: left; **/
    position: relative;
}
li a {
    display: block;
    color: white;
    text-align: center;
    padding: 20px 25px;
   text-decoration: none;
   font-size: 100%;
   font-family:Lucida Sans Unicode;
}
li a:hover {
    background-color: #111;
}
li a:active{
    background-color: grey;
}
ul li:hover >  ul {
 display:block;
  visibility: visible;
}
li > ul li {
  float: none;
}
<ul>
  <li><a class="active" href="#home" font size="16">Home</a></li>
  <li><a class="kaires" href="#news">Dropd</a>
      <ul>
        <li><a href="#">1</a></li>
        <li><a href="#">2</a></li>
        <li><a href="#">3</a></li>
        <li><a href="#">4</a></li>
     </ul>
    </li>
  <li><a  href="#about">Something</a></li>
  <li><a  href="#about">Contact us</a></li>
</ul>
blecaf
  • 1,625
  • 1
  • 8
  • 13