kkt
kkt

Reputation: 53

Changing li class reorders list

I have a banner-style list, with one of the items being a dropdown. For some reason, the list is not displayed in the correct order. There should be four items, Home, Products and Materials, Testimonials, and Contact, the second of which should be a dropdown. Instead what is happening is the dropdown item is being displayed last.

HTML:

<ul class="banner">
  <li class="banner"><a href="#home"><H2>Home</H2></a></li>
  <li class="dropdown">    
      <a href="#javascript:void(0)" class="dropbtn"><h2>Products and Materials</h2> </a>
      <div class="dropdown-content">
       <a href="#student"><h4>Materials for School settings</h4></a>
       <a href="#adult"><h4>Materials for adult clinical settings</h4></a>
       <a href="#teletherapy"><h4>Materials for Teletherapy</h4></a>
      </div>
  </li>
  <li class="banner"><a href="#home"><H2>Testimonials</H2></a></li>
  <li class="banner"><a href="#home"><H2>Contact</H2></a></li>
</ul>

CSS:

/*bannner buttons*/
ul.banner {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333;
}

li.banner {
    float: left;
}

li a, .dropbtn {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {
    background-color: #00e5ee;
}

li.dropdown {
    display: inline-block;
}


/*dropdown button*/

.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: 5px 15px;
    text-decoration: none;
    display: block;
    text-align: left;
}

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

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

Upvotes: 4

Views: 31

Answers (2)

kodabear
kodabear

Reputation: 340

you missed adding a float left

li.dropdown {
    display: inline-block;
    float: left;
}

Upvotes: 3

Imphusius
Imphusius

Reputation: 678

In CSS instead of writing li.banner change it to .banner li like this:

ul.banner {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333;
}

.banner li {
    float: left;
}

li a, .dropbtn {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {
    background-color: #00e5ee;
}

li.dropdown {
    display: inline-block;
}


/*dropdown button*/

.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: 5px 15px;
    text-decoration: none;
    display: block;
    text-align: left;
}

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

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

And remove banner class from all list items

<ul class="banner">
  <li><a href="#home"><H2>Home</H2></a></li>
  <li class="dropdown">    
      <a href="#javascript:void(0)" class="dropbtn"><h2>Products and Materials</h2> </a>
      <div class="dropdown-content">
       <a href="#student"><h4>Materials for School settings</h4></a>
       <a href="#adult"><h4>Materials for adult clinical settings</h4></a>
       <a href="#teletherapy"><h4>Materials for Teletherapy</h4></a>
      </div>
  </li>
  <li><a href="#home"><H2>Testimonials</H2></a></li>
  <li><a href="#home"><H2>Contact</H2></a></li>
</ul>

Upvotes: 1

Related Questions