Untitled
unknown
plain_text
a year ago
3.3 kB
6
Indexable
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CSS-Only Custom Dropdown with Groups</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="dropdown-container"> <input type="checkbox" id="dropdown-toggle" class="dropdown-toggle"> <label for="dropdown-toggle" class="dropdown-button"> <span class="dropdown-selected">Select an option</span> <span class="dropdown-arrow">▾</span> <!-- Downward arrow character --> </label> <div class="dropdown-options"> <div class="dropdown-group"> <div class="dropdown-group-title">Group 1</div> <label for="dropdown-toggle" class="dropdown-option">Option 1.1</label> <label for="dropdown-toggle" class="dropdown-option">Option 1.2</label> </div> <div class="dropdown-group"> <div class="dropdown-group-title">Group 2</div> <label for="dropdown-toggle" class="dropdown-option">Option 2.1</label> <label for="dropdown-toggle" class="dropdown-option">Option 2.2</label> <label for="dropdown-toggle" class="dropdown-option">Option 2.3</label> </div> <div class="dropdown-group"> <div class="dropdown-group-title">Group 3</div> <label for="dropdown-toggle" class="dropdown-option">Option 3.1</label> <label for="dropdown-toggle" class="dropdown-option">Option 3.2</label> </div> </div> </div> </body> </html> /* styles.css */ body { font-family: Arial, sans-serif; margin: 20px; } .dropdown-container { position: relative; display: inline-block; width: 200px; } .dropdown-toggle { display: none; } .dropdown-button { display: flex; justify-content: space-between; align-items: center; width: 100%; padding: 10px; border: 1px solid #ccc; background-color: #f9f9f9; cursor: pointer; box-sizing: border-box; position: relative; } .dropdown-arrow { margin-left: 10px; font-size: 12px; color: #333; } .dropdown-options { display: none; position: absolute; width: 100%; background-color: #fff; border: 1px solid #ccc; box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2); box-sizing: border-box; z-index: 1; max-height: 150px; overflow-y: auto; } .dropdown-group { margin-bottom: 20px; } .dropdown-group-title { padding: 10px; font-weight: bold; background-color: #f0f0f0; } .dropdown-option { display: block; padding: 10px 10px 10px 30px; /* Left padding for nested effect */ cursor: pointer; color: black; } .dropdown-option:hover, .dropdown-option:focus { background-color: #f1f1f1; } /* Show dropdown options when checkbox is checked */ .dropdown-toggle:checked ~ .dropdown-options { display: block; } /* Update the selected value */ .dropdown-option:active { background-color: #e1e1e1; } .dropdown-option:focus::before { content: attr(title); position: absolute; left: -9999px; overflow: hidden; white-space: nowrap; visibility: hidden; }
Editor is loading...
Leave a Comment