Write less code with these 5 CSS tips

Поделиться
HTML-код
  • Опубликовано: 13 июн 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 RUclips 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!

Комментарии • 100

  • @clevermissfox
    @clevermissfox 2 месяца назад +71

    Love that zindex shirt!!!

    • @kdvr60
      @kdvr60 2 месяца назад +3

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

    • @Syntax-Savvy
      @Syntax-Savvy 2 месяца назад +4

      ⁠channel members can get content early before public release.

    • @YellowBriefs
      @YellowBriefs 2 месяца назад +2

      We've all done it 😂

    • @LorenzoGasperoni
      @LorenzoGasperoni Месяц назад +1

      Please tell us where to find it

  • @thornikejangveladze8240
    @thornikejangveladze8240 2 месяца назад +32

    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 2 месяца назад +2

      Haha. Just scrolled down to suggest the same. 👍

  • @JacksonGreenMagic
    @JacksonGreenMagic 2 месяца назад +18

    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 2 месяца назад +5

      I thought exactly that :) nicely reduce the flickering too

  • @AndyFairlie
    @AndyFairlie 2 месяца назад +11

    That first tip for lists with and without classes is genius

    • @markshall94
      @markshall94 Месяц назад +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.

    • @PhilLesh69
      @PhilLesh69 22 дня назад

      You can start with a :not([class]) to create your own "default" styling for a tag, then add classes to any tags that need different styling. Though you could always use a combination of classes in the same tag to do a much better job of that.

  • @Kapcash
    @Kapcash 2 дня назад

    The "all previous" and "all after" selectors are useful for calendars, so you can target all days before the one you are hovering (like disabling them or whatever)!

  • @aspizuwastaken
    @aspizuwastaken 2 месяца назад +22

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

    • @visuellegedanken
      @visuellegedanken Месяц назад

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

  • @flavioczuk
    @flavioczuk 23 дня назад

    A simple idea to use on 1:18 is a simple start rating system for a product, ecommerce or something. Very handy for the user to rate something from 1-5 for example.

  • @RasmusSchultz
    @RasmusSchultz 5 дней назад

    that showcase hover effect is nice! I simplified it a bit:
    .showcase-list:hover img:not(:hover) {
    opacity: .5;
    }
    that is, instead of :has(img:hover) on the parent selector, just use :hover - simpler and actually looks better, since the whole list of images won't flicker in and out when you hover over the gaps between the items. 💁‍♂

  • @madiko
    @madiko Месяц назад

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

  • @moy2010
    @moy2010 2 месяца назад +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 Месяц назад +2

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

    • @moy2010
      @moy2010 Месяц назад

      @@Le_Sylvestre Sure, will do!

  • @andru2625
    @andru2625 2 месяца назад +1

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

  • @xldoesit
    @xldoesit 2 месяца назад +2

    the image grid nth child thing blew my mind

  • @rikithikatakizu2
    @rikithikatakizu2 Месяц назад +1

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

  • @vinodkumarpujar1
    @vinodkumarpujar1 2 месяца назад +1

    Thank you for knowledge sharing Kevin.

  • @YacineBougera
    @YacineBougera 2 месяца назад

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

  • @dzsuvi
    @dzsuvi 2 месяца назад

    Good ones. Learning something every day. Thanks Kevin!

  • @lyrebird712
    @lyrebird712 Месяц назад

    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.

  • @potrosanjuan
    @potrosanjuan 2 месяца назад

    Master class as always, thanks!!!!!

  • @solvedfyi
    @solvedfyi 2 месяца назад

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

  • @funisallowed6046
    @funisallowed6046 2 месяца назад +1

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

  • @Nusamaxa
    @Nusamaxa Месяц назад

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

  • @todd.mitchell
    @todd.mitchell 2 месяца назад

    Brilliant! Thank you so much.

  • @5pointsaday
    @5pointsaday 2 месяца назад

    Thank you Kevin!

  • @george003
    @george003 2 месяца назад

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

  • @svivian
    @svivian 13 дней назад

    For the last one, instead of setting the color and background property each time, I would just change the custom property value (e.g. start with -button-color: -clr-neutral-900; then do -button-color: -clr-neutral-100; below).

  • @BenjiFriedman
    @BenjiFriedman 2 месяца назад

    Interesting CSS moves as always 👍

  • @Kogemate
    @Kogemate Месяц назад

    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).

  • @bobmarteal
    @bobmarteal 2 месяца назад +2

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

    • @DarrenbyDesign
      @DarrenbyDesign Месяц назад

      Came here to say THE EXACT SAME THING 😂

  • @Stephinmaju
    @Stephinmaju Месяц назад

    Kevin powell ❌ Kevin css well✅

  • @OCEMTechZone
    @OCEMTechZone 2 месяца назад

    Great Sir 🎉

  • @clevermissfox
    @clevermissfox 2 месяца назад +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

  • @realscout1775
    @realscout1775 2 месяца назад

    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.

  • @cymy2
    @cymy2 Месяц назад

    Thanks!

  •  2 месяца назад

    Awesome!!!

  • @Simulacra001
    @Simulacra001 Месяц назад

    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.

  • @robertlisaru
    @robertlisaru Месяц назад +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?

  • @cintron3d
    @cintron3d 2 месяца назад

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

  • @Krilllind
    @Krilllind 2 месяца назад

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

  • @SeanGrimLink
    @SeanGrimLink 2 месяца назад +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 2 месяца назад +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  2 месяца назад

      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.

  • @manishkarki8119
    @manishkarki8119 2 месяца назад

    LOVE FROM NEPAL🇳🇵❣️

  • @kirillvoloshin2065
    @kirillvoloshin2065 2 месяца назад +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

  •  2 месяца назад

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

  • @TheColonThree
    @TheColonThree 2 месяца назад

    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.

  • @ayushgogna9732
    @ayushgogna9732 2 месяца назад

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

  • @hoshi411
    @hoshi411 2 месяца назад

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

    • @KevinPowell
      @KevinPowell  2 месяца назад +1

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

    • @MattDunlapCO
      @MattDunlapCO 2 месяца назад

      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.

  • @sarahfox3652
    @sarahfox3652 Месяц назад

    I need that z-index shirt 😂

  • @ManiKandan-bf8jy
    @ManiKandan-bf8jy 2 месяца назад

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

    • @KevinPowell
      @KevinPowell  2 месяца назад +1

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

  • @ikhsan_345
    @ikhsan_345 2 месяца назад +1

    Review TenoxUI CSS Framework

  • @ADITYACHEBROLU
    @ADITYACHEBROLU 2 месяца назад

    What’s the font used here?

  • @andreasnulein782
    @andreasnulein782 2 месяца назад

    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

  • @NoFakeCoding
    @NoFakeCoding Месяц назад

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

    • @KevinPowell
      @KevinPowell  Месяц назад

      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.

  • @alandunbar4244
    @alandunbar4244 Месяц назад

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

  • @acubley
    @acubley 2 месяца назад

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

  • @rahmanmusah4470
    @rahmanmusah4470 Месяц назад

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

  • @mmottet88
    @mmottet88 2 месяца назад +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  2 месяца назад

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

    • @edwardgray6167
      @edwardgray6167 2 месяца назад +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 2 месяца назад +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 😉

  • @sharakpl
    @sharakpl 2 месяца назад

    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 2 месяца назад

      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 2 месяца назад

      @@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 2 месяца назад

      As always Kevin is ahead of me 😀 ruclips.net/user/shorts94qGY28H2cY?si=cDPTlkKib1PvLznc

    • @sharakpl
      @sharakpl Месяц назад

      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 2 месяца назад

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

  • @philwatts
    @philwatts 2 месяца назад

    Highest shirt in the world!

  • @6_clicks
    @6_clicks 2 месяца назад

    Always instructive ! An mind blowing!

  • @younessehorra5592
    @younessehorra5592 2 месяца назад

    ♥️💯

  • @abkalucard
    @abkalucard 2 месяца назад

    The shirt
    ...
    Where do I get one

    • @KevinPowell
      @KevinPowell  2 месяца назад

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

  • @juliusuwuigbe3274
    @juliusuwuigbe3274 2 месяца назад

    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.

    • @d18517
      @d18517 2 месяца назад +1

      Wes Bos 30 days of Javascript

    • @juliusuwuigbe3274
      @juliusuwuigbe3274 2 месяца назад

      @@d18517 thanks.

  • @Stoney_Eagle
    @Stoney_Eagle 2 месяца назад +1

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

  • @leader7700
    @leader7700 2 месяца назад

    круть

  • @MaxArt2501
    @MaxArt2501 2 месяца назад

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

  • @azsassiin2897
    @azsassiin2897 2 месяца назад

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

    • @SK-vg3mw
      @SK-vg3mw 2 месяца назад

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

  • @stephaniepeters2590
    @stephaniepeters2590 2 месяца назад

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

  • @RickBeacham
    @RickBeacham 2 месяца назад

    Thanks!

    • @KevinPowell
      @KevinPowell  2 месяца назад

      Thank you so much :D

    • @RickBeacham
      @RickBeacham 2 месяца назад

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