How do I make my Fade-In dropdown menu using CSS

#menu {
  width: 100%;
  margin: 10px auto;
}
#menu ul,
#menu li {
  margin: 0 auto;
  padding: 0 0;
  list-style: none
}
#menu ul {
  width: 100%;
}
#menu li {
  float: left;
  display: inline;
  position: relative;
}
#menu a {
  display: block;
  line-height: 35px;
  padding: 0 14px;
  text-decoration: none;
}
#menu li a {
  border-right: 1px solid #e5e5e5;
}
#menu li a:hover {
  color: #444;
  background-color: #e5e5e5;
}
#menu input {
  display: none;
  margin: 0 0;
  padding: 0 0;
  width: 80px;
  height: 30px;
  opacity: 0;
}
#menu label {
  font-size: 20px;
  display: none;
  width: 35px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  color: #77778b
}
#menu label span {
  position: absolute;
  left: 35px;
}
#menu ul.menus {
  height: auto;
  overflow: hidden;
  width: 180px;
  border-top: 1px solid #e5e5e5;
  border-left: 1px solid #e5e5e5;
  border-bottom: 1px solid #e5e5e5;
  position: absolute;
  z-index: 99;
  display: none;
  left: -1px;
}
#menu ul.menus li {
  display: block;
  width: 100%;
}
#menu ul.menus a {
  color: #77778b;
}
#menu li:hover ul.menus {
  display: block
}
      

<nav id='menu'>
  <input type='checkbox' />
  <label><i class='icon-reorder' /><span>Menu</span>
  </label>
  <ul>
    <li><a href='/'>Home</a>
    </li>
    <li><a class='dropdown' href='#'>Menu1</a>
      <ul class='menus'>
        <li><a href='#'>Submenu1</a>
        </li>
        <li><a href='#'>Submenu2</a>
        </li>
      </ul>
    </li>
    <li><a class='dropdown' href='#'>Menu2</a>
      <ul class='menus'>
        <li><a href='#'>Submenu1</a>
        </li>
        <li><a href='#'>Submenu2</a>
        </li>
        <li><a href='#'>Submenu3</a>
        </li>
      </ul>
    </li>

  </ul>
</nav>
      

Run codeHide result


How can I do Dropdowns Fade with CSS only? A bit of a Nubian question, but still trying to understand CSS. Anyone has an idea of ​​how to make it possible plz to offer. Thank!

+3


source to share


1 answer


One possible solution is to use opacity: 0

instead display: none

and use a hover transition :

#menu {
  width: 100%;
  margin: 10px auto;
}
#menu ul,
#menu li {
  margin: 0 auto;
  padding: 0 0;
  list-style: none
}
#menu ul {
  width: 100%;
}
#menu li {
  float: left;
  display: inline;
  position: relative;
}
#menu a {
  display: block;
  line-height: 35px;
  padding: 0 14px;
  text-decoration: none;
}
#menu li a {
  border-right: 1px solid #e5e5e5;
}
#menu li a:hover {
  color: #444;
  background-color: #e5e5e5;
}
#menu input {
  display: none;
  margin: 0 0;
  padding: 0 0;
  width: 80px;
  height: 30px;
  opacity: 0;
}
#menu label {
  font-size: 20px;
  display: block;
  width: 35px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  color: #77778b;
  visibility: hidden;
}
#menu label span {
  position: absolute;
  left: 35px;
}
#menu ul.menus {
  height: auto;
  overflow: hidden;
  width: 180px;
  border-top: 1px solid #e5e5e5;
  border-left: 1px solid #e5e5e5;
  border-bottom: 1px solid #e5e5e5;
  position: absolute;
  z-index: 99;
  display: block;
  left: -1px;
  opacity: 0;
}
#menu ul.menus li {
  display: block;
  width: 100%;
}
#menu ul.menus a {
  color: #77778b;
}
#menu li:hover ul.menus {
  opacity: 1;
  -webkit-transition: opacity 1s ease-in-out;
  -moz-transition: opacity 1s ease-in-out;
  -ms-transition: opacity 1s ease-in-out;
  -o-transition: opacity 1s ease-in-out;
  transition: opacity 1s ease-in-out;
}
      

