::before / ::after | CSS-Tricks (2024)

Psst! Create a DigitalOcean account and get $200 in free credit for cloud-based hosting and services.

  1. Ethan Thatcher

    Permalink to comment#

    Again, what do the ‘Before’ and ‘After’ elements do?

    Reply

    • Subin Varghese

      Permalink to comment#

      Check the “More Resources” section. The first link itself is “A whole bunch of cool stuff they can do.”

    • Vin

      Permalink to comment#

      I know I’m about 3 years late..but, nonetheless –

      They just add content before or after specific elements.

      Say I want a question mark ( ? ) added after every element.

      I would style it like this:

      <style>p::after{content: "?"}</style><p>Turn down for what</p>

      Would give the following output:

      Turn down for what?

      Simple as that! ;)

      “But why bother using them?” You may ask.
      Apart from the obvious effort saving reasons, these are used to implement some cool css tricks!

      Check out the ribbon at the bottom here: http://cybernext.in

      Pure CSS my friend! The ribbon’s folded corners make use of ::before and ::after pseudo elements.

      Check this out for more : https://css-tricks.com/snippets/css/ribbon/

    • Dave

      Permalink to comment#

      I think you already knew the significance of :before & :after.

  2. Druid of Lûhn

    Permalink to comment#

    This is something that interests me (:before and :after).
    But you’ve still kept the clipped sprite from the video.

    Reply

    • Arkar Htun

      Permalink to comment#

      Hi teacher
      Can I write html tags within before and after?

  3. kevin

    Permalink to comment#

    android support?

    Reply

  4. Clark

    Permalink to comment#

    This may be a silly question, but I have some confusion over the use of 2 colons to define a pseudo element.

    i.e.,
    div:after {}

    vs.
    div::after {}

    I have seen many resources on the Internet (and articles on this site) that use a single colon, but it seems that some “newer” references use double colons. In real-world use (at least as far IE is concerned, IE8 specifically), the double colon is not compatible, but the single colon usage works in IE8 and all newer browsers (at least the ones I’ve tested). So, if a single colon works, why would I ever use double colons?

    So, can someone explain the reason behind the use of the double colons?

    Reply

    • Clark

      Permalink to comment#

      Oops… I guess I should have completely read the article above which summarizes the fact that IE8 doesn’t support double colon usage, and recommended to use single colons for now… that basically answers my question. Thanks.

    • Edis

      Permalink to comment#

      It is not that silly at all. So, in all CSS versions up to and including CSS 2.1 there was no way to differentiate what is pseudo-class and what is pseudo-element.

      I am still a beginner web developer, and although I now know a lot more than when I started, it is still sometimes confusing to properly differentiate what is pseudo-element and what is pseudo-class. The problem I believe many of us still share.

      It is so much easier to to remember that two colon (::) syntax means pseudo-element, and that one colon (:) syntax means pseudo-class. No more guess work involved.

    • Thiago Frias

      Permalink to comment#

      Is for semantic and better understand of the code, double colons is for pseudo element like ::first-line, ::-vendor-scrollbar and single colon is for pseudo class like :hover,** :active**

    • Don

      Permalink to comment#

      It is. Here is a CodePen showing it.

    • Edis

      Permalink to comment#

      In this particular case, no it is not an example of ::before pseudo-element. Let me explain it:

      Pseudo-element generates a virtual element as a last child element inside targeted element. In case you did not know every letter in an element is in a sort of a virtual element called line box. In this case the <div> element was empty (no text, no line box), and when the ::after pseudo-element was created his content “hi” string (generated by content property), was the only thing inside that <div> element, and that is the little illusion that is confusing you.

      For example, if you add any text in that <div> element, that “hi” string will end up as the last content in that element.

      I hope that helps to clarify things for you, at least a little bit.

    • Firoz

      Permalink to comment#

      I want to learnt after and before.
      how to learnt about the after and before ?
      please I want to solution

    • Firoz

      Permalink to comment#

      after before

  5. NicC

    Permalink to comment#

    Would anyone use :before or :after in the tag to generate a page header & footer? Is this semantically correct to apply a pseudo element to the tag?

    Example:

    html:after { content: “© 2012. All Rights Reserved.”; background-color: transparent; font-size:x-small; font-family: Arial, Helvetica, sans-serif; margin-left: 40%; text-align: center; padding-bottom: 20px;}

    Reply

    • Edis

      Permalink to comment#

      I do not think so, because content of the pseudo-element is not in the DOM (can not even be selected), therefore it only exists visually for the users of visual browsers, and that excludes screen readers and web crawlers which is not a good practice.

      The CSS :after pseudo-element matches a virtual last child of the selected element. Typically used to add cosmetic content to an element, by using the content CSS property. This element is inline by default.

      The emphasis is mine. Source: MDN Docs

  6. Kyle

    Permalink to comment#

    Aren’t the HTML examples backwards for ::after and ::before?

    Reply

  7. RZK

    Permalink to comment#

    hi
    — Rest of stuff in side div —

    must be change to
    — Rest of stuff in side div —
    hi

    Reply

  8. Makis Tracend

    Permalink to comment#

    Today I used this information to create a common loading overlay for web apps:
    https://gist.github.com/tracend/8553152

    Thanks!

    Reply

  9. Lakshya

    Permalink to comment#

    That’s cool but what is the > difference between them

    Reply

  10. Jayanta Boruah

    Permalink to comment#

    Thanks for explaining the difference between : and : :

    Reply

  11. Mohammad Heydari

    Permalink to comment#

    Simple and Perfect

  12. shishir

    Permalink to comment#

    I dont think it works in android default browzer :(..Any solution ?

    Reply

  13. Seldom

    Permalink to comment#

    Thanks for the clarification about using double-colons versus not. I’d read elsewhere not to use them, which frustrated me a bit, as I thought they were a helpful in the code visually speaking.

    I see why people were saying not to use them (for the compatibility reason with older IE-versions), but my designing-with-legacy-IE-versions-in-mind days are over, so it’s double-colons on out from here!

    Reply

    • Firoz

      Permalink to comment#

      I want to learnt after and before.
      how to learnt about the after and before ?
      please I want to solution

  14. Salman

    Permalink to comment#

    HI every one in need to give background. But the main issue is want to that the backgrond should apply from left to right and as its come to right site the hight of background should decrease as it come to right side ……… how can i apply it in CSS3

    Reply

  15. AJ Kandy

    Permalink to comment#

    Resurrecting this thread – is it possible to apply a pseudo-element to :after content? For instance, if you wanted to apply :first-letter to it in a neat CSS way, vs. having to hack it with some sort of targeted JS string parsing.

    Reply

  16. Deangelis

    Permalink to comment#

    Reply

  17. srinivas

    Permalink to comment#

    i use before tag to paragraph. i use image in that before tag. how can i fix the hight and width of that image.

    Reply

  18. Ionut Botizan

    Permalink to comment#

    You forgot the attr function which allows you to do stuff like:

    [data-tooltip]::before { content: attr(data-tooltip); /* ... */}

    Reply

  19. Randall Glass

    Permalink to comment#

    The content image can be resized.

    <!DOCTYPE html><html><head><style>.image:before {display:inline-block;content:url("MyImage.png"); -webkit-transform: scale(0.5); -moz-transform: scale(0.5);}.image {width:300px;height:300px;}</style></head><body><div class="image"></div></body></html>

    Reply

  20. Tobitron

    Permalink to comment#

    So what is the benefit of using ::after/::before vs using JQuery. I’m having trouble imagining a scenario when I wouldn’t just do this in jQuery.

    Reply

    • Simon

      Permalink to comment#

      Besides the obvious reasons that you’re using “native” implementations (giving you a better understanding of what you’re actually doing), using a CSS pseudo element is faster than using JavaScript. DOM manipulation is very expensive.

    • Tobitron

      Permalink to comment#

      Great answer, thanks!

  21. Amitoj Singh

    Permalink to comment#

    Great. Wanted to learn this “after before” thing since long but learned today only when stucked into something that needs this knowledge to proceed. Wanted to code heading decorations on two projects of mine ( lotterywale and punjabstatelottery)

    I hope I will be able to code that successfully. Will ask for help here otherwise.

    Thanks much,
    Amitoj

    Reply

  22. JJ

    Permalink to comment#

    Is it possible to use the psuedo elements within a class?

    e.g.

    .className li::before {

    }

    Thanks

    Reply

  23. Sarbjit Singh

    Permalink to comment#

    What is the use of after before element in CSS selector?

    Reply

  24. lechant

    Permalink to comment#

    Im sorry but whats the difference between inserting the css properties into the class with the after selector (.example:after{position:absolute;}) and just inserting it into the class itself (.example{position:absolute;}) , i dont quite get the concept behind this.

    Reply

    • Geoff Graham

      Permalink to comment#

      It is pretty confusing, but there is indeed a difference. Both ::before and ::after are called pseudo-elements because they elements in and of themselves that are distinct but directly related to the element they are attached to.

      I other words, .my-class::after is very much an element like .my-class and can be styled apart from it. Chris has an oldie but goodie on how they work and some interesting things you can do with them: https://css-tricks.com/pseudo-element-roundup/

  25. kalpna123

    Permalink to comment#

    it’s not working with tag

    does it work with it?

    Reply

    • Geoff Graham

      Permalink to comment#

      Yes, it does. Something like this:

      a::before {
      content: "";
      /* other properties */
      }

  26. Mehmet Yıldırım

    Permalink to comment#

    *,
    *:before,
    *:after {

    }

    what does it mean ?

    Reply

    • Chris Coyier

      Permalink to comment#

      It’s selecting the before/after pseudo-elements for ALL elements. The * means “any tag”.

  27. Carlos

    Permalink to comment#

    Where’s ::selection ?

    Reply

    • Geoff Graham

      Permalink to comment#

      ::selection

  28. BabakFP

    Permalink to comment#

    This doesn’t work

    body :not(i, [class=-icon], [class*=dashicons], ::before, ::after)
    font-family: $font-family !important

    …but this works
    body :not(i, [class=-icon], [class=dashicons]),
    body:not(
    )::before,
    body:not(*)::after
    font-family: $font-family !important

    Please add it to your article because there is no article about this issue.

    Reply

Leave a Reply

::before / ::after | CSS-Tricks (2024)
Top Articles
Latest Posts
Article information

Author: Terrell Hackett

Last Updated:

Views: 6638

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.