How do I add / insert a before or after pseudo element into Chrome's Inspector?
Asked Answered
M

2

103

I can add a regular style rule via the + sign (New Style Rule) but I can't add one under the "Pseudo ::before Element" or "Pseudo ::after Element" sections of the Style Inspector. If I try to add the ::before or ::after element into the HTML via "Edit as HTML", it comes out as text. My workaround is to add <span class="pseudo_before"></span> and then style that. Am I missing something?

Misty answered 25/2, 2014 at 19:15 Comment(2)
You can use the plus sign, then while the class or element is highlighted, add the pseudo element by editing the class or element name. Hitting the right arrow key will put you at the end.Lovell
Is there a way to do this in Firebug?Establishmentarian
J
152

This is the easiest way and the way I do it:

  1. Inspect the element you want to add the ::before or ::after to by right clicking it and going to "Inspect Element".

  2. Now in the Developer Tools Console, click on the plus sign icon aka. "New Style Rule". See the image below, the plus sign is next to the "Toggle Element State" button.

    enter image description here

  3. Next, you will be able to edit the selector so add ::before / ::after to it:

    enter image description here

  4. Now edit the content to whatever you like, i.e.

Like so:

.grp-row::before {       
   content: '> '; 
}

That's all there is to it :)

Janessa answered 3/11, 2014 at 16:59 Comment(4)
What if your element doesn't have a class? Chrome will just use a standard selector like p which would affect all elements instead of the element you want.Marja
Following these exact instructions produces no real-time updates in latest chrome.Uriel
Worked for me in Chrome 72, as long as I put in a content property, even if blank like content: ''Hankering
@Vic, you will just need to add a class in the LHS of the inspector. Ie, right-click on the p tag, choose Edit as HTML, and then add class="whatever"Codycoe
M
3

Open the stylesheet you want in the inspector by holding Ctrl and clicking on a style property from that stylesheet (on Windows, click here for Mac). Then you can add whatever you want and it updates in realtime.

For example:

p::before {
    content:'TEST';
}

This will add the word 'TEST' as a prefix to any <p> tags it finds. Check it out on MDN

You can even save the stylesheet so you don't have to do it twice. Right-click inside the stylesheet and hit 'Save as'.

Mcgannon answered 25/2, 2014 at 23:34 Comment(5)
The Ctrl clicking doesn't work for me (maybe because I'm on Mac?) but simply clicking on the link to the stylesheet to the right of a style declaration opens the stylesheet in the Sources tab and, yes, it is editable. Little did I know! Thanks!Misty
Hmmm. When I add a :before or :after declaration to the source sheet it doesn't apply. It seems I still need that little ::before or ::after element in the (inspector) markup. No?Misty
You can also get to it by opening the editor, then going to the Resources tab and selecting the 'Frames' folder at the top of the menu on the left. You can then click down through the file structure of your site and edit the CSS there.Mcgannon
Aaaah, it was the double :: in the style that got me! Thanks!Misty
yeah, it's a bit misleading ehMcgannon

© 2022 - 2024 — McMap. All rights reserved.