Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How add style for child elements with react-jss component?

Tags:

reactjs

I'm using react-jss component. Here's my sample code:

<ul>
  <li className={classNames(classes.listStyleNone)}>Item 1</li>
  <li className={classNames(classes.listStyleNone)}>Item 2</li>
  <li className={classNames(classes.listStyleNone)}>Item 3</li>
</ul>

and my code in style.js is:

const styles = theme => ({
    listStyleNone: {
        listStyle: 'none',
    },
})

Is there a way I add just a class to ul element and style it's children elements with that in react-jss component? like this:

.listStyleNone > li {list-style: none;}
like image 965
Atousa Darabi Avatar asked Sep 18 '18 08:09

Atousa Darabi


People also ask

How do you pass style to child component React?

To pass styles to child component and use it as scoped style in Vue. js, we can use the deep selector. to add the deep select with >>> , /deep/ or ::v-deep . They all select the b select in a .

Does styled components use JSS?

Styled Components is on version 5 and JSS is on version 10. Most of the significant bugs and limitations have been worked out by now. As far as the code goes, pretty much anything you desire to accomplish can be accomplished with either library.


Video Answer


2 Answers

Thanks all, However for list-style it's working but for other css like padding: 10px; it's not. So here's a better way:

<ul className={classNames(classes.listStyleNone)}>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>

const styles = theme => ({
    listStyleNone: {
        '& li':{
            listStyle: 'none';
        }
    },
})
like image 166
Atousa Darabi Avatar answered Nov 09 '22 22:11

Atousa Darabi


Please see following code snippet. I hope it will help you clearly.

parent: { textAlign: 'center', '& div': { padding: 15, '&:hover': { color: 'yellow', } } }

enter image description here

Here is the codepen link. If child elements of parent, will have a padding with 15px. If you hover those div tags, text color changes.

like image 25
Oni Avatar answered Nov 09 '22 23:11

Oni