Using the :after CSS pseudo-element without inserting content

zigojacko

Is it possible to use the :after CSS pseudo-element to offset alignment without actually inserting anything in content: "". It doesn't seem to render unless content is specified so just wondered whether this is possible or if there are any known workarounds.

As an example:-

.nav-primary li.level0 a:after {
    content: "";
    padding-right: 1px;
    margin-left: -1px;
}

As you can see from the example, simply changing the colour of the content would not work in this instance as would still consume space affecting the offset.

Jukka K. Korpela

It is possible to use an :after pseudo-element with the content property set to the empty string "", but not without setting it all (so that it has its initial value none, which means that the pseudo-element is not generated at all).

The reason why you do not see any effect is that your settings effectively cancel each other out. You set a negative left margin, shifting the element leftwards, but you set an equal amount of padding. The pseudo-element itself is empty and thus invisible, so all that matters is the space that it occupies.

This can be illustrated by drawing an outline. I’m using the value 10px instead of 1px for clarity:

.nav-primary li.level0 a:after {
    content: "";
    padding-right: 10px;
    margin-left: -10px;
    outline: solid red;
}
<div class=nav-primary>
  <ul>
     <li class=level0><a href=foo>bar</a>xxx
     <li><a href=foo>bar</a>xxx
  </ul>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Is there a way to use SVG as content in a pseudo element :before or :after

From Dev

css content without pseudo-class

From Dev

Using :after pseudo element after anchor element - browser differences

From Dev

Modify CSS of the :after pseudo-element using jQuery

From Dev

Select the first CSS element using a pseudo class

From Dev

Image overlay using :after pseudo-element

From Dev

CSS Transition on the 'content' property of :before pseudo element

From Dev

Cannot rotate :after pseudo-element using CSS in IE8

From Dev

Insert &times; using CSS pseudo element

From Dev

CSS, using display:table with before pseudo element

From Dev

LESS CSS selector combining not and :after pseudo element

From Dev

Is it possible to insert an HTML tag from pseudo-element content using CSS?

From Dev

CSS pseudo after/before using content or background for img?

From Dev

CSS centering content add by pseudo-element after

From Dev

Generate CSS Pseudo content for before/after dynamically using Angularjs

From Dev

Creating a shape using a :before pseudo element in CSS

From Dev

CSS pseudo-element input-placeholder::after to show with or without value in text box

From Dev

Adding html content after an element using css

From Dev

CSS - Using a pseudo-element for an image mask

From Dev

Modify CSS of the :after pseudo-element using jQuery

From Dev

Pseudo element content with JQuery

From Dev

CSS Transition on the 'content' property of :before pseudo element

From Dev

Is it possible to insert an HTML tag from pseudo-element content using CSS?

From Dev

CSS: <a> tags disabled after using pseudo elements

From Dev

CSS ::after pseudo element clear not working

From Dev

Move content added with pseudo-element:after

From Dev

Modify the css of a pseudo element using JQuery

From Dev

Pseudo element in after starting from center - CSS

From Dev

How to remove element after pseudo-content with jQuery

Related Related

  1. 1

    Is there a way to use SVG as content in a pseudo element :before or :after

  2. 2

    css content without pseudo-class

  3. 3

    Using :after pseudo element after anchor element - browser differences

  4. 4

    Modify CSS of the :after pseudo-element using jQuery

  5. 5

    Select the first CSS element using a pseudo class

  6. 6

    Image overlay using :after pseudo-element

  7. 7

    CSS Transition on the 'content' property of :before pseudo element

  8. 8

    Cannot rotate :after pseudo-element using CSS in IE8

  9. 9

    Insert &times; using CSS pseudo element

  10. 10

    CSS, using display:table with before pseudo element

  11. 11

    LESS CSS selector combining not and :after pseudo element

  12. 12

    Is it possible to insert an HTML tag from pseudo-element content using CSS?

  13. 13

    CSS pseudo after/before using content or background for img?

  14. 14

    CSS centering content add by pseudo-element after

  15. 15

    Generate CSS Pseudo content for before/after dynamically using Angularjs

  16. 16

    Creating a shape using a :before pseudo element in CSS

  17. 17

    CSS pseudo-element input-placeholder::after to show with or without value in text box

  18. 18

    Adding html content after an element using css

  19. 19

    CSS - Using a pseudo-element for an image mask

  20. 20

    Modify CSS of the :after pseudo-element using jQuery

  21. 21

    Pseudo element content with JQuery

  22. 22

    CSS Transition on the 'content' property of :before pseudo element

  23. 23

    Is it possible to insert an HTML tag from pseudo-element content using CSS?

  24. 24

    CSS: <a> tags disabled after using pseudo elements

  25. 25

    CSS ::after pseudo element clear not working

  26. 26

    Move content added with pseudo-element:after

  27. 27

    Modify the css of a pseudo element using JQuery

  28. 28

    Pseudo element in after starting from center - CSS

  29. 29

    How to remove element after pseudo-content with jQuery

HotTag

Archive