Carregando...

Menu dropdown responsive com HTML e CSS

Postado: 14 de abril de 2023 Tempo de Leitura: 3 Minutos

Menu dropdown responsive com HTML e CSS

Veja neste tutorial como criar um Menu dropdown responsive com html e css e jquery para dar o efeito mobile.

Nesta seção, projetaremos uma estrutura simples na tag <div><nav><ul> ,<li> , <span> e a <a> para inserirmos o link de destino. 

Essas são as tags que usaremos para criar o nosso Menu dropdown.

Vamos Adicionar o HTML

        <div class="menu">

            <nav class="main-nav">
            
              <ul>
                
                <li><a href="#" title="Home">Home</a></li>
                
                <li><a href="">Front-end</a>
                 
                    <ul>
                        
                        <li><a href="#" title="Html">Html</a></li>
                        <li><a href="#" title="Css">Css</a></li>
                        <li><a href="#" title="Jquery">Jquery</a></li>
                        <li><a href="#" title="Javascript">Javascript</a></li>

                    </ul>

                </li>
                
                <li><a href="">Back-end</a>
                
                    <ul>

                        <li><a href="#" title="PHP">PHP</a></li>
                        <li><a href="#" title="Python">Python</a>
                        <li><a href="#" title="Ruby">Ruby</a>
                        <li><a href="#" title="C++">C++</a></li>
                        <li><a href="#" title="Banco de Dados">Banco de Dados</a>
                        
                          <ul>
                            <li><a href="#" title="MySQL">MySQL</a></li>
                            <li><a href="#" title="MariaDB">MariaDB </a></li>
                          </ul>
                        
                        </li>
                        
                      
                    </ul>

                </li>

                <li><a href="#" title="Blog">Blog</a></li>
                <li><a href="#" title="Fale Conosco">Fale Conosco</a></li>
              
              </ul>
            
            </nav>

            <div class="mob-btn">
              <span></span>
              <span></span>
              <span></span>
            </div>

            <div class="overlay"></div>
          
        </div><!-- menu -->

Vamos Adicionar o CSS

Nesta seção, usaremos algumas propriedades CSS para estilizar o nosso Menu dropdown.

.menu {
 
    background: #0553a5;
    margin-bottom: 0;
    width: 100%;
    float: left;
    margin-top: 0;
    border-top: 1px #2369b2 solid;

}

@media only screen and (min-width: 1200px) {
 
  .desk-hide {
    display: none;
  }

  .main-nav {
    font-size: 17px;
  }

  .main-nav > ul {
    margin: 0;
    padding: 0;
  }

  .main-nav > ul > li {
    display: inline-block;
    position: relative;
  }

  .main-nav > ul > li:last-child {
    margin: 0;
  }

  .main-nav > ul > li.submenu:after {

    content: "";
    position: absolute;
    height: 0;
    width: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-top: 6px solid #fff;
    right: 7px;
    top: 24px;
    pointer-events: none;

  }

  .main-nav > ul > li > a {
    
    color: #fff;
    display: block;
    padding: 15px 20px;
    text-decoration: none;
    font-size: 15px;
  
  }

  .main-nav > ul > li:hover > a {
    
    color: #fff;
    background: #023f80;
  
  }

  .main-nav > ul > li:hover > ul {
    
    visibility: visible;
    opacity: 1;
    z-index: 1;
  
  }

  .main-nav > ul > li ul {

    position: absolute;
    margin: 0;
    padding: 0;
    background: #023f80;
    width: 250px;
    visibility: hidden;
    opacity: 0;
    font-size: 14px;
    display: block !important;

  }

  .main-nav > ul > li ul li {
    display: block;
    border-bottom: 1px solid #0553a5;
    position: relative;
  }

  .main-nav > ul > li ul li:last-child {
    border: 0;
  }

  .main-nav > ul > li ul li a {

    padding: 15px 20px;
    display: block;
    text-decoration: none;
    font-size: 15px;
    color: #fff;

  }

  .main-nav > ul > li ul li:hover > ul {
    
    visibility: visible;
    opacity: 1;
    left: 250px;
    top: 0;
  
  }

  .main-nav > ul > li ul li:hover > a {
  
    background: #00b2f8;
  
  }

  .main-nav > ul > li ul li.submenu::after {
    
    content: "";
    position: absolute;
    height: 0;
    width: 0;
    border-bottom: 5px solid transparent;
    border-left: 6px solid #fff;
    border-top: 5px solid transparent;
    right: 11px;
    top: 20px;
    pointer-events: none;
  
  }

  .mob-btn {
    display: none;
  }

  
}

