Write less code with these 5 CSS tips

Sdílet
Vložit
  • čas přidán 17. 05. 2024
  • Looking to step up your CSS? I have free and premium courses 👉 kevinpowell.co/courses?...
    🔗 Links
    ✅ Code from this video: codepen.io/kevinpowell/pen/rN...
    ⌚ Timestamps
    00:00 - Introduction
    00:43 - The problem with overly-generic rules
    01:15 - Using [class] and :not[class]
    02:50 - Selecting ranges of content
    05:33 - Selecting all the elements except the one I’m interactive with
    09:02 - Selecting the elements before or after a specific element
    12:15 - Using nesting to style an element depending on context
    #css
    --
    Come hang out with other dev's in my Discord Community
    💬 / discord
    Keep up to date with everything I'm up to
    ✉ www.kevinpowell.co/newsletter
    Come hang out with me live every Monday on Twitch!
    📺 / kevinpowellcss
    ---
    Help support my channel
    👨‍🎓 Get a course: www.kevinpowell.co/courses
    👕 Buy a shirt: teespring.com/stores/making-t...
    💖 Support me on Patreon: / kevinpowell
    ---
    My editor: VS Code - code.visualstudio.com/
    ---
    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 CZcams channel, make sure to follow me on Instagram and Twitter.
    Twitter: / kevinjpowell
    Codepen: codepen.io/kevinpowell/
    Github: github.com/kevin-powell
    ---
    And whatever you do, don't forget to keep on making your corner of the internet just a little bit more awesome!

