Mobile Dev

Center Heart Icon in React Navigation HeaderRight

Fix left alignment of Heart icon in React Navigation Stack.Screen headerRight using Tailwind. Override with headerRightContainerStyle for perfect centering in react native navigation across iOS and Android.

1 answer 1 view

How to center the Heart icon inside the headerRight container in React Navigation Stack.Screen?

I tried adding Tailwind classes items-center justify-center to the View, but it aligns to the left.

Here is my HeartButton component:

jsx
const HeartButton = () => (
 <TouchableOpacity
 onPress={handleFavouritePress}
 disabled={!mongoUser?.userId || isSaving}
 activeOpacity={0.7}
 >
 <View>
 <Heart
 color="#ef4444"
 size={22}
 fill={isFavourited ? '#ef4444' : 'none'}
 />
 </View>
 </TouchableOpacity>
);

Used in:

jsx
<Stack.Screen
 options={{
 headerRight: () => <HeartButton />,
 }}
/>

What styles should I apply to center the icon properly in React Native (using Tailwind/NativeWind)?

In React Navigation Stack.Screen setups, the headerRight slot defaults to left-aligning your Heart icon, even if you’ve slapped on Tailwind classes like items-center justify-center to the inner View. The quick fix? Override it with headerRightContainerStyle on your screen options—something like twjustify-center items-center`` to turn that right-side space into a proper flex container that centers the HeartButton perfectly. This works across iOS and Android, dodging those pesky safe area glitches, and pairs seamlessly with Tailwind React Native or NativeWind.


Contents


Why HeaderRight Aligns Left in React Navigation

Ever notice how your Heart icon hugs the left edge of the headerRight area, no matter what flex magic you throw at it inside the component? That’s React Navigation’s native stack behavior at work—especially on iOS 16+, where the header buttons treat their slots like rigid boxes without built-in centering. You added items-center justify-center to the View in HeartButton, but Tailwind styles on the child don’t override the parent container’s defaults.

This trips up tons of devs using react native navigation stacks. The headerRight isn’t a full flex parent by default; it’s more like a fixed-position buddy that shoves content left unless you intervene. GitHub discussions on react-navigation nail it: without explicit container styles, your icon expands or shifts, messing with the title alignment too. And if you’re on Expo or tailwind react native, safe areas can push it even further off-kilter.

But here’s the good news—it takes one prop to rule them all.


The Core Fix: headerRightContainerStyle

Don’t tweak the HeartButton endlessly. Target the wrapper with headerRightContainerStyle. This prop styles the invisible flex container React Navigation wraps around your headerRight render function.

Start simple:

jsx
options={{
 headerRight: () => <HeartButton />,
 headerRightContainerStyle: {
 justifyContent: 'center',
 alignItems: 'center',
 },
}}

Boom—centered Heart icon, snug in its slot. Why does this work? It flips the container to flex-center mode, constraining your button to natural size while positioning it dead center. For tailwind react native fans (NativeWind setup assumed), swap in:

jsx
headerRightContainerStyle: tw`justify-center items-center`,

Tested this on iOS simulators? Your icon won’t takeover full width anymore. React Navigation issues confirm: pair it with flex: 1 if needed for edge cases, like twflex-1 justify-center items-center``. Short and sweet, but it scales.

What if it’s still off? Padding might be your friend—more on that later.


Updating Your HeartButton for Perfect Centering

Your HeartButton looks solid, but that inner View? Make it flex-friendly to play nice with the container override.

Here’s the glow-up:

jsx
const HeartButton = () => (
 <TouchableOpacity
 onPress={handleFavouritePress}
 disabled={!mongoUser?.userId || isSaving}
 activeOpacity={0.7}
 style={tw`p-1`} // Tiny padding for touch target
 >
 <View style={tw`items-center justify-center p-1`}>
 <Heart
 color="#ef4444"
 size={22}
 fill={isFavourited ? '#ef4444' : 'none'}
 />
 </View>
 </TouchableOpacity>
);

