{"id":270203,"date":"2018-05-01T06:59:52","date_gmt":"2018-05-01T13:59:52","guid":{"rendered":"http:\/\/css-tricks.com\/?p=270203"},"modified":"2020-12-13T10:17:16","modified_gmt":"2020-12-13T18:17:16","slug":"solved-with-css-dropdown-menus","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/solved-with-css-dropdown-menus\/","title":{"rendered":"Solved with CSS! Dropdown Menus"},"content":{"rendered":"\n

CSS is getting increasingly powerful, and with features like CSS grid and custom properties (also known as CSS variables), we\u2019re seeing some really creative solutions emerging. Some of those solutions focus around not only making the web prettier, but making it more accessible, and making styling accessible experiences better. I\u2019m definitely here for it!<\/p>\n\n\n\n\n\n\n\n

\n

Article Series:<\/h4>\n
    \n
  1. Colorizing SVG Backgrounds<\/a><\/li>\n
  2. Dropdown Menus (this post)<\/li>\n
  3. Logical Styling Based On the Number of Given Elements<\/a><\/li>\n<\/ol>\n<\/div>\n\n\n\n
    \n\n\n\n

    A common UI<\/abbr> pattern that we see on the web are dropdown menus. They\u2019re used to display related information in pieces, without overwhelming the user with buttons, text, and options. Somewhere that we see these a lot is inside of headers or navigation areas on websites.<\/p>\n\n\n\n

    \"A
    A Google search for “dropdown menu” yields many examples<\/figcaption><\/figure>\n\n\n\n

    Let\u2019s see if we can make one of these menus with CSS alone. We\u2019ll create a list of links within a nav component like so:<\/p>\n\n\n\n

    <nav role=\"navigation\">\n  <ul>\n    <li><a href=\"#\">One<\/a><\/li>\n    <li><a href=\"#\">Two<\/a><\/li>\n    <li><a href=\"#\">Three<\/a><\/li>\n  <\/ul>\n<\/nav><\/code><\/pre>\n\n\n\n

    Now, say we want a sub-menu dropdown on the second navigation item. We can do the same thing there and include a list of links within that list item:<\/p>\n\n\n\n

    <nav role=\"navigation\">\n  <ul>\n    <li><a href=\"#\">One<\/a><\/li>\n    <li><a href=\"#\">Two<\/a>\n      <ul class=\"dropdown\">\n        <li><a href=\"#\">Sub-1<\/a><\/li>\n        <li><a href=\"#\">Sub-2<\/a><\/li>\n        <li><a href=\"#\">Sub-3<\/a><\/li>\n      <\/ul>\n    <\/li>\n    <li><a href=\"#\">Three<\/a><\/li>\n  <\/ul>\n<\/nav><\/code><\/pre>\n\n\n\n

    We now have our two-tiered navigation system. In order to have the content hidden and displayed when we want it to be visible, we\u2019ll need to apply some CSS. All style properties have been removed from the following example for clarity on interaction:<\/p>\n\n\n\n

    li {\n display: block;\n transition-duration: 0.5s;\n}\n\nli:hover {\n  cursor: pointer;\n}\n\nul li ul {\n  visibility: hidden;\n  opacity: 0;\n  position: absolute;\n  transition: all 0.5s ease;\n  margin-top: 1rem;\n  left: 0;\n  display: none;\n}\n\nul li:hover > ul,\nul li ul:hover {\n  visibility: visible;\n  opacity: 1;\n  display: block;\n}\n\nul li ul li {\n  clear: both;\n  width: 100%;\n}<\/code><\/pre>\n\n\n\n