Komentáře • 95

  • @clevermissfox
    @clevermissfox Před měsícem +68

    Love that zindex shirt!!!

    • @kdvr60
      @kdvr60 Před měsícem +3

      How is it that video shows its been uploaded 1 min ago but your comment is 10h old?

    • @Syntax-Savvy
      @Syntax-Savvy Před měsícem +4

      ⁠channel members can get content early before public release.

    • @YellowBriefs
      @YellowBriefs Před měsícem +2

      We've all done it 😂

    • @LorenzoGasperoni
      @LorenzoGasperoni Před 22 dny +1

      Please tell us where to find it

  • @thornikejangveladze8240
    @thornikejangveladze8240 Před měsícem +28

    Nice use case for changing something on preceding siblings could be star rating module. Like when you hover on 4th star fill all the preceding stars.

    • @itsviney
      @itsviney Před měsícem +2

      Haha. Just scrolled down to suggest the same. 👍

  • @JacksonGreenMagic
    @JacksonGreenMagic Před měsícem +16

    8:35 for dimming all the images except the one you’re hovering on, I think you can do the first approach with one selector and without using :has. You could just make it .showcase-list:hover img:not(:hover). Pretty similar to the second approach, but still without :has since some people don’t want to use it yet.

    • @arnaudbrowet7872
      @arnaudbrowet7872 Před měsícem +5

      I thought exactly that :) nicely reduce the flickering too

  • @AndyFairlie
    @AndyFairlie Před měsícem +11

    That first tip for lists with and without classes is genius

    • @markshall94
      @markshall94 Před měsícem +2

      I actually think it's really bad. What if you just happen to need to add a class on there for something unrelated to bullet points? It would instantly fail. I'd rather just have a `.no-bullets` utility class, or something like that and just apply it in the markup. Yes you're writing slightly more code, but it's more verbose, less confusing and you know straight away what it's doing.
      I think it's a bad use of the attribute selector.

  • @aspizuwastaken
    @aspizuwastaken Před měsícem +22

    .showcase-list:hover img:not(:hover) { opacity: .5; } would have been also worked I think for a single rule approach.

    • @visuellegedanken
      @visuellegedanken Před měsícem

      Yes! Came here to say that, but checked other comments first. 😄

  • @moy2010
    @moy2010 Před měsícem +5

    Hey Kevin! I've been following your channel for years and I totally love your content! Recently I experimented with CSS-only performance improvements for rendering large lists, and found out that content-visibility: auto, contain: content and content-intrinsic-size provided the best performance improvements (in that order).
    I'm planning to write a blog post around it in the future, vut thought that it would also be a good subject for you to cover in your channel.

    • @Le_Sylvestre
      @Le_Sylvestre Před měsícem +2

      Don't forget to inform us when you finish writing your blog post, it would be nice to read it.

    • @moy2010
      @moy2010 Před měsícem

      @@Le_Sylvestre Sure, will do!

  • @xldoesit
    @xldoesit Před měsícem +2

    the image grid nth child thing blew my mind

  • @andru2625
    @andru2625 Před měsícem +1

    As always, another great video! Thanks a lot for the info!

  • @madiko
    @madiko Před 7 dny

    Kudos to your T-Shirt designer. This is hillarious! Love it.

  • @lyrebird712
    @lyrebird712 Před 8 dny

    I had no idea vanilla CSS had nested rules! This feature was one of the reasons I had finally learned SASS for my own projects. I have another tool to use now in my other projects where I can't convince the team to use (read: learn) SASS.

  • @funisallowed6046
    @funisallowed6046 Před měsícem +1

    I am working with css more than 2 years now but I wasn't aware that css is this much cool

  • @dzsuvi
    @dzsuvi Před měsícem

    Good ones. Learning something every day. Thanks Kevin!

  • @rikithikatakizu2
    @rikithikatakizu2 Před 27 dny +1

    Changing styles for preceding siblings could be useful while creating range date picker

  • @vinodkumarpujar1
    @vinodkumarpujar1 Před měsícem +1

    Thank you for knowledge sharing Kevin.

  • @YacineBougera
    @YacineBougera Před měsícem

    I watched all your last year videos ...I literally learner a lot...thanks

  • @potrosanjuan
    @potrosanjuan Před měsícem

    Master class as always, thanks!!!!!

  • @bobmarteal
    @bobmarteal Před měsícem +2

    Dude... had to pause with the ul[class] and just say you can end it here. Dag!

  • @todd.mitchell
    @todd.mitchell Před měsícem

    Brilliant! Thank you so much.

  • @solvedfyi
    @solvedfyi Před měsícem

    This is exactly the type of video I want. Do these

  • @5pointsaday
    @5pointsaday Před měsícem

    Thank you Kevin!

  • @george003
    @george003 Před měsícem

    nice another learning video , great Kevin, thank you, I'll take notes, when will you make more CSSBattle videos?

  • @Nusamaxa
    @Nusamaxa Před 29 dny

    I believe this video is one of the most helpful videos you ever did 😊✌

  • @Kogemate
    @Kogemate Před 13 dny

    11:18 An idea: on an image gallery (or a list), when there is a function to change the position of a image (with the precedent or following).

  • @Stephinmaju
    @Stephinmaju Před měsícem

    Kevin powell ❌ Kevin css well✅

  • @BenjiFriedman
    @BenjiFriedman Před měsícem

    Interesting CSS moves as always 👍

  • @clevermissfox
    @clevermissfox Před měsícem +2

    Omg I literally was watching the recent video on select ranges an hour ago! Was trying to select nth-child 3-5. It kept overwriting each other but I think it’s because I was using :is() something like :is( :nth-child(n - 5), :nth-child(n + 3) )
    Will have to try again and chain them together instead

  • @OCEMTechZone
    @OCEMTechZone Před měsícem

    Great Sir 🎉

  • @Simulacra001
    @Simulacra001 Před 9 dny

    It would create a nice effect if the images both after and before were of stepped opacity. Basically creating a fading out effect from the hovered image getting darker the further out it gets.

  • @realscout1775
    @realscout1775 Před měsícem

    11:28 you could make it so that all the images that aren’t being hovered on fade out and the image that is being hovered on increase in scale as to highlight it.

  •  Před měsícem

    Awesome!!!

  • @Krilllind
    @Krilllind Před měsícem

    For the inverse class example, could you not use a selector like `:has(.inverse) .button { ... }` instead?

  • @SeanGrimLink
    @SeanGrimLink Před měsícem +3

    I prefer using [role="list"] or the nav element to unset the list styles, this keeps semantics.
    Your class solution feels like a less ideal solution, this require a bit more style to keep semantics.

    • @mendosis
      @mendosis Před měsícem +2

      Actually, relying on presence of class attribute may be a horrible idea if your markup is going to be updated one day by anyone (including yourself). Someone may want to add a class name for any reason and unexpectedly cause a visual bug.

    • @KevinPowell
      @KevinPowell  Před měsícem

      I've gone back and forth on that, but lately have stopped, and only use it if I feel like I *really* need the item to be a list.
      First, it only removed the semantics for VoiceOver with Safari, and this was done on purpose as many screen reader users prefer when not everything is announced as a list... but VoiceOver also recently-ish changed this too and made it a bit more contextual, like a list in a nav will be announced as a list, even with the list-style: none on it now.

  • @cintron3d
    @cintron3d Před měsícem

    We've been using nesting syntax like that in styled components for years

  • @hoshi411
    @hoshi411 Před měsícem

    CSS with web-components in shadowDOM makes it all so easy 🤯

    • @KevinPowell
      @KevinPowell  Před měsícem +1

      Some of the limitations of the shadow DOM drive me nuts though

    • @MattDunlapCO
      @MattDunlapCO Před měsícem

      We're in the middle of converting our company's web component library to headless components (like tanstack) mainly because of how hard styling is across shadow/light dom with slots/parts. Now we know.

  • @TheColonThree
    @TheColonThree Před měsícem

    May I ask why you'd rather nest elements in a list instead of just placing them in the section/nav element directly? I've seen many people do it, but don't understand the exact reason.

  •  Před měsícem

    You could make the image you hover larger, and the images on both sides slightly larger, similar to the MacOS dock...

  • @kirillvoloshin2065
    @kirillvoloshin2065 Před měsícem +1

    this is insane)
    I feel like such CSS solutions need an obligatory comment, because start to feel like regexes
    p.s. saying as a frontend developer

  • @ayushgogna9732
    @ayushgogna9732 Před měsícem

    I am so used to using tailwindcss everyday seeing you write vanilla css looks so difficult i still cant name classes😖

  • @manishkarki8119
    @manishkarki8119 Před měsícem

    LOVE FROM NEPAL🇳🇵❣️

  • @robertlisaru
    @robertlisaru Před 25 dny +1

    I feel like using [class] may introduce some confusion at some later point in the project, making styles seem to "appear out of nowhere". I think it should be used carefully. Do you agree?

  • @RickBeacham
    @RickBeacham Před měsícem

    Thanks!

    • @KevinPowell
      @KevinPowell  Před měsícem

      Thank you so much :D

    • @RickBeacham
      @RickBeacham Před měsícem

      ​@@KevinPowell I've learned so much. Just wanted to give back finally.

  • @ikhsan_345
    @ikhsan_345 Před měsícem +1

    Review TenoxUI CSS Framework

  • @sarahfox3652
    @sarahfox3652 Před 23 dny

    I need that z-index shirt 😂

  • @ADITYACHEBROLU
    @ADITYACHEBROLU Před měsícem

    What’s the font used here?

  • @rahmanmusah4470
    @rahmanmusah4470 Před 22 dny

    hi friends, which youtuber teaches javascript so well like how Kevin Powell teaches css?

  • @NoFakeCoding
    @NoFakeCoding Před měsícem

    I am just curious why you don't use BEM class names. Is this something that is just matter of choice?

    • @KevinPowell
      @KevinPowell  Před měsícem

      I used to use it all the time, but I've moved away from it, sort of. I've stopped using it in simple demos because there's no need, when all I have is a single component/block, or even a simple page. I've also moved more toward the CUBE CSS approach as well, in real projects.

  • @ManiKandan-bf8jy
    @ManiKandan-bf8jy Před měsícem

    Plz put a video about dev tools tips and tricks for css

    • @KevinPowell
      @KevinPowell  Před měsícem +1

      Recorded exactly that this morning 😆 - It'll be out this upcomign Tuesday.

  • @andreasnulein782
    @andreasnulein782 Před měsícem

    first tip: interesting, but proned to give $future-me or $other-dev a headache. "WHY IS MY BROWSER GOBBLING UP THE BULLET POINTS. I'M JUST ADDING A CLASS" i hear myself despairing in 6 months.
    second tip: nice, but why CSS, why!, can't it just be "n >= 7" and "n

  • @mmottet88
    @mmottet88 Před měsícem +1

    Seems like using the has is a bit overcomplicating things, why not go with ".showcase-list:hover img:not(:hover)" for the second opacity trick 🙂

    • @KevinPowell
      @KevinPowell  Před měsícem

      Definitely could have done that. I was already down the rabbit hole with :has() 😅

    • @edwardgray6167
      @edwardgray6167 Před měsícem +1

      That would leave you with a situation where when you hover over a gap in the grid then the images are still at 0.5 opacity. Kevin's solution means that the opacity changes only take effect when hovering over an image.

    • @mmottet88
      @mmottet88 Před měsícem +1

      ​@@edwardgray6167 It felt to me like he wanted to do a one style variant of the first effect but ended up with the flickering, and so added "if it's what you're going for" to justify it 🙂 Just wanted to friendlily point out that the one style variant could also be done without flickering 😉

  • @acubley
    @acubley Před měsícem

    I starting to really like :has, found out it lets me count- .column:has(> .subcolumn:nth-child(4)) { }.

  • @alandunbar4244
    @alandunbar4244 Před 27 dny

    Why does VS Code say that @layer is not compatible with the major browsers when it clearly is

  • @sharakpl
    @sharakpl Před měsícem

    And how would you fix this situation? I have a global style for `a{color:#000}` and for `.btn-light{color:#000;background:#eee}`. Now I want a dark footer so I add something like `footer{background:#222;color:#eee}` and also have to fix links so I add `footer a{color:inherit}` but this also breaks my buttons that happen to be used inside footer because now my in the footer has the same color and bg. So now I'm forced to use !important on all my .btn to ensure their colors always fit the background or add more styles specifically to all possible footer elements eg. footer .btn-light{} or have to be more specific on my footer links' selector eg. `footer a:not(.btn):not(.dropdown-item):not(.whatever){color:inherit}`. Is there any easier way to enforce proper styles for such components?

    • @Zirpho
      @Zirpho Před měsícem

      I use custom properties for this.
      a {
      color: var(--color-link)
      }
      .button {
      color: var(--color-button);
      }
      Then for your various color schemes, you can have a class for each defining the element's colors within them.
      .theme-dark {
      background-color: darkcolor;
      --color-link: somecolor;
      --color-button: someothercolor;
      }
      Then the button and link colors can be defined separately from each other. And if you only ever use the theme classes for color schemes/background colors you can be sure that any content within it will be colored correctly without having to add more styles.

    • @sharakpl
      @sharakpl Před měsícem

      @@Zirpho thanks but I was rather looking for a way to avoid setting additional styles for buttons based on the theme or parent components since it shouldn't matter in this case. Button's color should be only determined by the buttons background, no matter its location on the site. I think @scope could be useful here eg. @scope (.btn-light) {color: #222; background: #eee; }. Though I'm not sure footer a{} won't override it anyway due to higher specificity. Need to do some testing since I've never used @scope before...

    • @sharakpl
      @sharakpl Před měsícem

      As always Kevin is ahead of me 😀 czcams.com/users/shorts94qGY28H2cY?si=cDPTlkKib1PvLznc

    • @sharakpl
      @sharakpl Před 25 dny

      Looks like the way to go is "footer :where(a) {}" although it doesn't really work when using class ".main-footer :where(a)" because of a higher specificity (it's actually the same specificity, but because it's after .btn then it overrides it). I think the only way is to force btn's colors in all states with !important 🫤

  • @zitansalehin6941
    @zitansalehin6941 Před měsícem

    Love css. Without it website become lifeless but it is underrated, sad truth I think..if disagree someone may reply

  • @abkalucard
    @abkalucard Před měsícem

    The shirt
    ...
    Where do I get one

    • @KevinPowell
      @KevinPowell  Před měsícem

      mal-shop.fourthwall.com/en-cad/products/z-index-9999 😊

  • @6_clicks
    @6_clicks Před měsícem

    Always instructive ! An mind blowing!

  • @philwatts
    @philwatts Před měsícem

    Highest shirt in the world!

  • @juliusuwuigbe3274
    @juliusuwuigbe3274 Před měsícem

    Can anyone suggest a channel like this that is solely dedicated to JS vanilla, that helps to explain complex things in a simple way with practical examples. Thanks already.

  • @younessehorra5592
    @younessehorra5592 Před měsícem

    ♥️💯

  • @Stoney_Eagle
    @Stoney_Eagle Před měsícem +1

    z-index: 9999; and the host is still halfly hidden 🤯

  • @MaxArt2501
    @MaxArt2501 Před měsícem

    Hear me out: :not(:nth-child(n + 10)) is more readable than :nth-child(-n + 9)
    Negative coefficient counters mess up with developers' minds...

  • @leader7700
    @leader7700 Před měsícem

    круть

  • @azsassiin2897
    @azsassiin2897 Před měsícem

    Hey Kevin, are you looking for an apprentice? Lol….but seriously?

    • @SK-vg3mw
      @SK-vg3mw Před měsícem

      I am wondering what would he do with you, lol ? 😂 (but seriously?)

  • @stephaniepeters2590
    @stephaniepeters2590 Před měsícem

    Would .showcase-list:hover img:not(:hover) not also work? that would remove the flicker when not hovering any image

  • @cymy2
    @cymy2 Před 28 dny

    Thanks!