{"id":4088,"date":"2015-04-20T10:52:22","date_gmt":"2015-04-20T13:52:22","guid":{"rendered":"https:\/\/www.turbosite.com.br\/blog\/?p=4088"},"modified":"2015-04-20T10:52:22","modified_gmt":"2015-04-20T13:52:22","slug":"tutorial-como-criar-um-menu-dropdown-com-apenas-css","status":"publish","type":"post","link":"https:\/\/www.turbosite.com.br\/blog\/tutorial-como-criar-um-menu-dropdown-com-apenas-css\/","title":{"rendered":"Como criar um Menu Dropdown com apenas CSS"},"content":{"rendered":"<p>Este tutorial ensina a como criar um menu Dropdown com apenas recursos do CSS 3. Veja abaixo o resultado final:<\/p>\n<p><iframe loading=\"lazy\" src=\"\/\/jsfiddle.net\/7ojkaybf\/11\/embedded\/result,html,css\/\" width=\"100%\" height=\"400\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>O menu que vamos criar \u00e9 composto de 3 n\u00edveis, t\u00edtulo principal,\u00a0sub categoria e sub-sub categoria. Uma vez que passar o mouse por cima do t\u00edtulo, automaticamente \u00e9 disparado o evento :hover que mostrar\u00e1 as sub categorias.\u00a0A primeira s\u00e9rie de sub-links aparecem embaixo da barra principal de navega\u00e7\u00e3o, ent\u00e3o a segunda s\u00e9rie de links ir\u00e1 aparecer horizontalmente a partir do primeiro sub item. D\u00ea uma olhada na imagem.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4092\" src=\"https:\/\/www.turbosite.com.br\/blog\/wp-content\/uploads\/2015\/04\/menu.jpg\" alt=\"menu\" width=\"277\" height=\"231\" \/><\/p>\n<h3 class=\"header\">A Estrutura HTML<\/h3>\n<p>Primeiro vamos precisar criar nossa\u00a0estrutura HTML. Vamos utilizar a tag\u00a0<code>&lt;nav&gt;<\/code>\u00a0do HTML 5, em seguida, adicionar os links de navega\u00e7\u00e3o prim\u00e1rios\u00a0em uma lista desordenada.<\/p>\n<pre class=\"top-margin:20 bottom-margin:20 left-set:true left-margin:20 right-set:true right-margin:20 whitespace-before:2 whitespace-after:2 lang:xhtml decode:true\" title=\"Estrutura HTML\">&lt;nav&gt;\n\t&lt;ul&gt;\n\t\t&lt;li&gt;&lt;a href=\"#\"&gt;Home&lt;\/a&gt;&lt;\/li&gt;\n\t\t&lt;li&gt;&lt;a href=\"#\"&gt;Tutoriais&lt;\/a&gt;\n\t\t\t&lt;ul&gt;\n\t\t\t\t&lt;li&gt;&lt;a href=\"#\"&gt;CSS&lt;\/a&gt;&lt;\/li&gt;\n\t\t\t\t&lt;li&gt;&lt;a href=\"#\"&gt;HTML&lt;\/a&gt;&lt;\/li&gt;\n\t\t\t\t&lt;li&gt;&lt;a href=\"#\"&gt;JavaScript&lt;\/a&gt;\n\t\t\t\t\t&lt;ul&gt;\n\t\t\t\t\t\t&lt;li&gt;&lt;a href=\"#\"&gt;jQuery&lt;\/a&gt;&lt;\/li&gt;\n\t\t\t\t\t\t&lt;li&gt;&lt;a href=\"#\"&gt;Angular&lt;\/a&gt;&lt;\/li&gt;\n\t\t\t\t\t&lt;\/ul&gt;\n\t\t\t\t&lt;\/li&gt;\n\t\t\t&lt;\/ul&gt;\n\t\t&lt;\/li&gt;\n\t\t&lt;li&gt;&lt;a href=\"#\"&gt;Artigos&lt;\/a&gt;\n\t\t\t&lt;ul&gt;\n\t\t\t\t&lt;li&gt;&lt;a href=\"#\"&gt;Node.js&lt;\/a&gt;&lt;\/li&gt;\n\t\t\t\t&lt;li&gt;&lt;a href=\"#\"&gt;MongoDB&lt;\/a&gt;&lt;\/li&gt;\n\t\t\t&lt;\/ul&gt;\n\t\t&lt;\/li&gt;\n\t\t&lt;li&gt;&lt;a href=\"#\"&gt;Crie seu Pr\u00f3prio Menu&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;\/ul&gt;\n&lt;\/nav&gt;<\/pre>\n<p>O resultado sem a inser\u00e7\u00e3o do CSS ficaria da seguinte forma:<\/p>\n<p><iframe loading=\"lazy\" src=\"\/\/jsfiddle.net\/7ojkaybf\/1\/embedded\/result,html,css\/\" width=\"100%\" height=\"300\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<h3 class=\"header\">Estilizando com\u00a0CSS<\/h3>\n<p>Vamos come\u00e7ar com o b\u00e1sico, primeiro vamos apenas esconder nossos sub-links e faz\u00ea-los aparecer caso passe o mouse por cima do menu pai. Para isto, vamos utilizar o seletor <code>:hover<\/code>\u00a0que, quando passado o mouse sobre o menu, os seu elementos filhos ser\u00e3o mostrados. Com este pequeno c\u00f3digo j\u00e1 temos um resultado interessante. Passe o mouse sobre os links para ver o resultado em a\u00e7\u00e3o. Tamb\u00e9m centralizamos o menu.<\/p>\n<pre class=\"top-margin:20 bottom-margin:20 left-set:true left-margin:20 right-set:true right-margin:20 whitespace-before:2 whitespace-after:2 lang:css decode:true\" title=\"B\u00e1sico\">nav ul ul {\n\tdisplay: none;\n}\n\n\tnav ul li:hover&gt; ul {\n\t\tdisplay: block;\n\t}\n\nnav {\n\tmargin: 0 auto; \n\ttext-align: center;\n}<\/pre>\n<p><iframe loading=\"lazy\" src=\"\/\/jsfiddle.net\/7ojkaybf\/embedded\/result,html,css\/\" width=\"100%\" height=\"300\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>Agora vamos dar uma melhorada no visual adicionando\u00a0cor e espa\u00e7amentos.<\/p>\n<pre class=\"top-margin:20 bottom-margin:20 left-set:true left-margin:20 right-set:true right-margin:20 whitespace-before:2 whitespace-after:2 lang:css decode:true\" title=\"Um pouco de cor...\">nav ul {\n\tbackground: #efefef; \n\tbackground: linear-gradient(top, #efefef 0%, #bbbbbb 100%);  \n\tbackground: -moz-linear-gradient(top, #efefef 0%, #bbbbbb 100%); \n\tbackground: -webkit-linear-gradient(top, #efefef 0%,#bbbbbb 100%); \n\tbox-shadow: 0px 0px 9px rgba(0,0,0,0.15);\n\tpadding: 0 20px;\n\tborder-radius: 10px;  \n\tlist-style: none;\n\tposition: relative;\n\tdisplay: inline-table;\n}\n\tnav ul:after {\n\t\tcontent: \"\"; clear: both; display: block;\n\t}<\/pre>\n<p>Veja como est\u00e1 ficando at\u00e9 agora.<\/p>\n<p><iframe loading=\"lazy\" src=\"\/\/jsfiddle.net\/7ojkaybf\/2\/embedded\/result,html,css\/\" width=\"100%\" height=\"300\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>Continuando, agora vamos fazer o posicionamento dos menus, definir a cor para os sub-menus e dispor como espa\u00e7os e blocos.<\/p>\n<pre class=\"top-margin:20 bottom-margin:20 left-set:true left-margin:20 right-set:true right-margin:20 whitespace-before:2 whitespace-after:2 lang:php decode:true\" title=\"Posicionamento\">\tnav ul li {\n\t\tfloat: left;\n\t}\n\t\tnav ul li:hover {\n\t\t\tbackground: #4b545f;\n\t\t\tbackground: linear-gradient(top, #4f5964 0%, #5f6975 40%);\n\t\t\tbackground: -moz-linear-gradient(top, #4f5964 0%, #5f6975 40%);\n\t\t\tbackground: -webkit-linear-gradient(top, #4f5964 0%,#5f6975 40%);\n\t\t}\n\t\t\tnav ul li:hover a {\n\t\t\t\tcolor: #fff;\n\t\t\t}\n\t\t\n\t\tnav ul li a {\n\t\t\tdisplay: block; padding: 15px 20px;\n\t\t\tcolor: #757575; text-decoration: none;\n\t\t}<\/pre>\n<p><iframe loading=\"lazy\" src=\"\/\/jsfiddle.net\/7ojkaybf\/7\/embedded\/result,html,css\/\" width=\"100%\" height=\"300\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>Neste ponto nosso menu come\u00e7a a ficar interessante, ganhando um visual e funcionando de modo prec\u00e1rio ainda. Agora, vamos adicionar os posicionamentos e cores. Veja que sub-menus agora ganham uma cor diferenciada e seus descendentes uma formata\u00e7\u00e3o para melhor apresenta\u00e7\u00e3o.<\/p>\n<pre class=\"top-margin:20 bottom-margin:20 left-set:true left-margin:20 right-set:true right-margin:20 whitespace-before:2 whitespace-after:2 lang:php decode:true\" title=\"Posicionamento...\">\tnav ul ul {\n\t\tbackground: #5f6975; border-radius: 0px; padding: 0;\n\t\tposition: absolute; top: 100%;\n\t}\n\t\tnav ul ul li {\n\t\t\tfloat: none; \n\t\t\tborder-top: 1px solid #6b727c;\n\t\t\tborder-bottom: 1px solid #575f6a; position: relative;\n\t\t}\n\t\t\tnav ul ul li a {\n\t\t\t\tpadding: 15px 40px;\n\t\t\t\tcolor: #fff;\n\t\t\t}\t\n\t\t\t\tnav ul ul li a:hover {\n\t\t\t\t\tbackground: #4b545f;\n\t\t\t\t}<\/pre>\n<p><iframe loading=\"lazy\" src=\"\/\/jsfiddle.net\/7ojkaybf\/8\/embedded\/result,html,css\/\" width=\"100%\" height=\"300\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>A barra de navega\u00e7\u00e3o est\u00e1 quase completa, agora ela j\u00e1 est\u00e1 eslizada mas seus sub-menus ainda apresentam algum problema de posicionamento. Eles est\u00e3o herdando configura\u00e7\u00f5es de seus pais, vamos mudar este comportamento para os sub-menus se posicionarem ao lado de seus elementos pai. Adicione mais este c\u00f3digo:<\/p>\n<pre class=\"top-margin:20 bottom-margin:20 left-set:true left-margin:20 right-set:true right-margin:20 whitespace-before:2 whitespace-after:2 lang:lisp decode:true\">\tnav ul ul ul {\n\t\tposition: absolute; left: 100%; top:0;\n\t}<\/pre>\n<p>Resultado:<\/p>\n<p><iframe loading=\"lazy\" src=\"\/\/jsfiddle.net\/7ojkaybf\/9\/embedded\/result,html,css\/\" width=\"100%\" height=\"300\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>No elemento final, adicionamos uns efeitos ao fundo, veja como ficou.<\/p>\n<p><iframe loading=\"lazy\" src=\"\/\/jsfiddle.net\/7ojkaybf\/11\/embedded\/result,html,css\/\" width=\"100%\" height=\"400\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>Dica: Utilizando o normalize.css, voc\u00ea obter\u00e1 uma maior compatibilidade com browsers antigos.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Este tutorial ensina a como criar um menu Dropdown com apenas recursos do CSS 3. Veja abaixo o resultado final: O menu que vamos criar \u00e9 composto de\u2026<\/p>\n","protected":false},"author":1,"featured_media":4093,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,6,28],"tags":[154,157,188,212,418],"class_list":["post-4088","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css-2","category-dicas","category-tutoriais","tag-css","tag-css3","tag-dicas-de-css","tag-efeito-css3","tag-menu-css"],"_links":{"self":[{"href":"https:\/\/www.turbosite.com.br\/blog\/wp-json\/wp\/v2\/posts\/4088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.turbosite.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.turbosite.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.turbosite.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.turbosite.com.br\/blog\/wp-json\/wp\/v2\/comments?post=4088"}],"version-history":[{"count":0,"href":"https:\/\/www.turbosite.com.br\/blog\/wp-json\/wp\/v2\/posts\/4088\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.turbosite.com.br\/blog\/wp-json\/wp\/v2\/media\/4093"}],"wp:attachment":[{"href":"https:\/\/www.turbosite.com.br\/blog\/wp-json\/wp\/v2\/media?parent=4088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.turbosite.com.br\/blog\/wp-json\/wp\/v2\/categories?post=4088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.turbosite.com.br\/blog\/wp-json\/wp\/v2\/tags?post=4088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}