Create Drop-Down Menus with Plain CSS

"Building the Future, One Line of Code at a Time!"
I am a full stack developer with a passion for creating, building, and designing products from scratch. My focus is on delivering exceptional user experiences through seamless UI/UX design.
I began my journey in the electronics domain, where I honed my skills in embedded C and led a team of 10+ students in various small-scale projects. My entrepreneurial spirit led me to focus on the elderly care segment, where I validated the problem statement with 50+ people, built an MVP and ran tests on 5+ individuals. However, things didn't pan out as I hoped.
My love for design prompted me to transition to the design field, where I enjoyed creating various designs, ranging from graphic design to web UIs. But I felt like something was missing and that's when I took the leap into web development. This transition has been incredibly rewarding and has allowed me to create beautiful and functional designs that are brought to life through my technical skills and eye for design.
I specialize in using a diverse tech stack as follows:
- Front-End: Next.js, React.js, Solid.js, TypeScript, Blockly.js, A-Frame, Three.js, HTML, and CSS.
- Back-End: Node.js, Express.js, Mongoose, and REST APIs.
- Database/Cloud: MongoDB, AWS (Lambda, EC2, Cloudfront, ELB, S3), and Cloudinary.
- Front-End Tools: React Redux, Recoil, Redis, webRTC, Github, Gitlab, and Netlify.
- CSS Framework: Styled Components, Lit Element, Chakra UI, Material UI, and Bootstrap.
- Analytics: Amplitude and Google Analytics.
Outside of work, I love mentoring others, learning about psychology and finance, speaking in public, and exploring visual media and communication. Cooking and playing football are also among my favorite hobbies.
Dropdown menus are a great way of including a long list of links without cluttering up your page. The issue though is that they can be hard to style, but look quite ugly if you don’t.
I have tried my level-best to keep the code as short & simple as possible. This is built with plain CSS and HTML only. I have not used any library.
Before we begin, let me show you a glimpse of what we are going to create.

Creating the HTML Part
Breaking down the components:

Now we can move along to create the HTML elements.
<nav id="nav-element">
<button id="button-dropdown">Dropdown </button>
<div id="div-products">
<a href="">Product 1 </a>
<a href="">Product 2 </a>
<a href="">Product 3 </a>
<a href="">Product 4 </a>
<a href="">Product 5 </a>
</div>
</nav>
Adding the CSS:
Let us first add CSS to the "nav" tag.
/* <nav> element */
#nav-element {
position: relative;
display: inline-block;
}
Note: In display, Use inline-block as only when we hover in-line, the drop-down appears. Do not use block element, as then the drop-down will appear when we hover around the block.
Adding CSS to the "button" tag.
/* <button> element */
#button-dropdown {
width: 10rem;
margin: auto;
padding: 0.5rem 0rem;
background-color: #3b70ff;
color: white;
border: 1px solid #3b70ff;
}
Adding CSS to "div" tag.
/* <div> element */
#div-products {
display: none;
width: 10rem;
margin: auto;
color: #3b70ff;
border: 1px solid #3b70ff;
}
Note: When creating these components initially, set "display: block", else you will not be able to see the output.
Adding CSS to "a" tag.
/* <a> element */
#div-products a {
display: block;
text-decoration: none;
padding: 0.5rem 0rem;
}
Note: We use "block" as "a" tag is an in-line element ie. the output of all the "a" tags are in the same line.
Hurray! Now you are 95% done with the work. All that is remaining is the hover features. Let us get right into it!
Adding the hover feature:
When we hover over the "nav" element, the display of "div" tag should appear ie. drop-down menu should appear.
#nav-element:hover #div-products {
display: block;
}
Another feature:
When we hover over the "a" elements ie. Product 1, Product 2, etc., we want a change in background color & color of the text.
#div-products a:hover {
background-color: #3b70ff;
color: white;
}
Aaaanddd, we are done. Do try the code and let me know if you found it useful.
Here's a link to the live demo: https://71cqf.csb.app/
Disclaimer: This is my first blog into the tech space. I am a complete newbie into the web dev world, so if I haven't practiced the best-practices - do let me know & I'll be more than glad to correct myself. :D
I document my journey and experiences on Twitter and LinkedIn.

