Create Dropdowns UI with Next.JS and Tailwind CSS Last Updated : 26 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Dropdowns are an essential component of web applications used for navigation, filtering data or selecting items from a list. In this article, we’ll explore how to create a clean and responsive dropdown UI using Next.js and Tailwind CSS. Tailwind's utility-first CSS framework allows us to easily style components without writing custom CSS.PrerequisitesNext.jsReact.jsTailwind CSSApproach To Create Dropdowns UIStart by creating a new Next.js project using create-next-app.Create a dropdown component (Dropdown.js) in the components folder.We will create an array of dropdown options that we want to add.We will create a button that will toggle the dropdown and display the currently selected option.Use the React useState hook to manage the dropdown's open/close state.Add logic to handle user clicks. When a user selects an option update the button text and close the dropdown.We will use Tailwind’s utility classes to style the dropdown button and the menu options.Steps To Create Dropdowns UIStep 1: Create the NextJs App using the following command.npx create-next-app@latest dropdownConfiguration which you should follow while creating the App :Select the required options while creating ApplicationStep 2: Move to the project folder from Terminalcd dropdownStep 3: Install dependencynpm i react-iconsProject StructureFolder StructureDependencies"dependencies": { "next": "14.2.13", "react": "^18", "react-dom": "^18", "react-icons": "^5.3.0" },"devDependencies": { "postcss": "^8", "tailwindcss": "^3.4.1" }Step 4: Write the following code in the required files(The name of the files is mentioned in the first line of each code block) CSS /*global.css*/ @tailwind base; @tailwind components; @tailwind utilities; :root { --background: #ffffff; --foreground: #171717; } @media (prefers-color-scheme: dark) { :root { --background: #0a0a0a; --foreground: #ededed; } } body { color: var(--foreground); background: var(--background); font-family: Arial, Helvetica, sans-serif; } @layer utilities { .text-balance { text-wrap: balance; } } JavaScript // page.js import Dropdown from "./components/Dropdown"; export default function Home() { return ( <div> <h3 className="text-center text-3xl font-bold mt-12"> Select Course </h3> <Dropdown /> </div> ); } JavaScript // Dropdown.js 'use client' import { useState } from 'react'; import { FaCaretDown } from 'react-icons/fa'; export default function Dropdown() { const [isOpen, setIsOpen] = useState(false); const [selectedLanguage, setSelectedLanguage] = useState('Select Course'); const languages = ['DSA Self Placed', 'JavaScript', 'Python', 'Java', 'C++', 'Ruby', 'Go', 'TypeScript']; const toggleDropdown = () => { setIsOpen(!isOpen); }; const handleSelect = (language) => { setSelectedLanguage(language); setIsOpen(false); }; return ( <div className="flex justify-center min-h-screen"> <div className="relative inline-block text-left"> {/* Dropdown button */} <button type="button" className="inline-flex justify-center w-full rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-sm font-medium text-black hover:bg-gray-50" onClick={toggleDropdown} > {selectedLanguage} <FaCaretDown className="ml-2" /> </button> {/* Dropdown menu */} {isOpen && ( <div className="origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5 focus:outline-none"> <div className="py-1"> {languages.map((language, index) => ( <a key={index} href="#" className="block px-4 py-2 text-sm text-black hover:bg-gray-100" onClick={() => handleSelect(language)} > {language} </a> ))} </div> </div> )} </div> </div> ); } Run your app by executing below command.npm run devOutputCreate Dropdowns UI with Next.JS and Tailwind CSS Comment More infoAdvertise with us Next Article Create Form Layouts UI using Next.JS and Tailwind CSS Y yuvrajghule281 Follow Improve Article Tags : Web Technologies Tailwind CSS Next.js Similar Reads Create Dropdowns UI using React and Tailwind CSS Dropdown UI components are often used in web applications for forms, navigation, or user preferences, allow users to select from a list of options by clicking a button, which will display a drop-down menu, in this article we will create a dropdown element using React for functionality and Tailwind C 3 min read Create FAQs using Next.js and Tailwind CSS This tutorial will guide us through building a dynamic FAQ section in a Next.js application. The FAQ section will allow users to click on a question to reveal the corresponding answer, providing a clean and user-friendly interface. We'll use Tailwind CSS for styling, ensuring the component is fully 4 min read Create Form Layouts UI using Next.JS and Tailwind CSS This guide walks through building a responsive form layout in Next.js using Tailwind CSS for styling. It covers setting up a Next.js project, configuring Tailwind, and creating a form component with optional validation, ensuring a seamless development process for a modern web form UI.ApproachThe for 3 min read Create Footers using NextJS and Tailwind CSS The footer is an important part of any web application. It contains important links, social media icons, and copyright information, enhancing user experience and providing easy navigation. we will learn how to create a responsive footer using the Next.js application and Tailwind CSS.PrerequisitesJav 4 min read Create Hero Sections using Next.JS and Tailwind CSS We will learn how to create a beautiful Hero Section using Next.js and Tailwind CSS. A Hero Section is the first visual element a user sees on a website and it is very important for making a good first impression. It contains a bold headline, a call to action (CTA), and sometimes an image or video. 3 min read Create Flyout Menus using Next.js and Tailwind CSS We'll use Next.js as the framework to set up the project and Tailwind CSS for rapid styling. Tailwind makes it easy to style components without writing custom CSS for each element, keeping the development process smooth and efficient.PrerequisitesNode.js Next.jsTailwind CSSApproach For Creating Flyo 4 min read Like