Course contentsShow
← React Native
Lesson 302 of 1,5554. Flexbox and Native StylingFree lesson

Managing Floating Action Buttons

Position interactive buttons globally while maintaining proper screen coverage and padding.

In this lesson, you will learn how to anchor Floating Action Buttons (FABs) to the screen edges while ensuring they remain accessible and responsive across different device sizes.

The Power of Absolute Positioning

When building a Floating Action Button, the standard flex layout isn't enough because a FAB needs to "float" independently of the main content flow. We achieve this by using position: 'absolute' within a relative parent container. By setting position: 'absolute', you take the element out of the normal document flow, allowing it to overlap other components like ScrollViews or lists.

To position it correctly, you use the bottom, right, top, or left properties. The trick to a professional-looking FAB is combining these coordinates with consistent padding.

Maintaining Screen Coverage

A common mistake is hard-coding pixels for spacing. Because modern phones have varying screen sizes and "safe areas" (like home indicators or notches), you should always apply padding that respects the device's boundaries.

const styles = StyleSheet.create({
  container: {
    flex: 1,
    // Ensures the container takes up full screen space
  },
  fab: {
    position: 'absolute',
    bottom: 20, // Distance from the bottom edge
    right: 20,  // Distance from the right edge
    width: 56,
    height: 56,
    borderRadius: 28, // Perfectly circular
    backgroundColor: '#6200EE',
    justifyContent: 'center',
    alignItems: 'center',
    elevation: 8, // Adding depth via dynamic shadow casting
  },
});

Pro-Tip: The Parent Container

For this layout to work, the FAB must be a child of a View that occupies the full screen height (usually the root View or a SafeAreaView). If you nest the FAB inside a smaller container, your bottom: 20 will be relative to that container’s bottom, not the screen's edge. By keeping the FAB at the root level of your screen component, you ensure it stays pinned exactly where you want it regardless of what is happening in your primary content area (like a long list of items).

Always ensure your FAB has a high zIndex if you are working with complex layouts, so it doesn't get hidden behind other components.

Key Takeaway: Use position: 'absolute' combined with bottom and right offsets to pin FABs, ensuring they are placed at the root level of your view hierarchy to avoid layout clipping.