@media only screen and (max-width: 1199px) {
  
  .main-nav {
    
    position: fixed;
    width: 250px;
    top: 0;
    right: -250px;
    background: #0553a5;
    height: 100%;
    transition: all ease 0.2s;
    z-index: 1005;
    overflow-y: auto;
  
  }
  
  .main-nav ul {
    margin: 0;
    padding: 0;
    display: block;
    margin-top: 0px;
  }
  
  .main-nav ul.mnav {
    margin-bottom: 30px;
  }
  
  .main-nav ul li {
    display: block;
    border-bottom: 1px solid #246ab3;
    position: relative;
    text-transform: capitalize;
    font-size: 15px;
  }
  
  .main-nav ul li span {
    display: none;
  }
  
  .main-nav ul li a {
    color: #fff;
    text-decoration: none;
    font-size: 1em;
    display: block;
    padding: 10px;
    padding-right: 40px;
  }
  
  .main-nav ul li a:hover {
    
    background-color: #043b75;
    color: #ffffff;

  }
  
  .main-nav ul li.submenu i {
    position: absolute;
    width: 43px;
    height: 39px;
    top: 0;
    right: 0;
    cursor: pointer;
    -webkit-transition: all ease 0.5s;
    -moz-transition: all ease 0.5s;
    transition: all ease 0.5s;
  }
  
  .main-nav ul li.submenu i:after {

    content: "";
    height: 0;
    width: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-top: 6px solid #fff;
    position: absolute;
    right: 17px;
    top: 20px;
    z-index: 10;

  }
  
  .main-nav ul ul {
    margin: 0;
    display: none;
  }
  
  .main-nav ul ul li:last-child {
    border-bottom: 0;
  }
  
  .main-nav ul ul li:first-child {
    border-top: 1px solid #246ab3;
  }
  
  .main-nav ul ul li a {
    padding-left: 20px;
  }
  
  .main-nav ul ul li ul a {
  
    padding-left: 30px;
  
  }

  .show-menu .main-nav {
  
    right: 0;
  
  }
  
  .show-menu .overlay {
  
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
    z-index: 1001;
  
  }

  .mob-btn {
  
    position: absolute;
    float: right;
    padding: 5px 7px;
    margin: 0px;
    background-color: transparent;
    cursor: pointer;
    right: 15px;
    top: 23px;
  
  }
  
  .mob-btn span {
  
    display: block;
    width: 25px;
    height: 2px;
    border-radius: 1px;
    background: #fff;
  
  }

  .mob-btn span + span {

    margin-top: 5px;
  
  }

}

Vamos Adicionar o script para dar o efeito mobile

<script src="js/jquery.js"></script>
<script>
    
    //Menu
    $(".main-nav ul li:has(ul)").addClass("submenu");
    $(".main-nav ul li:has(ul)").append("<i></i>");
    $(".main-nav ul i").click(function() {
      $(this)
        .parent("li")
        .toggleClass("open");
      $(this)
        .parent("li")
        .children("ul")
        .slideToggle();
    });

    //Menu Mobile
    $(".mob-btn").click(function() {
      if (!$("html").hasClass("show-menu")) {
        $("html").addClass("show-menu");
      } else {
        $("html").removeClass("show-menu");
      }
    });

    $(".overlay").click(function() {
      if ($("html").hasClass("show-menu")) {
        $("html").removeClass("show-menu");
      }
    });

    //Aparecer addClass
    $(".first ul")
      .clone()
      .prependTo(".main-nav")
      .addClass("desk-hide");
    $(".last ul")
      .clone()
      .appendTo(".main-nav")
      .addClass("desk-hide");

</script>

Combinando as Três seções acima com Html Css e jquery temos o seguinte Resultado!

Veja o Resultado baixo!

Baixar Código Veja Funcionando

Publicado por: Loop Nerd

2.062 Visualizações

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Artigos Relacionados