Layout · 12 of 23

FAB Expand

Floating button reveal

HTML · CSS · No dependencies · MIT

HTML

<div class="fab-container">
  <button class="fab-main">+</button>
  <div class="fab-actions">
    <button class="fab-mini">A</button>
    <button class="fab-mini">B</button>
  </div>
</div>

CSS

.fab-container {
  position: relative;
  width: 50px;
  height: 50px;
  /* Bottom-anchor the FAB so the expanded stack has room to grow upward. In a
     flex container use justify-content: flex-end on the parent instead. */
  margin-top: 120px;
}

.fab-main {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: #7c5cff;
  border: none;
  color: #fff;
  font-size: 1.5rem;
  cursor: pointer;
  position: relative;
  z-index: 2;
  transition: transform 0.3s;
}

/* Actions stack directly above the button (bottom: 100%) instead of being pushed
   there by a fixed translateY(-100px). The old travel put the top mini button
   ~188px above the container, which overflowed a 240px demo box and clipped it.
   Stack height now: 2*40 + 8 gap + 8 offset + 50 button = 146px. */
.fab-actions {
  position: absolute;
  bottom: calc(100% + 0.5rem);
  left: 0;
  width: 100%;
  display: flex;
  flex-direction: column-reverse;
  align-items: center;
  gap: 0.5rem;
  z-index: 1;
}

.fab-mini {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #444;
  border: none;
  color: #fff;
  cursor: pointer;
  opacity: 0;
  transform: translateY(12px) scale(0.4);
  transition: opacity 0.25s cubic-bezier(0.16, 1, 0.3, 1),
              transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

/* Hovering (or tabbing into) the container: + rotates into x, minis pop upward */
.fab-container:hover .fab-main,
.fab-container:focus-within .fab-main {
  transform: rotate(45deg);
}

.fab-container:hover .fab-mini,
.fab-container:focus-within .fab-mini {
  opacity: 1;
  transform: translateY(0) scale(1);
}

/* Nearest button leads, so the stack unfurls upward */
.fab-container:hover .fab-mini:nth-child(2),
.fab-container:focus-within .fab-mini:nth-child(2) {
  transition-delay: 0.06s;
}