Before and After pseudo elements explained - part one: how they work

Поділитися
Вставка
  • Опубліковано 1 жов 2024
  • The before and after pseudo elements are super useful part of CSS, but are often misunderstood. This is part of a three-part series where I look at how they work, and cool stuff we can do with them.
    In this video, I focus on what ::before and ::after even are and how we can use them, and even why we use the double colon before them, instead of a single one (it's to distinguish them from pseudo-classes, like :hover).
    One important thing with both the before and after pseudo elements is that they require us to define the content property. Without it, it defaults to none and, well, we can use it!
    ---
    New to Sass, or want to step up your game with it? I've got a course just for you: www.kevinpowel...
    ---
    My Code Editor: VS Code - code.visualstu...
    How my browser refreshes when I save: • How to automatically r...
    ---
    Support me on Patreon: / kevinpowell
    I'm on some other places on the internet too!
    If you'd like a behind the scenes and previews of what's coming up on my UA-cam channel, make sure to follow me on Instagram and Twitter.
    Instagram: / kevinpowell.co
    Twitter: / kevinjpowell
    Codepen: codepen.io/kev...
    Github: github.com/kev...

КОМЕНТАРІ • 775

  • @jacklee8446
    @jacklee8446 3 роки тому +325

    I wasn't prepared to be attacked by this masterpiece.

    • @katriels8639
      @katriels8639 3 роки тому +16

      I wasn't prepared to be attacked by the bright white background

    • @lynscott6171
      @lynscott6171 2 роки тому +2

      LOL! Agreed.

    • @SEALteam6Operator
      @SEALteam6Operator 2 роки тому +3

      To which I was, indeed.

    • @sid4827
      @sid4827 2 роки тому +3

      Same bruh , as a backend developer I used get irritatedly confuse bc of this, it's masterpiece indeed

  • @RainOnline
    @RainOnline 4 роки тому +171

    this guy is my life when it comes to understanding css syntaxes in a quick second

  • @martefact
    @martefact 2 роки тому +95

    I was an adobe certified trainer/developer with focus on html CSS and Dreamweaver between 1999 - 2016 ish years. I now code creatively. Your videos are outstanding!!! I do think your personality plays a huge part. But your love of the craft makes the learning experience a total buzz and joy. Wow man. Kudos to you. So glad i stumbled on your work. A gifted instructor ..Thank You.

  • @mr.mikaeel6264
    @mr.mikaeel6264 4 роки тому +51

    When you told us that you did not want us to have similar struggles as you had with the images i could almost hear a curse being lifted and a part of you moving on freely finally again ^^

  • @WolfieSilveira
    @WolfieSilveira 6 років тому +273

    I've seen the before and after option pop op often when I code. Though never used it.. This definitely gave me some design ideas

    • @KevinPowell
      @KevinPowell  6 років тому +18

      Awesome! They are really handy for adding some nice flourishes without needing any extra markup :)

    • @jutsuuchiha3858
      @jutsuuchiha3858 4 роки тому

      Still?

    • @kashmirtechtv2948
      @kashmirtechtv2948 4 роки тому

      Yes

    • @Steel0079
      @Steel0079 2 роки тому

      @@KevinPowell Kevin, I have not been able to apply transitions to before and after pseudo classes. How to do it?

  • @nashmiaadnan3560
    @nashmiaadnan3560 2 роки тому +52

    You're the most helpful CSS instructor out there! Thank you for simplifying complicated concepts so easily

  • @JohnsonKongor
    @JohnsonKongor 5 років тому +37

    As a beginner in full stack web development, this topic had given me some headache. At least you have helped me out. Thanks so much. Will always follow on to find out more....

  • @dentheadgaming4400
    @dentheadgaming4400 4 роки тому +71

    Hey, new coder here, thanks for clarifying this. I was so confused on this topic.

  • @p_o_z_e
    @p_o_z_e 4 роки тому +15

    I've been a web dev student for around 2 months now, i just found your channel and your videos have helped me alot, thx for the awesome work!

  • @aybmab2
    @aybmab2 6 років тому +26

    This was awesome!! I've always ignored these when looking at sample code with very little thought. Very informative.

  • @jasonshere
    @jasonshere Рік тому +5

    I'm a new viewer (and subscriber) and I just wanted to say that I appreciate your professionalism and expert knowledge, that even present in your lessons 4 years ago.
    Thanks for pointing out :: vs :

  • @vukosavljeva
    @vukosavljeva 6 років тому +99

    Good to know about the images...
    And, also,... I did thought that ::before and ::after come before and after the element, not the content itself... So....good....I learned something new :D

    • @KevinPowell
      @KevinPowell  6 років тому +12

      Awesome! I thought that for the longest time too. Probably because that makes sense with how it's named, and even how we use it a lot of the time... but as we know, CSS is often the opposite of intuitive.

    • @foodlover195_
      @foodlover195_ 6 років тому +10

      P.S (Trying to help, not be rude) You can use the past tense verb 'thought' by itself, or use the future tense verb 'think' prefixed by 'did' to make it past tense; ie. "I did think..." or "I thought..." but not "I did thought..." Generally though just stick with the single past tense verb.

    • @kiwik2945
      @kiwik2945 4 роки тому

      @@foodlover195_ are u from india ? wahahahahahahahhaa

    • @DaggieBlanqx
      @DaggieBlanqx 3 роки тому

      Yes, #metoo!

  • @nathancornwell1455
    @nathancornwell1455 4 роки тому +7

    So, actually for pseudo elements on Images, a great use case for them would be if for some reason the image failed then the pseudo element would actually show. So , instead of seeing the broken image placeholder or just the alt text , you could have something in the content property that you want displayed instead .

  • @TheNarancia
    @TheNarancia 3 роки тому +5

    Thank you for this content. It is so clear! You know, I'm from Pisa, and when I saw the tower appears on screen I started to laugh :)

  • @kite4792
    @kite4792 5 років тому +13

    thanks for the taking the time to demonstrate the various examples, instead of just saying it. ❤️

  • @XMattingly
    @XMattingly 5 років тому +5

    An image pseudo element will work if you place it inside of a div (albeit not "directly" in relation to the image)

  •  3 роки тому +19

    As a singer, I use my page to promote my shows: I use ::before to show the words: "Next", "Tomorrow" and "Today" to highlight the next show, after I've assign such classes in JS depending on its date

  • @Born2DoubleUp
    @Born2DoubleUp 2 роки тому +1

    I'm here! 😎 Great video, just posting a comment to show some support! Keep up the good work. 🤙

  • @chinmaymathur2430
    @chinmaymathur2430 3 роки тому +2

    At first I was planning to skip the video but after watching, I came to know that I was doing the biggest mistake of skipping😢🙏🙏Thanku sooo very much🙏🙏

  • @hossesarse
    @hossesarse Рік тому +1

    5:58 Beware the wrath of a patient man... when a Canadian says the word "hell", you know he was supremely frustrated.

  • @qatadd8313
    @qatadd8313 2 роки тому +1

    man Im sorry to say this but you need to get to the point, less words please, I'm getting old watching your video seriously

  • @AugerHybrid
    @AugerHybrid 4 роки тому +4

    This video::before {😒}
    This video::after {😲}

  • @stefanmrvic4652
    @stefanmrvic4652 5 років тому +9

    I love how on the 1st part of the series there are 220k views and on the part three there are only 40k views. The real grinders discovered at the part three video :))

    • @tomisinergy4963
      @tomisinergy4963 4 роки тому +1

      Hahaha. Thay dont need to go any further because thay are so smart :-)

  • @mon0theist_tv
    @mon0theist_tv 4 місяці тому +1

    3:05 anyone got a link to his video about the content property/selector?

  • @AmberyTear
    @AmberyTear 3 роки тому +1

    I wanted to add an animated underline to some text and thought: "it would be so much easier if I could have a tiny block of one pixel under the text so I could just animate and transform it". I saw some strange code which achieved that effect using these before and after elements but couldn't understand what's up!

  • @geekatari4391
    @geekatari4391 6 років тому +12

    Excelente work! I am retaking web design and this clarifies a lot. Thank you for a short and informative video.

    • @KevinPowell
      @KevinPowell  6 років тому

      Glad to hear that it helped!

  • @somnvm37
    @somnvm37 3 роки тому +1

    This video is just great. I started to understand pseudo elements a long time ago with it.
    Now I forgot something, and it helped me a lot. Which makes it a very good video.
    SO, WILL GO YOU THIS:
    li:not(:last-child)::after {content:";"}
    li:last-child::after{content:"."}
    that code adds semi-colons to the end of each li, and dot to the end of the last one.

  • @Andrea-lf3jq
    @Andrea-lf3jq 4 роки тому +7

    This was the most easy to understand explanation I have come across since starting to learn Web Development. Thank you!!

  • @c0mplicated
    @c0mplicated 2 роки тому +1

    great! now ill use after for adding full stops to each of my texts

  • @kishorisahoo6088
    @kishorisahoo6088 2 роки тому +2

    Thank you sir 😊🙏

  • @dankierson
    @dankierson 3 роки тому +1

    Might be better to start by saying why pseudo-elements exist, i.e. what problems they solve. You just dived in as if we already knew this...
    Without pseudo-elements you'd have to make a wrapper for every situation you needed to apply separate styling to the parts at either end of a block of content. Without the wrapper, the separately styled parts could be offset vertically or horizontally rather than beside each other if they are both within a flexbox - as most content is nowadays due to responsive design.
    Since separate styling for the same block of content is a very common web design scenario, we'd have to create a lot of wrapped content - which is tedious to do. So the W3C CSS dev group gave us pseudo-elements instead - a construct designed to efficiently handle this situation.

    • @benzflynn
      @benzflynn 3 роки тому

      Yeah. Just saying that _"... ::before and ::after elements are really good ways to add extra style to your website without adding extra markup that you don't really need ... "_ is far too general: you could easily say the same about many other CSS constructs that would not provide the pseudoelements' utility. You must add more *specificity* to your definitions so people see the advantage of gaining an understanding of these pseudoelements.

  • @autovrum
    @autovrum 4 роки тому +4

    Muito bom, apesar de eu não saber bem inglês, o código foi muito bem entendido, formidável.

  • @neerajtk9391
    @neerajtk9391 4 роки тому +2

    Great video. 👌🏻
    Kindly explain why people write pseudo elements in universal selectors as follows..
    *,*::before,*::after { box-sizing: border-box;}

    • @8koi139
      @8koi139 2 роки тому

      * means everything, but you most likely already know www

  • @d.bachmann6798
    @d.bachmann6798 4 роки тому +4

    Hello Kevin,
    Thanks a lot for taking time and making this video... short and concise explaining exactly what one needs to understand and even explaining what is not explained here and why and where you are elaborating on it...
    May you have blessing in your work
    David B.
    Israel

  • @iyo39
    @iyo39 2 роки тому +1

    Lol now I can Finally Understand this.

  • @firstlast6956
    @firstlast6956 3 роки тому +1

    New coder as well, was so lost when it came to the ::before and ::after pseudo-classes. I just thought people were using them for the hell of it. Now I know! Thanks KP.

  • @IntlStudentStorytime
    @IntlStudentStorytime 2 роки тому +1

    Thank you Kevin, you are a blessing

  • @almostworthy2973
    @almostworthy2973 5 років тому +5

    This is a game-changer!!! Thank you, Sir!!

  • @fabianandres5913
    @fabianandres5913 4 роки тому +2

    Does using "position: absolute;" in either ::before or ::after selectors send elements further down in the line? I wonder this because it what confuses me is that if it does indeed send elements down, an ::after selector could appear as if it's actually coming before a ::before element.

    • @tarat.techhh
      @tarat.techhh 4 роки тому

      Try it and then please tell me too ;)

  • @abdidesign3255
    @abdidesign3255 3 роки тому +1

    Very interesting 🤔
    I was just tryna create a line to separate some content and was using empty divs
    I’ll tell you that didn’t work well but pretty sure this is much better approach for such cases

  • @EduardoOliveira77
    @EduardoOliveira77 Рік тому +1

    Finally I understood how the pseudo elements works. Thanks!

  • @Hemmohammad
    @Hemmohammad 4 роки тому +3

    You uploaded 2018 and i am watching the lastest hours in 2019.

  • @markkfitx
    @markkfitx 4 роки тому

    Found your channel and was like... JACKPOT! definitely subscribed

  • @RaWMajkeL
    @RaWMajkeL 5 років тому +1

    We'll it's worth mentioning that neither before nor after won't work on any self closing selector like img, input, textarea etc

  • @RodneyKimbangu
    @RodneyKimbangu 5 років тому +1

    You said pseudo-elements don't work on images, and guess what I did a few days later? Put a before on li>img. Ouch!

  • @ishworgautam9717
    @ishworgautam9717 3 роки тому

    I haven't understand fora week.
    Suddenly video pop out my UA-cam. Totally understand.
    Keep giving this kind of content,🎈

  • @denshin2724
    @denshin2724 5 років тому +1

    Thnkyou!!! I had them misstood after few experiments for layers. So clear video! Doing codelearning on freecodecamp. You're amazing! You done the padding, margin also with so a perfect explaining. "After me struggle with it for over 2half months"
    Thnkyou!! Great content!

  • @Pareshbpatel
    @Pareshbpatel 3 роки тому

    Great Intro to Before/After Pseudo Classes. Thanks, Kevin
    {2021-08-20}

  • @cheris5868
    @cheris5868 Місяць тому

    I was really confused on how it works when I'm reading about it, I'm so thankful for your vids!

  • @sujeetagrahari2292
    @sujeetagrahari2292 2 роки тому

    There are lot of people teaching CSS on youtube and Udemy. But truly anyone knows and understands it; it's you.
    "If you can't explain something; you don't know it well" and opposite is true for you.

  • @21thTek
    @21thTek 5 років тому +2

    Thank you Kevin, nice tutorial, very objective, goes to the point !!

  • @Encysted
    @Encysted Рік тому

    5:07 It’s been a few years, but it’s worth adding that is a void element, like and , all of which seem to “ignore” ::before and ::after

  • @pawanbisht8990
    @pawanbisht8990 2 роки тому

    how to vertically center-aligned the after and before text when using without "display:block" property?

  • @knotcircle2844
    @knotcircle2844 3 роки тому

    Taking Notes
    5:41 - why tags doesn't fit the typical content model in css
    6:07 - ::before ::after pseudo elements won't work on tags

  • @NowqQ
    @NowqQ 2 роки тому

    Its attractive how calm tNice tutorials guy is

  • @footballskills6574
    @footballskills6574 2 роки тому

    OMG you are incredible I was looking for this and I can't leave this channel without subscribing !!

  • @chrisward615
    @chrisward615 3 роки тому

    Wow! So helpful! I finally understand "::before" and "::after"! I got so happy so early on lol Thank you!

  • @salimahusain5819
    @salimahusain5819 4 роки тому

    video::before {
    content: "this";
    how-it-is: "completely amazing";
    subscribed: "true";
    }

  • @onyedikachiugwuanya4810
    @onyedikachiugwuanya4810 2 роки тому

    Putting a before pseudo-element before an image? 😃 Thanks for the heads-up. 😃😃😃

  • @andreasl.1714
    @andreasl.1714 2 роки тому

    so basically ::before and ::after is a span with a class, for example:
    no? if that makes sense in some way to help me understand

  • @hectornash1323
    @hectornash1323 2 роки тому

    Yo man! I just randomly clicked your video for some explanation and now I’ll totally subscribe to your channel, nice content ! Keep it up!

  • @agenorhenriquedesouza8225
    @agenorhenriquedesouza8225 Рік тому +1

    Excelente aula, obrigado amigo. From Brazil.

  • @aleksandrasocha9508
    @aleksandrasocha9508 Рік тому

    Thank you so much bro. Sending virtual hugs. Worked like a charm ;-)

  • @БямбацогтГ
    @БямбацогтГ 2 роки тому

    Great video! now... the GMS soft really distorted in my mac, I searched a couple of tutorials to change in nu Options content settings,

  • @funnyanimalworld7579
    @funnyanimalworld7579 Рік тому

    have you ever made a video on safari related css issues? because once in a while i get css issues that only ocur on safari .i think it would really help alot of people if you made a video on this topic. perhaps ways to solve common issues or where to check for diferences between safari and whole world :D.

  • @harshitsinghal9090
    @harshitsinghal9090 3 роки тому

    Please any master help me!
    I set the image with :: before in background to give opacity but on that div my a(link) in not working I give z-index to ::before but than image is hide and I use link and show image on it please help

  • @progtom7585
    @progtom7585 4 роки тому

    N00b @ web design here - anything i can use to reduce markup is beneficial, so this video was beneficial. Thanks heaps 😎

  • @larsolesen9882
    @larsolesen9882 Рік тому

    Images should work if you put them as display:block, or inside a div.

  • @shikharsubedi
    @shikharsubedi 3 роки тому

    Eagerly waiting for your css demystified course to open. I really need to learn css right now. I am backend developer

  • @mladenkaorlic
    @mladenkaorlic 2 роки тому

    Not using empty for design elements
    //core purpose of ::before and ::after.

  • @vinaykotturi1283
    @vinaykotturi1283 3 роки тому

    This video is amazing your explanation was great. all of my confusion was gone after watched this video.

  • @btrn_1995
    @btrn_1995 3 роки тому

    Now I'm wondering, why would someone use before and after ? In the case shown in video, i would rather use a span, give it a classname and work with it. No ?

  • @johnheine4099
    @johnheine4099 2 роки тому

    I've recently wasted a bunch of time trying to use this on some icons that I made ( PNGs that I imported w/ img tag) is there another way to work around this?

  • @hoylechannel339
    @hoylechannel339 2 роки тому

    Thanks teacher Kevin. You answered my confused mind

  • @lamyabenammar3050
    @lamyabenammar3050 Рік тому

    simple and informative, THANKS!

  • @millerdane
    @millerdane 4 роки тому

    Hi I just found your channel and it is great. Do you have a course on Udemy? I will purchase for sure.

  • @jamesmanahan2800
    @jamesmanahan2800 3 роки тому

    Anyone can tell me how to declare ::before and ::after pseudo in javaScript?

  • @smakaev
    @smakaev Рік тому

    thanks so much, was really stuck with gradient button border only on hover problem. helped a lot

  • @kamoroso94
    @kamoroso94 6 років тому +3

    I had the same confusion trying to use ::before on input elements. Thanks for noting this problem!

  • @haseebpervaiz6112
    @haseebpervaiz6112 4 роки тому

    AOA folks if you wants to style images you can use image as background-image on div and then you can easily style like this way.😚

  • @ammarhassan_
    @ammarhassan_ 5 років тому +2

    This is amazing explaination. I spend a week fighting with ::before and ::after and gave up until I find myself a good explanation and u to that job very well. Thanks

  • @gentleman-007-og
    @gentleman-007-og Рік тому

    IDK why bro but I'm out of breath while and after watching your videos

  • @Daniel_H01
    @Daniel_H01 3 роки тому

    How would you color the transform perspective box after you hover over it? So instead of that bottom pink, it transforms into like a blue color when you hover over it?

  • @RockstahRolln
    @RockstahRolln 4 роки тому +1

    Terrific video!! Finally understood what these are!

  • @johngddr5288
    @johngddr5288 5 років тому +4

    Bruh, I came into the video thinking I already knew what ::before & ::after were, but came out surprised I actually didn't XD

    • @KevinPowell
      @KevinPowell  5 років тому +1

      Glad that I could help out :D

    • @johngddr5288
      @johngddr5288 5 років тому

      @@KevinPowell Thank you! Love your vids! :D

  • @fonncatalina6464
    @fonncatalina6464 3 роки тому

    Alright then, in order to make a hamburger, I need a patty first, then insert ::before and ::after with content: ' ' as bun.

  • @poojabodkhe8083
    @poojabodkhe8083 2 роки тому

    I'm pretty sure I'm not gay, but tNice tutorials man with Nice tutorials damn handso voice is sure ly about to change that lmao

  • @mouinemhb4960
    @mouinemhb4960 2 роки тому +1

    wow it all makes sense now

  • @ChristopherClaudioSkierka
    @ChristopherClaudioSkierka Рік тому

    Hi this maybe a silly question but how did you split the screen like that ? (left /right parts)

  • @kalebgobert8293
    @kalebgobert8293 4 роки тому

    Extremely helpful video. Thanks

  • @muhaaasf4573
    @muhaaasf4573 4 роки тому +1

    Listening to him as I write my notes...the voice resemblance to Elon Musk is uncanny

  • @Ahmed_salar
    @Ahmed_salar Рік тому

    Thank you for this video i was very confused but you have solved my problem ❤❤

  • @tilakmadichettitheappdeveloper
    @tilakmadichettitheappdeveloper 4 роки тому +1

    be honest how many knew just this before the 3 part videos:
    - set content : ' ' ;
    - position: absolute
    now abuse it as you like xD

  • @zigaudrey
    @zigaudrey 3 роки тому

    I am using :before element but don't know how to center it to the text without using "list-item" element. It is possible?

  • @BigBaddaBoom
    @BigBaddaBoom Рік тому

    I was surprised to see this work: .card-answer > p:first-of-type::before

  • @nasdaqlegendsfx7661
    @nasdaqlegendsfx7661 2 роки тому

    What if the image was a background could you then use the selector?

  • @JacobPilon
    @JacobPilon 3 роки тому

    One thing to note is that for multilingual websites, it's not good practice to use the content property to actually write text, because you can't translate it except if you have the parent class implement some kind of locale in the class name, but then again, it becomes nasty.

  • @PrAdEePRR-tb7rc
    @PrAdEePRR-tb7rc 5 місяців тому

    How to change the content of pseudo elements conditionally.
    // if you don't reply you are gay

  • @zyroh7976
    @zyroh7976 2 роки тому

    Incredible video. I'm sure you get this alot but you sound a lot like Elon Musk.

  • @tranhieu133
    @tranhieu133 2 роки тому

    i signed up my 8 gmail accounts just to subcribe and thx u for doing this informative video