Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't target :before Pseudo selector in JSS

I am trying to target the after pseudo selector of an element on my page. I am trying out JSS for this project and am a huge fan so far, but still very new to it. I am having troubles selecting the :after selected with JSS. Is this possible because I thought it was when reading the docs.

This is my mark up:

<Link to="about" className={classes.link}>About</Link>

and my JSS looks like this:

link: {     position: 'relative',     fontFamily: fonts.family.primary     '&:before': {         content: ' ',         position: 'absolute',         bottom: 0,         left: 50,         width: '100%',         height: '1rem',         display: 'block',         background:styles.colors.white     } } 

if anyone who is familiar with JSS could help, that would be great!

like image 376
Brady Avatar asked Dec 05 '16 01:12

Brady


People also ask

What does the pseudo selectors allows us to do before and after?

CSS ::before and ::after pseudo-elements allow you to insert “content” before and after any non-replaced element (e.g. they work on a <div> but not an <input> ). This effectively allows you to show something on a web page that might not be present in the HTML content.

What is before pseudo selector in CSS?

In CSS, ::before creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.

What is the Speciality of pseudo selectors?

Pseudo-classes are CSS classes used to define the state of an element. They target elements that can't be targeted with combinators or simple selectors like id or class. They are used to select elements based on their attributes, states, and relative position.

Can you chain pseudo selectors?

You can still chain pseudoselectors. For example, a:focus:hover works just fine to apply styles only if the element is focused AND hovered. See this link for a demonstration. It works fine (in Chrome, at least); click in the demo area, press tab to focus the link, then hover it to see it become bolded.


2 Answers

What you did is right. Except for 2 things:

1) Syntax error: You're missing a comma after the entry fontFamily: fonts.family.primary

2) The content should be a string enclosed in double quotes which in turn should be enclosed in single quotes. So, an empty content would be content: '""',

So just try the following:

link: {     position: 'relative',     fontFamily: fonts.family.primary,     '&:before': {         content: '""',         position: 'absolute',         bottom: 0,         left: 50,         width: '100%',         height: '1rem',         display: 'block',         background:styles.colors.white     } } 
like image 95
Nishanth Matha Avatar answered Sep 20 '22 02:09

Nishanth Matha


Seems like a good place to add this too...

If you want to add a symbol (like an ") to the content: then this did the trick for me:

// symbol const $quoteSym = '"';  // jss class quote: {    color: 'white',    position: 'relative',    padding: '2rem',    '&:before': {       display: 'inline-block',       content: `'${$quoteSym}'`,    }, }, 

Looks a little hacky but the encapsulation method/order, with regards to what type of quotes you use, seems to make a difference.

like image 25
Harry Lincoln Avatar answered Sep 21 '22 02:09

Harry Lincoln