CSS Dropdowns

Dropdown menus are a common UI element that allows users to select an option from a list. They are often used in navigation bars or forms. CSS can be used to create and style dropdowns effectively.

Basic Dropdown Menu

Here’s a simple example of a basic dropdown menu:


/* Basic Dropdown Menu */
.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown-content a:hover {
    background-color: #f1f1f1;
}

.dropdown:hover .dropdown-content {
    display: block;
}

.dropdown:hover .dropbtn {
    background-color: #3e8e41;
}

        

Example: Basic Dropdown Menu

Responsive Dropdown Menu

For a responsive dropdown menu that works well on mobile devices, you can use media queries and flexbox:


/* Responsive Dropdown Menu */
.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown-content a:hover {
    background-color: #f1f1f1;
}

.dropdown:hover .dropdown-content {
    display: block;
}

.dropdown:hover .dropbtn {
    background-color: #3e8e41;
}

/* Responsive Design */
@media (max-width: 768px) {
    .dropdown-content {
        min-width: 100%;
        position: static;
    }
    
    .dropdown-content a {
        padding: 14px;
        text-align: center;
    }
}

        

Example: Responsive Dropdown Menu