DEV Community

Cover image for React Styling Methods
Himanshu Vaidya
Himanshu Vaidya

Posted on

React Styling Methods

Have you ever wondered how developers get their webpages to look so good? Ever taken a thought into how much work goes behind the design? Probably not, but it may or may not be as difficult as you initially thought. There are several different methods to help style your web pages, and through React, you are able to explore a vast number of options to help make you pages look nice and pretty! We are going to go over some of those methods to give you an idea for your next project, whether it is through simple CSS, or requires the help of an additional tools to really impress your viewers.

There are many different ways of designing and styling webpages, but this is all dependent on several different factors. These factors include the language that you are using, the tools and extras associated with language, and ultimately, your comfort level in coding. If you are just beginning your journey in coding, you may want to opt for the easier methods, rather than using the more advanced methods as this will only create confusion and possibly deter you from making any progress. Below, we will see some of the various styling methods and also a few examples of what your code should look like and how you should go about getting started with the process. Please note that these are only a number of the many methods out there and that this should not be limited to your choices.

Inline CSS

One of the more simpler approaches would be to use the inline CSS style. This method isn't the preferred method as it can lead to some issues with stability and readability but can be used if you don't have too much to style or you want to get some sort of stying in quickly without having to disrupt too much of your code. You can simply define a variable and within that variable, you can add your styling in directly. Because this is a regular JavaScript object, you cannot incorporate the normal CSS properties, but you are able to use basic styling that will be quick and efficient.

code1

Regular CSS

Regular CSS is the more common approach, and is likely the more used method rather than inline CSS. The styles can be imported to any number of pages and elements unlike inline CSS, which is applied directly to the particular element. Regular CSS has several advantages, such as native browser support (it requires no dependencies), there are no extra plugins required or tools to learn, and you can style as much as you want without effecting your current code with the className method. This is key because it will only style the className that you chose to style and will not effect any other elements. You can choose any class and everything within that class would adopt your styling. This can make for quick changes to multiple elements sharing the same className.

You can maintain any number of style sheets, and it can be easier to change or customize styles when needed. However, regular CSS can cause issues if you are working with a large team or if you have a very large project that has multiple components and many lines of code. This can become very time consuming and cumbersome, so it's best to use this accordingly.

Styled Components

Another very popular method is the styled component method. This builds custom components using actual CSS in your JavaScript. A styled-component is basically it's own React component that is used just for styling purposes. Some of the features include unique class names, dynamic styling and better management of the CSS as each component has its own separate styles. By focusing on single use case, the developer has a better experience managing CSS. Also, styled-components keep track of the components that are being rendered on the page and injects their styles and nothing else. Combined with code splitting, the app will load faster as well.

npm package install

In order to use this method, you would need to take up some extra steps than you normally would and install an "nmp package". Once you have done this, you would import the "styled-component" into the React app and then you would go about styling as you please.

code2

CSS Modules

Moving on, we have the CSS Module method. A CSS module is, in simple words, a CSS file. But with a key difference: by default, when imported, every class name and animation inside a CSS module is scoped locally to the component that is importing it. This allows you to use virtually any valid name for your classes, without worrying about conflicts with other class names in your application. CSS modules also allow you to extend one or more classes, inheriting their styles. This concept is called class composition.

code3

You can use this method by creating a css module and assign the class names accordingly. Once you have styled each of the classes, you can import that file into your App component and this will make your modules available throughout your App. This makes keeping track of what you want to style and where it is located, very easy to do. This can be beneficial in styling a block of code in specific areas. But this can also be overwhelming if you are styling a very small amount, so it is important to think of the scope in which you are working within and what you are trying to achieve so that you are able to work in the most efficient manner.

React Frameworks

Lastly, we can dive even deeper and implement the use of different React Frameworks, which are packages of code that have different functionalities and templates of code that is prewritten for a specific purpose. These templates provide an idea of what you are looking for specifically and can be implemented fairly easily by simply copy/pasting the code directly into your project. There are thousands of templates listed online for public use. Some of the more popular frameworks include React BootStrap, Semantic UI, Material UI, Ant Design, and many others just to name a few. You can download the packages directly from your terminal and will be ready to use immediately.

Conclusion

As seen above, there are many methods that are at your disposal to help style your web app and help bring your app to life. Once you have become familiarized with the standard methods of styling, you can move on to bigger and more powerful methods which will really help you stand out from the rest. The more you practice, the better your Apps will become so be sure to use the methods above and you will become a master in styling in no time!

Top comments (0)