What is CSS in JS?
CSS in JS is a way of writing css styles in javascript with much more flexiblity and features.
I have already created a video about this on the Cules Coding youtube channel.
Why CSS in JS?
Suppose you have a large-scale application with a lot of components.
Each component has different styles. So you add class names to them. Those class names have some styling.
Now it is easy to mess up with component names like the picture below.
And your whole layout breaks.
How can this problem be solved?
The solution is Scoping CSS
. Instead of writing css globally in css styles, what if we can write them inside
the component. Then the css styles won't affect other components.
But how do we do something like this?
This is where css-in-js comes into play. Css in js is writing css inside javascript. And we want to write css inside javascript. Now, what css-in-js the library will do is, it will generate a unique class name for the component. And that class will have all the styles.
It will add a unique hash to the class name. Just because the hash will be unique, that's why all the components will have a different style. Now we have Scoping CSS.
How do we write CSS in JS?
There are bunch of libraries out there. Popular ones are:
I like the Styled Component most. You can use whatever you want.
Don't think that all you can do with css-in-js is write scoping Css.
There are a lot of features that those libraries provide us. Like theming, props adaption, extending style, and so on.
And also remember you are writing in JavaScript. That means you can create your functionalities and features. So, don't let yourself restrict within the library features.
To learn more watch the Video.
Shameless Plug
Want to create your own blog? Well, I am creating a video series where you will learn about how to create a JAMstack blog with Nextjs and Chakra-UI.
Lessons
- Intro & Setup
- Build Homepage UI
- How our app will work
- MDX, MongoDB, Static Homepage
- Generate Static Blog Page
- Style Blog page with Chakra-UI and MDX-embed
- Build a real-time view counter
- Autocomplete search form with MongoDB Atlas Search Index
- Deploy application to Vercel
Demo
You can demo the website from here
Features
- Static Blog pages will make the website load faster.
- Blogs will have code blocks with syntax highlighting and many embed components like youtube videos, GitHub gist, Tweets, and so many other things.
- Autocomplete search feature for the blog posts.
- Real-time view counter and so on.
Please like and subscribe to Cules Coding. It motivates me to create more content like this.
That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.
By the way, I am looking for a new opportunity in a company where I can provide great value with my skills. If you are a recruiter, looking for someone skilled in full-stack web development and passionate about revolutionizing the world, feel free to contact me. Also, I am open to talking about any freelance project. I am available on Upwork
Contacts
- Email: thatanjan@gmail.com
- LinkedIn: @thatanjan
- Portfolio: anjan
- Github: @thatanjan
- Instagram (personal): @thatanjan
- Twitter: @thatanjan
- Upwork: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
- Beginners guide to quantum computers
Videos might you might want to watch: