JSFiddle - React, Tailwind, and code Playground

by jimmzzz

HTML

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Dropdown with Fixed Button</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="dropdown-container">
    <button class="dropdown-toggle">Select Option</button>
    <div class="dropdown">
      <div class="dropdown-list-container">
        <ul class="dropdown-list">
          <li class="dropdown-item">Option 1</li>
          <li class="dropdown-item">Option 2</li>
          <li class="dropdown-item">Option 3</li>
          <li class="dropdown-item">Option 4</li>
          <li class="dropdown-item">Option 5</li>
          <li class="dropdown-item">Option 6</li>
          <li class="dropdown-item">Option 7</li>
        </ul>
      </div>
      <button class="fixed-action-button">Submit</button>
    </div>
  </div>
</body>
</html>

CSS

/* General Reset */
body {
  margin: 0;
  font-family: Arial, sans-serif;
  background-color: #f5f5f5;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

/* Dropdown Container */
.dropdown-container {
  position: relative;
  width: 250px;
}

/* Dropdown Toggle */
.dropdown-toggle {
  width: 100%;
  padding: 12px 16px;
  font-size: 16px;
  color: #fff;
  background-color: #007BFF;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  text-align: left;
  transition: background-color 0.3s;
}

.dropdown-toggle:hover {
  background-color: #0056b3;
}

/* Dropdown */
.dropdown {
  display: none; /* Initially hidden */
  position: absolute;
  top: 100%;
  left: 0;
  width: 100%;
  background-color: #fff;
  border: 1px solid #ddd;
  border-radius: 4px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  max-height: 300px; /* Define a maximum height for the dropdown */
  display: flex;
  flex-direction: column;
}

/* Dropdown List Container */
.dropdown-list-container {
  flex-grow: 1; /* Allows the list to grow within the dropdown */
  overflow-y: auto; /* Makes the list scrollable if content exceeds */
}

/* Dropdown List */
.dropdown-list {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.dropdown-item {
  padding: 12px 16px;
  font-size: 14px;
  color: #333;
  cursor: pointer;
  transition: background-color 0.3s;
}

.dropdown-item:hover {
  background-color: #f0f0f0;
}

/* Fixed Action Button */
.fixed-action-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
  color: #fff;
  background-color: #28a745;
  border: none;
  cursor: pointer;
  border-top: 1px solid #ddd;
  flex-shrink: 0; /* Prevents the button from shrinking */
  transition: background-color 0.3s;
}

.fixed-action-button:hover {
  background-color: #218838;
}

/* Show Dropdown */
.dropdown-container:hover .dropdown {
  display: flex;
}