Create a horizontal media scroller with CSS

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 24. 07. 2024
  • Horizontal scrolling components are becoming more and more common, so let's look at the CSS behind them!
    đŸ’» The starting code: codepen.io/kevinpowell/pen/NW...
    đŸ’» The finished version: codepen.io/kevinpowell/pen/Ex...
    🔗 Links
    ✅ Replay with Adam Argyle that I mentioned: / 1277849387
    ✅ Adam's GUI challenge media scroller: web.dev/building-a-media-scro...
    ✅ Give Adam a follow on Twitter: / argyleink
    ✅ Open Props: open-props.style/
    ✅ Lea Verou's article on locally scoped custom properties: lea.verou.me/2021/10/custom-p...
    ✅ MDN on scroll snapping: developer.mozilla.org/en-US/d...
    ✅ More info on flex vs grid: ‱ Flexbox or grid - How ...
    ✅ Customize the scrollbar: ‱ Create custom scrollba...
    ⌚ Timestamps
    00:00 - Introduction
    01:25 - The HTML and what we're starting with
    02:51 - Creating the horizontal scrolling component
    08:42 - Styling the individual items
    12:09 - Improved locally scoped custom props
    15:21 - Adding scroll snapping
    20:43 - Making scroll groups

    #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 • 294

  • @piyusharyaprakash4365
    @piyusharyaprakash4365 Pƙed rokem +1

    Man love this tutorial, the fact that you explain what a particular css property does is amazing. Always needed someone to help me understand what a property actually does. I'll be visiting your channel every now and then for these css tips!

  • @mohammadabujaradeh6993
    @mohammadabujaradeh6993 Pƙed 2 lety +5

    To be honest I am not fan of css but after following you for almost a year your are changing my thoughts on css and how enjoyable can be working on it , your videos ( not matter the topic ) is clear, easy to understand and supported with great examples . Well done Kevin. And same as everyone looking forward for open props too

  • @remkospaans
    @remkospaans Pƙed 2 lety +10

    Hadn't heard of Open props before, so yes, more explanation would be welcome. Thank you very much Kevin.

  • @ayitinya
    @ayitinya Pƙed 2 lety +12

    You literally just read my mind. Been looking for a tutorial like this. Much appreciation sir

  • @sidharthanand9946
    @sidharthanand9946 Pƙed 2 lety +2

    Wow. I was already working on creating a horizontal layout and was completely frustrated at not being able to create the way I thought. Thanks to Kevin for creating this video 👍

  • @joshfriedman3167
    @joshfriedman3167 Pƙed 2 lety +6

    Would love to see a video on open props. This is the first I'm hearing of it but definitely something that seems worth exploring!!

  • @the-suspect
    @the-suspect Pƙed 2 lety +46

    I want you to teach everything, seriously. Your teaching style is so good and easy to follow.

  • @P1vko_BeatBrewer
    @P1vko_BeatBrewer Pƙed 2 lety +1

    Yes please! I would gladly dive into watching video about open properties as well! Looks so helpful to boost creativity on projects!

  • @tedsemon1326
    @tedsemon1326 Pƙed 2 lety

    Wonderful video Kevin, I learned a lot. You explain things very clearly, not only to show us how to do something but telling us why we might want to do something too.

  • @williamogburn7836
    @williamogburn7836 Pƙed 2 lety

    I've been coding for a little over six weeks now and your videos have really helped me stay encouraged. Thanks, Kevin!

    • @gbbarn
      @gbbarn Pƙed 4 měsĂ­ci

      Did you make it?

  • @joelhodes1619
    @joelhodes1619 Pƙed 2 lety

    I was wondering just this morning how to implement snap horizontal scrolling. You’ve saved me a couple of hours work. Perfect timing!

  • @shinovar5465
    @shinovar5465 Pƙed 2 lety

    This is crazy i was googling like crazy for a good video on this and I cant believe you ended up posting one right away the next day. I LOVE YOUUUUUU !!

  • @atrus3823
    @atrus3823 Pƙed 2 lety +8

    I always learn something new on this channel! I didn't know CSS supported snapping or that overflow scroll behaviour thing, which is handy.
    For the spacing not being related to the .snaps-inline class. What I always do in these cases is use the multiple class selector, e.g., .media-scroller.snaps-inline and put the property on that. This way, .snaps-inline can be reused and have specific padding (or no padding) for other types of elements. The snap padding also isn't mucking up the .media-scroller. It only applies where both classes are used.
    PS: I once made an HTML/CSS-only scroller/carousel (more like Netflix with prev/next buttons) with radio buttons and old school HTML/CSS. I used hidden radio buttons before each slide (2 before first slide and none before last slide... explained later) and used labels for the slides that pointed to the radio button before the previous slide. When you clicked on a slide, the before the previous slide would become selected. This way, I could use the :checked + .slide selector to select the previous slide, :checked + .slide + * + .slide to select the current slide, and :checked + .slide + * + .slide + * + .slide to select the next slide. Because each slide was a label, clicking the slide itself took you to that slide, so to have prev and next buttons, I simply had a bit of the previous and next slides showing before and after (respectively) the current slide. I would post the CodePen, but I don't want this comment flagged.

    • @KevinPowell
      @KevinPowell  Pƙed 2 lety +1

      Yeah, I'm thinking of doing something like that as a "next" step on this one actually... it's a fun exploration of CSS combinators and other things which, while it can be a little annoying, they're a fun way to learn. I always get lost within the selectors when making them though, lol. I was thinking of using links with :focus-within to move things around, but your approach might be better.

  • @drewnorman3632
    @drewnorman3632 Pƙed 2 lety

    Nice work KP. Your tutorials make grid less scary. May the force be with you

  • @JeremyChone
    @JeremyChone Pƙed 2 lety

    So good. We have been doing grid for years, but still learn from your videos. Top content for any web app dev.

  • @AbdulRahman-wj9fw
    @AbdulRahman-wj9fw Pƙed 7 měsĂ­ci

    I've been looking for a way to fix the column issue in the grid carousel for ages now! You solved it perfectly. Very nice explanation. Loved your JS tutorial for carousel, it's so nice to understand what each code means!

  • @kristianulvund4274
    @kristianulvund4274 Pƙed 2 lety +1

    This is giving me tears of joy at the moment. Thanks, KevinđŸ€©

  • @Rico-cp4xp
    @Rico-cp4xp Pƙed 2 lety +84

    Open props seems like a game changer when it comes to quick prototyping. Would love to see a video on it!

  • @pranavyeole102
    @pranavyeole102 Pƙed 2 lety

    oh heck yeah, we need a in-depth video on open props! seems really interesting

  • @wendyhoile7659
    @wendyhoile7659 Pƙed 2 lety

    Yes Please to Open Props!!! Also thank you for this video. I made one by accident a year or so ago with flexbox, but of course the whole page moved. Now I know what to do. Thank you Kevin

  • @cesarimura3398
    @cesarimura3398 Pƙed 2 lety

    Oh my god, was looking for it yesterday, you are the man Kevin, thank you so so much!

  • @natalieacevedo4575
    @natalieacevedo4575 Pƙed 2 lety

    Kevin thanks so much for your awesome job and teaching style. I have learned sooo0 much from you, hands down the best teacher of youtube.. greetings from Portugal! 😘

  • @ayushgogna9732
    @ayushgogna9732 Pƙed 2 lety

    what a perfect guy you are you are using some css I didn't ever hear the name that these properties exist the work you put and its in your brain is like amazing

  • @SebastianZartner
    @SebastianZartner Pƙed 2 lety

    Can't give this video enough thumbs up. 😀There is so much useful information regarding horizontal scrolling, grid layout and scroll snapping.

  • @pinnaclewd
    @pinnaclewd Pƙed 9 měsĂ­ci

    A great addition to any developer. Snap scroll is becoming the in thing requested by more and more clients.

  • @thecoderabbi
    @thecoderabbi Pƙed 2 lety +1

    I will definitely try this out when I can. Love you Kevin!

  • @dave6012
    @dave6012 Pƙed 2 lety

    I struggled with scroll snap until I gave up on it. Thank you for demystifying! 🙌🙌

  • @user-kn4oi9oh4i
    @user-kn4oi9oh4i Pƙed 6 měsĂ­ci +1

    All hail the CSS King! Thank you for this awesome tutorial good sir!

  • @stephenchiou5505
    @stephenchiou5505 Pƙed 2 lety

    Thanks for the great video again! You never disappoint. Would love the open prop video as well~

  • @solaraproject3614
    @solaraproject3614 Pƙed 10 měsĂ­ci

    Bro, you saved my life! i was about to end it cause i couldn't, for the love of God, get the videos to fit properly in the container and it was acting very weird whenever i change display and position to get them in line. Thanks again yo! you the man Kevin!

  • @merko3043
    @merko3043 Pƙed 2 lety

    I always learn something new when I watch your videos. Thank you and keep it up! :)

  • @adityaprakash5093
    @adityaprakash5093 Pƙed 2 lety

    Thank you Kevin!! This video really helped me with a challenge I have in my project!

  • @justinjoka5019
    @justinjoka5019 Pƙed 2 lety

    Perfectly what i needed made me fall in love with CSS even more

  • @Sixthfred
    @Sixthfred Pƙed 2 lety

    I really like your videos Kevin, am starting to fall in love with css!

  • @frankdelange1122
    @frankdelange1122 Pƙed 2 lety +36

    Wonderful tutorial. I would love to see a part 2, where you get rid of the horizontal scrollbar (visually) on mobile and then add the desktop version with arrows. Netflix all the way.

    • @KevinPowell
      @KevinPowell  Pƙed 2 lety +27

      Yeah, thinking about doing a v2 with that, seems like a popular request 👍

    • @DomCos78
      @DomCos78 Pƙed 2 lety +2

      @@KevinPowell - as an iteration of that, I would love the horizontal scroll to automatically start scrolling when your mouse hovers over it and you use the scroll wheel. ie: no need to press shift and scroll and no need to grab the scroll bar. Without adding JS, the only solution I have found is to rotate the parent 90deg and then the child -90deg. BUT, I have not spent enough time figuring out the CSS to get it to look right. The function is there, it just looks....well....broken. Your insight would be amazing here!

    • @KevinPowell
      @KevinPowell  Pƙed 2 lety +2

      @@snehithreddy2684 not yet, no. Another 3-4 weeks probably

    • @dichen4495
      @dichen4495 Pƙed 2 lety +2

      @@KevinPowell It's been 8 weeks~

    • @hakeryk
      @hakeryk Pƙed 2 lety

      @@DomCos78 I was going to write the same request here - how to create this when your mouse is hovering on scroll then with scrolling down it would scroll right instead.

  • @twinconceptsdesign
    @twinconceptsdesign Pƙed 2 lety

    An explainer video on open props would be highly appreciated 👍. As always great tutorial

  • @nathanhoskins438
    @nathanhoskins438 Pƙed 2 lety +17

    This is perfect! Needed to know how to do this and I love how you explain every step in detail. What if I wanted my media to be videos and open in lightbox?

    • @WhatAwinning
      @WhatAwinning Pƙed 2 lety +2

      I LITERALLY NEED THIS SAME THING

    • @shinovar5465
      @shinovar5465 Pƙed 2 lety +2

      DUDE EVEN I LITERALLY NEEDED THIS EXACT THING! I was wondering why there isn't a single good video on this and BOOM! KP STRIKES

  • @vincentmesnige
    @vincentmesnige Pƙed 2 lety +26

    There's a little problem for your snaping scroll part for the "alone"' element, with the scroll-snap-align: start, when you're on the last item, you won't be able to snap to it, because it will snap to the "start" of the first element on the right.
    you need to add :last-child scroll-snap-align: end so you can snap on the last children ;)
    Doesn't bother the group part normally^^

  • @onnot701
    @onnot701 Pƙed 2 lety

    YES please video on open props

  • @DanteMishima
    @DanteMishima Pƙed 2 lety

    Yes! Please, an open prop would be greatly appreciated

  • @avinchaudhary1007
    @avinchaudhary1007 Pƙed 2 měsĂ­ci

    Kevin Powell is the goat of html and css......thank you sir for providing free content on youtube ......hope you will provide us more content on youtube

  • @sicktastictv
    @sicktastictv Pƙed 2 lety

    As always, great video Kevin!

  • @Rallosz
    @Rallosz Pƙed 2 lety

    It's like you read my mind; I've been thinking about this for a few days now! 👀

  • @crowebro9581
    @crowebro9581 Pƙed 2 lety

    Thank you for this! I esp loved the open props. I def want to see that video. Ok, let's face it. I love all of your videos but as far as learning is concerned, that would be the next logical bit of information for me to learn.

  • @RaveKev
    @RaveKev Pƙed 2 lety

    Yeah, an Open Props video would be great!

  • @gb76231
    @gb76231 Pƙed 2 lety

    the level of excitement that i felt when i saw this video's thumb really tells me that i got my career choice right :)

  • @shaystibelman
    @shaystibelman Pƙed 2 lety

    wow, so many new properties I have to Google now! jesus, inline-size!? wtf! thank you so much!

  • @D7460N
    @D7460N Pƙed 2 lety

    I'm sooooo going to use this! Thank you!

  • @cdey3886
    @cdey3886 Pƙed 2 lety

    Thanks, I really learnt a lot and I appriciate you channel and videos very much!
    P.S: 3:32 I love how you said "auto", and I don't know why.

    • @KevinPowell
      @KevinPowell  Pƙed 2 lety

      haha, the fun of slowing down a little as you type words out, lol

  • @bishwajeetpandey1570
    @bishwajeetpandey1570 Pƙed 2 lety

    I could not stop myself to subscribe your channel.
    Thx for this content 🙏

  • @marcelo25758
    @marcelo25758 Pƙed 2 lety

    Omg just the other day I hoped you'd make a video about this!!

  • @GotMoreCakes
    @GotMoreCakes Pƙed 2 lety +1

    The next part of this could be demonstrating increasing the size of a hovered element and not falling foul of overflow-y issues on the 'tracks' above/below.
    Netflix (desktop) use a modal approach, but don't have drag scroll.
    Amazon use nested overflows with weird magic number margins.

  • @universalponcho
    @universalponcho Pƙed 2 lety

    This was super helpful. Cheers.

  • @andrekrug9215
    @andrekrug9215 Pƙed rokem

    Thanks Kevin!!! You help me so much! thanks lot!

  • @polygonstorm4467
    @polygonstorm4467 Pƙed 2 lety

    Brutal tutorial!

  • @IAmSamuelCharpentier
    @IAmSamuelCharpentier Pƙed 2 lety

    For the 25% bit, I would make a container on which controls (buttons with arrows for example) are also child of and make that container have margin, with the controls positioned absolutely in that margin. On top of that, the next item would show underneath the controls, so 25 % would be a fine size imo.

  • @sabuein
    @sabuein Pƙed rokem

    Thank you, Kevin.

  • @ziadamer7621
    @ziadamer7621 Pƙed 2 lety

    thank u sir i just really wanted
    this video ❀❀❀

  • @ecological6454
    @ecological6454 Pƙed 2 lety +2

    Kevin is one of those Chad's who strive to improve the lives of many.

  • @semigodprogrammer
    @semigodprogrammer Pƙed 2 lety +6

    Your videos helped me understand CSS better than my teacher tauth me, thanks a lot

  • @renebesicke
    @renebesicke Pƙed 2 lety

    Awesome content again! :D

  • @adamsDevArt
    @adamsDevArt Pƙed rokem

    amazing thanks a lot kevin!!

  • @OryginTech
    @OryginTech Pƙed 2 lety +1

    Do you recommend open props for production code? Seems like a great tool to be using in industry not just for prototyping

  • @rohitshukla9179
    @rohitshukla9179 Pƙed 2 lety

    video is great, if anyone is following this video without open props, remember to add on img { max-inline-size: 100%; }

  • @justkailash
    @justkailash Pƙed rokem

    Awesome tutorial.... you always rock....

  • @bsully75
    @bsully75 Pƙed 2 lety

    I would definitely like to see a video from you on open props

  • @mpumelelo655
    @mpumelelo655 Pƙed 2 lety

    Let's gooooo open-props!!!!

  • @brickwarsnet
    @brickwarsnet Pƙed rokem +1

    Hey Kevin - I loved your tutorial. I'm curious if there is an easy way to implement forward and backward buttons. The reason for this is I like the look of a thinner horizontal scrollbar on mobile in addition to the swipe gesture. However, you're forced to use the scrollbars on desktop views and if I make them thinner, they are more difficult to grab. It would be nice to overlay larger forward and backward buttons on top of the div for some additional accessibility. I've looked into scrollToLeft, scrollToRight with simple onclick buttons - but was curious if you could point a resource that would work in conjunction with horizontal scrolling.

  • @user-ms8ei9le7x
    @user-ms8ei9le7x Pƙed 2 lety

    Kevin thanks , the nice lesson 🎓

  • @oktoniuszevanyasimanungkal9554

    thank you for the tutorial!

  • @youtindia
    @youtindia Pƙed 2 lety

    It's really cool. That been said, if you make say left and right button which scrolls the slider to left and right then there won't be any smooth scrolling effects in Safari. Safari does not support smooth scrolling so it will directly jump to next and previous on button press.
    That's the reason why in most cases we unfortunately cannot use this in production because these scrollers are usually tired with next/prev buttons and Safari messes that up.
    Safari is the new IE.

    • @KevinPowell
      @KevinPowell  Pƙed 2 lety +1

      scroll-snap and smooth-scrolling behavior are very different, scroll-snap is supported in Safari :)

  • @chetanjain4616
    @chetanjain4616 Pƙed 2 lety +2

    Great Video Kevin, one thing that was missing was scrolling feature where you scroll vertically but that section moves horizontally

    • @FeryRohrer
      @FeryRohrer Pƙed 2 lety

      Yes that would be great, but I guess for this there would hav to be some JavaScript involved

    • @KevinPowell
      @KevinPowell  Pƙed 2 lety

      I wouldn't do that because it can be *really* annoying for users. I know it's "easier" in a sense, specially since most users don't know you can horizontally scroll with shift + mouse wheel, but scroll-jacking in general is a dark pattern. I'd rather use JS to add a next and previous button.

    • @FeryRohrer
      @FeryRohrer Pƙed 2 lety

      @@KevinPowell Wold it be possible to give us an idea on how to achieve this next and previous buttons with JS? Thanks for your super great content, I really enjoy your videos a lot 👏👏👏

  • @MrXandrios
    @MrXandrios Pƙed 2 lety

    Nice, never new about, this is possible without js!
    Many thanks here

  • @richardinho95
    @richardinho95 Pƙed 2 lety

    Great video đŸ€©!
    Question: Would you be able to increase the size of the center most element without using any javascript?

  • @eyupucmaz
    @eyupucmaz Pƙed 2 lety

    Hey Kevin đŸ‘‹đŸ» really liked the video. And open props is useful lib, i'd like see video about it. Specially animations with open props 👀

  • @063_muhammedrinshadsr6
    @063_muhammedrinshadsr6 Pƙed rokem

    that was soo good it solved my problem within first 8 minutes thankyou soo much (note : But i watched the whole video :) )

  • @shadow-bm5wj
    @shadow-bm5wj Pƙed 2 lety +1

    Is there any way to move the focus onto the element that has been snapped to? Because there is an issue when navigating with the keyboard. Scrolling horizontally using arrow keys work nicely, but then pressing tab will take you back to the element that had the focus before which can mess up the snapping behaviour.

  • @Stoney_Eagle
    @Stoney_Eagle Pƙed 2 lety

    Open props is kinda like tailwind but it's used in css instead of html. Pretty neat!
    Have I been putting to much effort in creating my Netflix style scroller đŸ€”đŸ€Ż

  • @orrodrigez5051
    @orrodrigez5051 Pƙed rokem

    Thanks man! This video is awesome.
    Any way to style this scroll bar?

  • @cloudart4070
    @cloudart4070 Pƙed 2 lety

    Thank you sir I always problem faced with horizontal scrolling .

  • @sebastianmihaiprisacariu8975
    @sebastianmihaiprisacariu8975 Pƙed 4 měsĂ­ci

    Would be interesting to see how you would add arrows to this, although that probably isn’t css related.

  • @grgvgrgv
    @grgvgrgv Pƙed 2 lety

    I'm studying JS and I just need to make such a scroll. I can imagine the algorithm, but I haven't done it yet. I'll watch the video now :) Greetings from Moscow :)

  • @boldwin2
    @boldwin2 Pƙed 2 lety

    Great tutorial! Is it possible that you add an onClick behavior on a media-element and the element also animates to the snap location?

  • @malikd.mcnish6482
    @malikd.mcnish6482 Pƙed 2 lety

    hey great vid but if you could preview the end result of the website or design at the starting of your video would be great, it gives us more reference in what we making, Thanks again Kev.

  • @holdenjack
    @holdenjack Pƙed 2 lety

    Amazing video, please can you maybe do a part 2 and show how best to produce an infinite horizontal media scroller using just css.

  • @eftihisbotonakis7204
    @eftihisbotonakis7204 Pƙed rokem

    Amazing tut. I was just looking for this. I want to create something similar but instead of having 1 row I would like 2 rows. Any idea how that would work? Would I need to use grid flow row and grid template rows/ columns for that ?

  • @sergejstojanovic2518
    @sergejstojanovic2518 Pƙed 2 lety

    I needed this like 2 days ago. It's like You hear me XD thanksn

  • @prsmahajan
    @prsmahajan Pƙed 2 lety +1

    East to the west, Kevin remains always the supa best😍

  • @acnrsi7170
    @acnrsi7170 Pƙed rokem

    Have you made a video which is basically the same but instead of a scroll bar below, they can click an arrow on the sides to make the boxes move left or right?

  • @johanstam3602
    @johanstam3602 Pƙed rokem

    Could you do a vid if there is a slider in a container with overflow on the right?

  • @TenderHooligan
    @TenderHooligan Pƙed 8 měsĂ­ci +1

    how about if you want to be showing the center of the media scroller by default? i used justify-content: center which worked, but im getting an issue where i cant scroll leftwards

  • @xuguangyu88
    @xuguangyu88 Pƙed 2 lety

    Great tutorial!

  • @bonnes04
    @bonnes04 Pƙed rokem

    thanks, Open props

  • @detroitluv82
    @detroitluv82 Pƙed 2 lety

    this is awesome! I just need to make it responsive with different image sizes for mobile and desktop

  • @nosolocodigos
    @nosolocodigos Pƙed rokem

    Could you add to icons at each side? To scroll with the arrows icon left and right?

  • @marcusunivers
    @marcusunivers Pƙed 2 lety

    Awesome Tutorial. 😊👍
    I have one question to this:
    How I can make it behave that after I scroll to the end on the container, the entire page scrolls normal down again?

  • @novanoskillz4151
    @novanoskillz4151 Pƙed 2 lety

    thanks... is there a video where you remove the scrollbar??

  • @AmanpreetSinghCHD
    @AmanpreetSinghCHD Pƙed rokem

    How to control the width of the scroller for some reason my scrollbar I tried both width/height nothing seem to work

  • @privateinvestigationenterp4403

    This is good. What happens when you want to populate videos in the slider? I see photos, but

  • @cristiannass3945
    @cristiannass3945 Pƙed 8 měsĂ­ci

    Thank you for the tutorial. I have a question though:
    In mobile view, when I touch and drag to the left or right, I want to shift just one image to either side, regardless of how hard I drag. Is there a way to achieve this (Please help me I have to finish this task ASAP)