Adding a floating action button (FAB) or "Back to Top" trigger in the bottom-right corner of a website seems straightforward until mobile browser URL bars hide the button or iOS notches cut off touch targets. Building a robust floating button requires position: fixed, modern env(safe-area-inset-bottom) spacing, and clean accessibility focus management.
Production CSS & HTML Implementation
<!-- Floating Action Button (FAB) -->
<button class="fab-button" aria-label="Scroll to top of page">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M18 15l-6-6-6 6"/>
</svg>
</button>.fab-button {
/* Pin element to viewport bottom-right corner */
position: fixed;
bottom: clamp(1rem, 3vw, 2rem);
right: clamp(1rem, 3vw, 2rem);
/* Support mobile notch safe areas (iOS Safari) */
padding-bottom: max(0px, env(safe-area-inset-bottom));
padding-right: max(0px, env(safe-area-inset-right));
/* Flexbox centering for SVG icon */
display: inline-flex;
align-items: center;
justify-content: center;
width: 56px;
height: 56px;
border-radius: 50%;
background-color: #2563eb;
color: #ffffff;
border: none;
cursor: pointer;
z-index: 9999;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
transition: transform 0.2s ease, background-color 0.2s ease;
}
.fab-button:hover {
background-color: #1d4ed8;
transform: translateY(-2px);
}
.fab-button:focus-visible {
outline: 3px solid #93c5fd;
outline-offset: 2px;
}Key CSS Positioning Takeaways:
`position: fixed`: Removes the element from standard document flow and positions it relative to the browser viewport.
`env(safe-area-inset-bottom)`: Prevents touch controls from overlapping home indicator bars on modern mobile displays.
`z-index: 9999`: Ensures the button remains visible above scrolling content containers.
Comments and corrections