196

I'm making an HTML email signature with inline CSS (i.e. CSS in style attributes), and I am curious as to whether it's possible to use the :before and :after pseudo-elements.

If so, how would I implement something like this with inline CSS?

td { 
    text-align: justify;
}
td::after { 
    content: "";
    display: inline-block;
    width: 100%;
}
cassepipe
  • 371
  • 6
  • 16
Bah-Bee
  • 1,995
  • 2
  • 13
  • 8

10 Answers10

150

You can't specify inline styles for pseudo-elements.

This is because pseudo-elements, like pseudo-classes (see my answer to this other question), are defined in CSS using selectors as abstractions of the document tree that can't be expressed in HTML. An inline style attribute, on the other hand, is specified within HTML for a particular element.

Since inline styles can only occur in HTML, they will only apply to the HTML element that they're defined on, and not to any pseudo-elements it generates.

As an aside, the main difference between pseudo-elements and pseudo-classes in this aspect is that properties that are inherited by default will be inherited by :before and :after from the generating element, whereas pseudo-class styles just don't apply at all. In your case, for example, if you place text-align: justify in an inline style attribute for a td element, it will be inherited by td:after. The caveat is that you can't declare td:after with the inline style attribute; you must do it in the stylesheet.

Community
  • 1
  • 1
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
60

as mentioned above: its not possible to call a css pseudo-class / -element inline. what i now did, is: give your element a unique identifier, f.ex. an id or a unique class. and write a fitting <style> element

<style>#id29:before { content: "*";}</style>
<article id="id29">
  <!-- something -->
</article>

fugly, but what inline css isnt..?

honk31
  • 3,895
  • 3
  • 31
  • 30
  • 12
    that's not inline CSS. Inline CSS requires the style="" attribute to be passed to the individual HTML elements. Commonly required for sending CSS formatted to Gmail, which strips anything in – kez Mar 12 '14 at 11:44
  • 1
    I think this is the closest you can get to inline pseudo-elements. Better yet, use the new `scoped` styles and `:root` psuedo-class (this is so cool): `
    `.
    – Ben J Oct 27 '15 at 19:36
  • 3
    Correction: Use the `:scope` pseudo-class: `
    `
    – Ben J Oct 27 '15 at 19:44
  • 1
    This stuff is very new, probably not implemented, and will possibly change. It's in the current [HTML spec (`scope`d styles)](https://html.spec.whatwg.org/multipage/semantics.html#attr-style-scoped) and [CSS Spec (`:scope`)](http://www.w3.org/TR/selectors4/#the-scope-pseudo). I should have been more clear. – Ben J Jan 08 '16 at 10:32
  • Using tags is called internal or embedded CSS, NOT inline CSS. – James Anderson Jr. Apr 22 '20 at 12:22
31

You can use the data in inline

 <style>   
 td { text-align: justify; }
 td::after {
    content: attr(data-content);
    display: inline-block;
    width: 100%; }
</style>

