Learning NextJs - Day 4
You can add css to your pages components using styled-jsx and since the creators of this library are the same than next you can use it without write a single line of configuration.
You will need the special style jsx component inside of your pages.
1 | function Home({ users }) { |
This page will show a list of users and the name of each user will be blue since we are giving that color to the p element.
One advantage of style jsx is that our css is isolated from the rest of the world, in this case the world means other pages/components. So other p elements will remaing with the default color.
Global Styles
But what about if we want to all of the p elements have the same color? Easy, style jsx give you an special global prop.
1 | function Home({ users }) { |
Dynamic Styles
Let’s suppose your manager goes crazy and tell you that your users pages needs
a button for every user and if the ID of the user is greater than 5 the button background must be red otherwise green.
First you create a custom button component and you accept and ID prop and according to that writing a simple ternary you can make the crazy request.
1 | function CustomButton({ children, id }) { |
Now in your home page just import the component
1 | import CustomButtom from "./../components/CustomButtom"; |
And now you have dynamic styles using the values of props.