How to use Radium in ReactJS for inline styling?

  • react-js
  • 74 Views
  • 3 Min Read
  • 7 May 2024

Hey developers! Today I'll teach you how can we use Radium to apply inline styles in React. Inline CSS can be super handy, especially when you need to tweak small styles quickly.

 

Radium is a handy tool that lets us add inline styles, along with media query and pseudo selectors like :hover, :active, :visited, etc., to JSX elements. React's inline styles don't support these selectors by default, we'll need to install radium to use them effectively.

 

So let's get started and learn how to use it easily.

 

To use the Radium tool, you need to install its package in your project. For this, execute the command given below.

 
// For npm
npm i --save radium --force

// For yarn
yarn add radium
 

Once installed, you can import radium into your components:

 
import Radium from 'radium';
 

To utilize radium's capabilities, wrap your component with the radium higher-order component (HOC) like this:

 
export default Radium(App);
 

We used this tool in the code example given below, which you can see to understand how to use it.

 
import React from 'react';
import Radium from 'radium';

function App() {
    const style = {
        button: {
            backgroundColor: '#fd6d1c',
            color: 'white',
            padding: '10px 20px',
            borderRadius: '5px',
            fontSize: '20px',
            cursor: 'pointer',
            border: '1px solid white',
            boxShadow: '2px 5px 5px #a3a3a3',
            transition: 'all 0.2s ease',
            ':hover': {
                backgroundColor: '#db5c13',
                transform: 'scale(1.1)',
            },
        },
        container: {
            display: 'grid',
            placeItems: 'center',
        }
    };

    return (
        <div style={style.container}>
            <h1>Click on this</h1>
            <button style={style.button}>Click Me!</button>
        </div>
    );
}

export default Radium(App);

 

React Radium Library

 

Many developers have asked, "Can we use inline media queries in React?"

 

The answer is yes, you can use media queries with the same Radium tools. To make use of media queries, you'll need to import StyleRoot from Radium and wrap your JSX or component with it.

 
// Importing StyleRoot from Radium to enable the usage of media queries
import { StyleRoot } from 'radium';

// Defining inline styles
const divStyle = {
    backgroundColor: 'red',
    "@media (max-width: 600px)": {
        backgroundColor: "blue",
    }
};

return (
    // Wrapping the component or JSX with StyleRoot to enable the usage of Radium's media queries
    <StyleRoot>
        <Home />
    </StyleRoot>
);
 
 

Vendor Prefixing

 

In web development, vendor prefixing is like adding insurance for your CSS. It's there to handle those tricky browser differences by tagging certain CSS properties with little codes. This way, your styles play nice on all browsers with no fuss.

 

For example, properties like transform and transition may require different prefixes for various browsers.

 
/* Standard CSS */
.element {
     transform: translateX(50px);
     transition: transform 0.3s ease;
     }
    
     /* Vendor-prefixed CSS */
     .element {
      -webkit-transform: translateX(50px); /* Safari and Chrome */
      -moz-transform: translateX(50px); /* Firefox */
      -ms-transform: translateX(50px); /* Internet Explorer */
      -o-transform: translateX(50px); /* Opera */
      transform: translateX(50px);
     
      -webkit-transition: -webkit-transform 0.3s ease; /* Safari and Chrome */
      -moz-transition: -moz-transform 0.3s ease; /* Firefox */
      -ms-transition: -ms-transform 0.3s ease; /* Internet Explorer */
      -o-transition: -o-transform 0.3s ease; /* Opera */
      transition: transform 0.3s ease;
     }
 

In this example, the transform and transition properties are vendor-prefixed for various browsers:

 

  • -webkit- prefix for Safari and Chrome
  • -moz- prefix for Firefox
  • -ms- prefix for Internet Explorer
  • -o- prefix for Opera
 
Without these prefixes, certain CSS features may not work correctly across different browsers. Radium, as mentioned earlier, automatically handles vendor prefixing for you, eliminating the need to manually add these prefixes to your code. This ensures consistent rendering and behavior of your styles across different browsers without the hassle of managing vendor prefixes manually.

 

 

Conclusion

 

Radium is great for adding inline styles directly to React apps. Its automated vendor prefixing feature guarantees consistent display across various browsers. With Radium, making good-looking and responsive interfaces is simple, which is super helpful for React developers.

 

But the important thing to note is that Radium is best suited for applying inline styles with advanced features like pseudo-selectors and media queries in smaller React projects. However, for larger projects, it might not be necessary. In such cases, utilizing external CSS or Tailwind CSS would be more appropriate.

 

If you have any questions about this solution or any other web development, you can ask them in the question box given below, and you will get the answer as soon as possible.

Didn't find your answer? Add your question.

Share

Comments (0)

No comments yet. Be the first to comment!