Course Content
CSS Basic
0/1
CSS Selectors
0/1
CSS Comments
0/1
CSS Backgrounds
0/1
CSS Borders
0/1
CSS Outline
0/1
CSS Fonts
0/1
CSS Height and Width
0/1
CSS Margins and Paddings
0/2
CSS Icons
0/1
CSS Links
0/1
CSS Lists
0/1
CSS Tables
0/1
CSS Display Properties
0/1
CSS Max-Width Property
0/1
CSS Positioning Elements
0/1
CSS Z-Index Property
0/1
CSS Overflow
0/1
CSS Float
0/1
CSS Opacity
0/1
CSS Forms
0/1
CSS Dropdowns
0/1
CSS Buttons
0/1
CSS Media Queries
0/1
About Lesson

How to create Dropdowns using CSS?

CSS dropdowns are a type of user interface element that allows users to select an option from a list of choices that appears when they interact with a trigger element. Dropdowns are commonly used in navigation menus, form select elements, or any scenario where a user needs to choose from multiple options.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <title>CSS Dropdown Example</title>
    <link rel="stylesheet" href="style.css"/>
</head>
<body>
    <div class="dropdown">
        <button class="dropbtn">Menu</button>
        <div class="dropdown-content">
            <a href="#">Option 1</a>
            <a href="#">Option 2</a>
            <a href="#">Option 3</a>
        </div>
    </div>
</body>
</html>
CSS
.dropdown {
    position: relative;
    display: inline-block;
}
  • position: relative;: Positions the dropdown relative to its normal position in the document flow. This is necessary for absolute positioning of the dropdown content.
  • display: inline-block;: Makes the dropdown behave like an inline element while allowing its dimensions to be set (in contrast to inline elements, which do not respect height and width properties).
CSS
.dropbtn {
    background-color: #3498db;
    color: #fff;
    padding: 10px;
    border: none;
    cursor: pointer;
}
  • background-color: #3498db;: Sets the background color of the dropdown button to a shade of blue (#3498db).
  • color: #fff;: Sets the text color of the dropdown button to white.
  • padding: 10px;: Adds 10 pixels of padding to the top and bottom, and left and right of the dropdown button.
  • border: none;: Removes any border from the dropdown button.
  • cursor: pointer;: Changes the cursor to a pointer when hovering over the dropdown button, indicating it is clickable.
CSS
.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
    z-index: 1;
}
  • display: none;: Initially hides the dropdown content.
  • position: absolute;: Positions the dropdown content absolutely with respect to the nearest positioned ancestor.
  • background-color: #f9f9f9;: Sets the background color of the dropdown content to a light gray (#f9f9f9).
  • min-width: 160px;: Sets the minimum width of the dropdown content to 160 pixels.
  • box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);: Adds a shadow effect to the dropdown content.
  • z-index: 1;: Sets the stacking order of the dropdown content to ensure it appears above other elements.
CSS
.dropdown-content a {
    color: #333;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}
  • color: #333;: Sets the text color of the dropdown content links to a dark gray (#333).
  • padding: 12px 16px;: Adds 12 pixels of padding to the top and bottom, and 16 pixels of padding to the left and right of each link.
  • text-decoration: none;: Removes the underline decoration from the dropdown content links.
  • display: block;: Makes the dropdown content links behave like block-level elements, causing them to stack vertically.
CSS
.dropdown-content a:hover {
    background-color: #ddd;
}
  • background-color: #ddd;: Changes the background color of the dropdown content links to a lighter gray (#ddd) when hovered over.
CSS
.dropdown:hover .dropdown-content {
    display: block;
}
  • This rule ensures that when you hover over the dropdown container, the dropdown content becomes visible by changing its display property to block.