Styling in React Native: Inline Styles vs Common Issues with NativeWind and Their Fixes
When working with React Native, developers have several options for styling their applications. One of the most common approaches is using inline styles with JavaScript objects, but frameworks like Tailwind CSS—or more specifically, NativeWind for React Native—are also gaining popularity. Let’s break down the different approaches, when to use them, and how they fit into the React Native ecosystem.
1. Inline Styles in React Native (Standard CSS in JS)
React Native’s core styling approach is using JavaScript objects to define styles, which are then passed to the style
prop. This is very similar to writing inline CSS in web development, but with a twist: instead of using CSS syntax, you use camelCase property names (e.g., backgroundColor
instead of background-color
).
Example:
jsCopy code<View style={{ alignItems: 'center', justifyContent: 'center' }}>
<Image
source={icon}
resizeMode="contain"
style={{ width: 24, height: 24, tintColor: color }} // Set size and color of the icon
/>
<Text
style={{
color: color,
fontWeight: focused ? '600' : '400',
fontSize: 10,
marginTop: 6
}}
>
{name}
</Text>
</View>
In this example:
We use a JavaScript object inside the
style
prop to align items, control dimensions, and apply color to theImage
andText
components.This method provides precise control over styling but can get verbose if multiple elements require complex styles.
Pros:
Native to React Native: No need for additional dependencies or libraries.
Precise: Every element can have its own unique style directly applied.
Familiar: Works similarly to traditional React and CSS in JS.
Cons:
Verbosity: Can become cumbersome for larger components.
Limited reusability: Styles are tightly coupled to components, reducing reusability.
2. Tailwind CSS for Web: A Utility-First Approach
Tailwind CSS is a utility-first CSS framework that has become extremely popular in web development. It encourages developers to build components using small, reusable utility classes (e.g., text-center
, bg-blue-500
).
However, Tailwind is primarily meant for web development. React Native uses a different styling approach, meaning Tailwind doesn’t work natively with React Native. To bridge this gap, libraries like NativeWind have emerged.
3. NativeWind: Tailwind-Like Styling in React Native
NativeWind brings Tailwind’s utility-first styling methodology to React Native. It lets developers write utility classes directly in their components, just like they would in a Tailwind CSS-based web project. This improves developer experience by offering an easy way to apply styles without writing lengthy inline style objects.
Example (with NativeWind):
jsCopy code<View className="items-center justify-center gap-2">
<Image className="w-6 h-6" style={{ tintColor: color }} />
<Text className={`text-xs ${focused ? 'font-semibold' : 'font-normal'} mt-2`} style={{ color: color }}>
{name}
</Text>
</View>
In this example:
The
className
attribute replaces thestyle
prop.Instead of defining custom styles, we use utility classes like
w-6
(width of 6),mt-2
(margin-top of 2), andfont-semibold
.NativeWind automatically translates these Tailwind-like classes into React Native styles behind the scenes.
Pros:
Faster development: Utility classes speed up development by reducing boilerplate code.
Consistency: Encourages a consistent design system across the app.
Readability: Classes like
text-xs
orw-6
are more readable and intuitive than the equivalent inline styles.
Cons:
Extra setup: NativeWind needs to be installed and configured before use.
Learning curve: Developers need to learn the Tailwind utility classes if they’re not already familiar with them.
Common Issues with NativeWind and Their Fixes
While NativeWind offers a convenient, Tailwind-like approach to styling in React Native, it doesn’t always behave perfectly. Below are some common issues you might encounter when using NativeWind, along with their resolutions using inline styles.
Example 1: Gap between Elements Using NativeWind
Problematic Code:
jsCopy code<View className="items-center justify-center gap-2">
<Image source={icon} className="w-6 h-6" style={{ tintColor: color }} />
<Text className={`text-xs ${focused ? 'font-semibold' : 'font-normal'}`} style={{ color: color }}>
{name}
</Text>
</View>
Issue: The gap-2
utility class does not work for spacing, leading to no visual gap between the icon and the text.
Fix: Use inline styles:
jsCopy code<View style={{ alignItems: 'center', justifyContent: 'center' }}>
<Image source={icon} style={{ width: 24, height: 24, tintColor: color }} />
<Text style={{ color: color, fontSize: 10, fontWeight: focused ? '600' : '400', marginTop: 6 }}>
{name}
</Text>
</View>
Example 2: Class Names Not Working with NativeWind
Problematic Code:
jsCopy code<View className="items-center justify-center">
<Image source={icon} className="w-6 h-6" style={{ tintColor: color }} />
<Text className={`text-xs ${focused ? 'font-semibold' : 'font-normal'} mt-2`} style={{ color: color }}>
{name}
</Text>
</View>
Issue: The w-6
and h-6
classes might not resize the image as expected due to limitations in NativeWind.
Fix: Use inline styles:
jsCopy code<View style={{ alignItems: 'center', justifyContent: 'center' }}>
<Image
source={icon}
resizeMode="contain"
style={{ width: 24, height: 24, tintColor: color }}
/>
<Text
style={{
color: color,
fontWeight: focused ? '600' : '400',
fontSize: 10,
marginTop: 6
}}
>
{name}
</Text>
</View>
Example 3: Using Tailwind-Specific Colors Not Working in NativeWind
Problematic Code:
jsCopy code<View className="bg-blue-500">
<Text className="text-white">Hello World</Text>
</View>
Issue: NativeWind may not support all Tailwind CSS colors, leading to unstyled components.
Fix: Use inline styles:
jsCopy code<View style={{ backgroundColor: '#3B82F6' }}>
<Text style={{ color: '#FFFFFF' }}>Hello World</Text>
</View>
Example 4: Font Weight and Text Alignment Issues in NativeWind
Problematic Code:
jsCopy code<Text className={`text-xs ${focused ? 'font-semibold' : 'font-normal'} text-center`}>
Profile
</Text>
Issue: Font weights might not change as expected, and text alignment may not work correctly.
Fix: Use inline styles:
jsCopy code<Text
style={{
color: color,
fontSize: 12,
fontWeight: focused ? '600' : '400',
textAlign: 'center'
}}
>
Profile
</Text>
Summary of Issues and Fixes:
Issue in NativeWind | Fix with Inline Styles |
gap-2 class does not work for spacing | Use marginTop or padding inline for spacing between elements |
w-6 and h-6 not resizing Image correctly | Use style={{ width: 24, height: 24 }} for explicit sizing |
Tailwind color classes like bg-blue-500 fail | Use backgroundColor and color hex codes directly in inline styles |
Font weight inconsistencies with font-semibold | Use fontWeight: '600' and fontWeight: '400' in inline styles |
text-center not centering text | Use textAlign: 'center' in inline styles |
When to Use Each Approach?
Inline Styles:
Best for when you need complete control over styles and want to leverage React Native’s built-in capabilities.
Ideal for small projects or components that have highly custom styles that don't repeat across the app.
NativeWind:
Great for larger projects where consistent design and utility-first classes speed up development.
Ideal for developers already familiar with Tailwind CSS and want to apply similar styling practices in React Native.
Final Thoughts
Styling in React Native provides flexibility through inline styles, which offer precise control and customization for components. On the other hand, NativeWind introduces a utility-first approach that enhances development speed and consistency, especially for larger projects. Ultimately, the choice between these methods depends on your project needs and personal preference for style management.
Stay tuned for more updates on how you can streamline your React Native development process! Happy coding!
Subscribe to my newsletter
Read articles from Prakash Das directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Prakash Das
Prakash Das
Software Engineer, focused on building and designing accessible, human-centered products and digital experiences