Subtle changes: Added padding to the TouchableOpacity for better taps (headers are cramped), and kept centering on the View as backup. But remember—the real hero is headerRightContainerStyle. Without it, Tailwind on these won’t budge the default left align.

Pro tip: If isFavourited toggles fast, memoize the component with useCallback or React.memo to avoid re-renders nuking position. Community blogs on customizing headers swear by this combo for react navigation stack reliability.


Full Stack.Screen Example with Tailwind

Let’s wire it up. Assume react navigation v6+ and NativeWind.

jsx
import { Stack } from 'expo-router'; // Or your navigator
import tw from 'twrnc'; // NativeWind/twrnc

<Stack.Screen
 name="YourScreen"
 options={{
 headerRight: () => <HeartButton />,
 headerRightContainerStyle: tw`justify-center items-center pr-3`, // Right padding for edges
 headerTitleAlign: 'center', // Bonus: centers title too
 headerTransparent: false, // Or true if blurred
 }}
/>

Paste, run, admire. The pr-3 (padding-right) offsets from screen edges—crucial on Android. Your Heart sits pretty, fill state and all. Scaling to multiple buttons? Wrap in a row: twflex-row justify-center items-center gap-2`` inside containerStyle.

This mirrors fixes from multiple GitHub threads, where devs battled iOS flex quirks.


Platform Tweaks for iOS and Android

iOS 16 loves to rebel—icons cut off or left-shift? Native-screens issues blame fixed positioning. Amp up headerRightContainerStyle:

jsx
headerRightContainerStyle: Platform.select({
 ios: tw`justify-center items-center pr-4`,
 android: tw`justify-center items-center mr-4`,
}),

Android safe areas post-Expo updates? Reddit threads report insets eating buttons. Counter with marginRight: 16 or twmr-4. Expo Router users: slap it in screenOptions`.

Transparent headers? Add position: 'absolute' sparingly, but test on devices—simulators lie.

Platform Common Pitfall Tailwind Fix
iOS Full-width takeover flex-1 justify-center items-center
Android Safe area overlap mr-4 items-center justify-center

One tweak, cross-platform peace.


Advanced Tips for React Native Navigation Headers

Want navigation bar react native that shines? Beyond centering:

  • Multiple icons: headerRight: () => ( <View style={twflex-row items-center gap-2}> <HeartButton /> <OtherIcon /> </View> ), then containerStyle centers the group.

  • Dynamic states: Use useNavigation in a custom hook for navigation.setOptions({ headerRightContainerStyle: ... }).

  • Libraries boost: HeaderButtons wrappers auto-center with <HeaderButtonContainer style={twjustify-center items-center}>.

From best practices guides, always lazy-load icons (e.g., @expo/vector-icons) and avoid heavy logic in headerRight.

React tab navigation or bottom tabs? Same prop works there too. And for bottom navigation react, mirror with tabBarIcon styles.

Pushed 2500+ words? Nah, but this covers it deeply without fluff. Experiment—headers evolve with your app.


Sources

  1. headerRight sometimes takes full header width instead of its content width on iOS 16
  2. Customizing the Navigation Header in React Navigation
  3. Show the bottom border in header
  4. Render multiple buttons in HeaderRight/Left iOS16
  5. Using Expo, icons get cut off in App Bar on Android using headerRight
  6. r/expo: headerLeft/Right buttons in safearea insets on Android
  7. Best Practices for Creating Navigation in a React Native Project
  8. Header buttons are not centered in iOS 16
  9. Easily render header buttons for react navigation

Conclusion

Centering that Heart icon in React Navigation boils down to headerRightContainerStyle: twjustify-center items-center``—a game-changer that fixes left-align woes without hacking your HeartButton. Tweak for platforms, add padding, and you’re golden across react native navigation stacks. Grab the code, drop it in, and watch your navigation bar react native level up. Questions? Headers are finicky, but this nails 90% of cases.

Authors
Verified by moderation
Moderation
Center Heart Icon in React Navigation HeaderRight