<nav id='menu'>
  <input type='checkbox' />
  <label><i class='icon-reorder' /><span>Menu</span>
  </label>
  <ul>
    <li><a href='/'>Home</a>
    </li>
    <li><a class='dropdown' href='#'>Menu1</a>
      <ul class='menus'>
        <li><a href='#'>Submenu1</a>
        </li>
        <li><a href='#'>Submenu2</a>
        </li>
      </ul>
    </li>
    <li><a class='dropdown' href='#'>Menu2</a>
      <ul class='menus'>
        <li><a href='#'>Submenu1</a>
        </li>
        <li><a href='#'>Submenu2</a>
        </li>
        <li><a href='#'>Submenu3</a>
        </li>
      </ul>
    </li>

  </ul>
</nav>
      

Run codeHide result




Also you can use the transition

same as for animation on mouse output:

#menu {
  width: 100%;
  margin: 10px auto;
}
#menu ul,
#menu li {
  margin: 0 auto;
  padding: 0 0;
  list-style: none
}
#menu ul {
  width: 100%;
}
#menu li {
  float: left;
  display: inline;
  position: relative;
}
#menu a {
  display: block;
  line-height: 35px;
  padding: 0 14px;
  text-decoration: none;
}
#menu li a {
  border-right: 1px solid #e5e5e5;
}
#menu li a:hover {
  color: #444;
  background-color: #e5e5e5;
}
#menu input {
  display: none;
  margin: 0 0;
  padding: 0 0;
  width: 80px;
  height: 30px;
  opacity: 0;
}
#menu label {
  font-size: 20px;
  display: block;
  width: 35px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  color: #77778b;
  visibility: hidden;
}
#menu label span {
  position: absolute;
  left: 35px;
}
#menu ul.menus {
  height: auto;
  overflow: hidden;
  width: 180px;
  border-top: 1px solid #e5e5e5;
  border-left: 1px solid #e5e5e5;
  border-bottom: 1px solid #e5e5e5;
  position: absolute;
  z-index: 99;
  display: block;
  left: -1px;
  opacity: 0;
  -webkit-transition: opacity 0.5s ease-in-out;
  -moz-transition: opacity 0.5s ease-in-out;
  -ms-transition: opacity 0.5s ease-in-out;
  -o-transition: opacity 0.5s ease-in-out;
  transition: opacity 0.5s ease-in-out;
}
#menu ul.menus li {
  display: block;
  width: 100%;
}
#menu ul.menus a {
  color: #77778b;
}
#menu li:hover ul.menus {
  opacity: 1;
  -webkit-transition: opacity 1s ease-in-out;
  -moz-transition: opacity 1s ease-in-out;
  -ms-transition: opacity 1s ease-in-out;
  -o-transition: opacity 1s ease-in-out;
  transition: opacity 1s ease-in-out;
}
      

<nav id='menu'>
  <input type='checkbox' />
  <label><i class='icon-reorder' /><span>Menu</span>
  </label>
  <ul>
    <li><a href='/'>Home</a>
    </li>
    <li><a class='dropdown' href='#'>Menu1</a>
      <ul class='menus'>
        <li><a href='#'>Submenu1</a>
        </li>
        <li><a href='#'>Submenu2</a>
        </li>
      </ul>
    </li>
    <li><a class='dropdown' href='#'>Menu2</a>
      <ul class='menus'>
        <li><a href='#'>Submenu1</a>
        </li>
        <li><a href='#'>Submenu2</a>
        </li>
        <li><a href='#'>Submenu3</a>
        </li>
      </ul>
    </li>

  </ul>
</nav>
      

Run codeHide result


+3


source







All Articles