<table><tr><td data-content="post"></td></tr></table>
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
Cevher
  • 421
  • 4
  • 4
  • 3
    This prints the `data-content` attribute as `content` for a pseudo-element. It has nothing to do with creating pseudo-elements with inline CSS. – Nils Kaspersson Apr 29 '14 at 11:05
  • 4
    I came here looking for how to apply pseudo selectors in inline CSS and this answer showed me another way to achieve the same thing. The content needed to be based on a large number of possible options created dynamically and so it wasn't practical to write heaps of separate CSS selectors for every possible result. – wunth Aug 24 '16 at 20:42
  • 11
    This actually is a very good answer for someone seeking to add dynamic content to an after content. Might not be that related to this issue, but this question is being displayed when searching for this solution via Google. – Aleks Apr 18 '17 at 11:18
  • 1
    See the [documentation](https://developer.mozilla.org/en-US/docs/Web/CSS/attr). – Sebastian Simon Jul 05 '20 at 18:34
  • This looks great. Unfortunately it does not seem to work for setting an url. – tcurdt Oct 27 '20 at 21:55
  • 1
    This also is not inline CSS. – TylerH May 04 '21 at 18:50
  • Very useful answer ! Not exactly what the OP ask for, but it works for content property of after/before pseudo element. – Chrysotribax Aug 10 '21 at 12:30
18

You can't create pseudo elements in inline css.

However, if you can create a pseudo element in a stylesheet, then there's a way to style it inline by setting an inline style to its parent element, and then using inherit keyword to style the pseudo element, like this:

<parent style="background-image:url(path/to/file); background-size:0px;"></parent>

<style> 
   parent:before{
      content:'';
      background-image:inherit;
   }
</style>

sometimes this can be handy.

Artem
  • 81
  • 6
Artem Mamaev
  • 335
  • 2
  • 6
9

No you cant target the pseudo-classes or pseudo-elements in inline-css as David Thomas said. For more details see this answer by BoltClock about Pseudo-classes

No. The style attribute only defines style properties for a given HTML element. Pseudo-classes are a member of the family of selectors, which don't occur in the attribute .....

We can also write use same for the pseudo-elements

No. The style attribute only defines style properties for a given HTML element. Pseudo-classes and pseudo-elements the are a member of the family of selectors, which don't occur in the attribute so you cant style them inline.

Community
  • 1
  • 1
GajendraSinghParihar
  • 9,051
  • 11
  • 36
  • 64
2

As mentioned before, you can't use inline elements for styling pseudo classes. Before and after pseudo classes are states of elements, not actual elements. You could only possibly use JavaScript for this.

Nesha Zoric
  • 6,218
  • 42
  • 34
1

Yes it's possible, just add inline styles for the element which you adding after or before, Example

 <style>
     .horizontalProgress::after { width: 45%; }
 </style> <!-- Change Value from here -->

 <div class="horizontalProgress"></div>
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
Manik Biradar
  • 1,294
  • 2
  • 9
  • 7
  • 29
    This is an inline stylesheet. Not inline css. – Esger Mar 14 '17 at 21:14
  • 4
    Other than the fact that this doesn't resolve the actual question, this code is wrong, `::after` and `::before` pseudo-elements need the `content: value` otherwise it defaults to `content:none` which results in basically nothing. – zer00ne Jul 16 '17 at 08:29
  • 1
    Using tags is called internal or embedded CSS, NOT inline CSS. – James Anderson Jr. Apr 22 '20 at 12:21
  • Welp. Despite the comments, this solution did what I wanted in the end. I didn't want to make a single entry in the stylesheet when everything else was embedded in the HTML so this works. – velkoon Aug 22 '22 at 22:15
1

If you have control over the HTML then you could add a real element instead of a pseudo one. ::before and ::after pseudo elements are rendered right after the open tag or right before the close tag. The inline equivalent for this css

td { text-align: justify; }
td::after { content: ""; display: inline-block; width: 100%; }

Would be something like this:

<table>
<tr>
<td style="text-align: justify;">
TD Content
<span class="inline_td_after" style="display: inline-block; width: 100%;"></span>
</td>
</tr>
</table>

Keep in mind; Your "real" before and after elements and anything with inline css will greatly increase the size of your pages and ignore page load optimizations that external css and pseudo elements make possible.

cassepipe
  • 371
  • 6
  • 16
txyoji
  • 6,680
  • 1
  • 44
  • 46
0

you can use

parent.style.setProperty("--padding-top", (height*100/width).toFixed(2)+"%");

in css

el:after{
  ....
  padding-top:var(--padding-top, 0px);
}
surinder singh
  • 1,463
  • 13
  • 12
0

EDITED: If you have access to the stylesheet, you can pass the variable values inline and then, in your stylesheet, use the inherit value for the pseudo-element property you want to manipulate:

HTML

<div style="color: whitesmoke;">
</div>

CSS

div::before {
  content: '';
  color: inherit;
}

Useful for background images for example.

itsgus.dev
  • 87
  • 5
  • And where are you going to write that `div::before` style? – TylerH May 04 '21 at 18:52
  • (Hint: this answer is not useful because you can't apply the CSS from it when writing *inline styles*, which is what the question is about). – TylerH May 04 '21 at 20:01
  • 1
    I found this answer helpful since I'm using css before & after and wanted to set the colour dynamically which I was able to do so by applying this approach – headwinds May 15 '21 at 19:09