Это видео недоступно.
Сожалеем об этом.

Stop fixed navigations covering content on scroll

Поделиться
HTML-код
  • Опубликовано: 12 авг 2024
  • When we have a fixed navigation with anchor links that go to different locations within the page, that navigation can cover content when it scrolls to that location on the page. Luckily, it's easy to fix with some scroll-padding!
    🔗 Links
    ✅ The CodePen: codepen.io/kevinpowell/pen/GR...
    ✅ More position sticky stuff: • A couple of cool thing...
    ⌚ Timestamps
    00:00 - Introduction
    00:51 - Making things smooth scroll
    01:40 - Adding scroll-padding
    02:33 - Improving it without magic numbers
    06:43 - The one downside
    #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!
    --
    This video has been dubbed into Spanish (United States) using an artificial voice via aloud.area120.google.com to increase accessibility. You can change the audio track language in the Settings menu. #DubbedWithAloud

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

  • @KevinPowell
    @KevinPowell  2 года назад +99

    I got early-access to a new thing YT is trialing for multi-lingual videos! If you speak Spanish, you can click the settings cog and there is an "audio track" option, with a Spanish audio track. Would love feedback on the quality of it!

    • @plexor-dev
      @plexor-dev 2 года назад +1

      Jajaja cool, that is amazing.

    • @QwDragon
      @QwDragon 2 года назад +2

      How have you made it? Just recorded video in one language and then add audio for another one? Or you can have different videos inside of one?
      I'm interested in making RU/EN videos :)

    • @plexor-dev
      @plexor-dev 2 года назад +4

      RUclips app added everything in Spanish from the title to the voice I was very surprised to hear it, it seems native Spanish

    • @KevinPowell
      @KevinPowell  2 года назад +8

      @@QwDragon I'm not entirely sure how it works tbh 😅 - I got invited to trial a new service called Aloud. In the end, they provide you with a single video that has multiple audio tracks to upload to RUclips. I don't know how it encodes the tracks to know the different languages for YT though... aloud.area120.google.com/

    • @QwDragon
      @QwDragon 2 года назад

      @@KevinPowell thanks, that's what I wanted to know - single video track and multiple audio. Very cool feature, but I expect comments in mixed languages to be a bit messy)

  • @MrMairu555
    @MrMairu555 2 года назад +14

    Others have said it before... now it's my turn... You're a mind reader! Have this exact issue that I've been trying to deal with the last couple of days! Been using a padding/negative padding hack, but it's "magic numbers" and a complete pain in the backside that way! This looks perfect and simple! Thank you!

  • @frittex
    @frittex 2 года назад +11

    I can't believe I wasted ~2hours on it, and actually, it's so easy. You're a life saver Kevin, thank you!

  • @bxrclxys
    @bxrclxys 2 года назад +2

    This is the exact problem I was having just yesterday and have managed to stumble upon this video a day later.
    Thank you very much Kevin, much appreciated for the consistent, helpful content!

  • @asyncawaited
    @asyncawaited Год назад +1

    You're an absolute beast. I've seen a few of your videos now and your knowledge of CSS has been helpful to me putting together this frontend. You've earned a sub.

  • @MrMudbill
    @MrMudbill 2 года назад +28

    I suppose it may be worth noting that aside from devs, users who are more likely to actually "resize" the window are those on mobile devices switching between portrait and landscape orientation. So if that causes a change to the layout, a ResizeObserver might be more relevant.

  • @RogerOnTheRight
    @RogerOnTheRight 2 года назад +1

    You do good work. Straightforward and to the point. All practical stuff.

  • @vimalathithand917
    @vimalathithand917 7 месяцев назад

    Hey kevin you literally are a lifesaver!! Was just searching for this for almost an hour! Thanks a lot!

  • @funworld8379
    @funworld8379 Год назад

    Thanks Kevin. I was doing a next js react project where I was facing this issue. I tried to think hard and couldn't come up with the solution. This 2 lines of code saved my day . Cheers🥳

  • @nanavybes
    @nanavybes 2 года назад +2

    Exactly what I was looking for 9days ago.
    Thank you 😊

  • @jonathanmcdermott25
    @jonathanmcdermott25 Год назад

    You are amazing Kevin! Thank you for all of the helpful tips and tricks. :)

  • @Lazy4444
    @Lazy4444 2 года назад +3

    Damn it scares the sht outta me when the video start and there was a voice over speaking spanish xD
    At first I tought that someone was reuploading your videos with spanish audio.
    My native language is spanish, but I still prefer to watch videos on english, specially when they have clear pronounciation like you

  • @kdfksfkldsklfkldsff
    @kdfksfkldsklfkldsff 2 года назад +3

    Thank you! Thank you! I was literally looking for a solution to this! So helpful!

  • @juanecheverry9055
    @juanecheverry9055 2 года назад +23

    The Spanish version sounds amazing! I will personally continue watching your videos in English because I understand both languages and I prefer your human voice over a robot, but I still think it’s pretty impressive that you can have multiple languages in a video like that.

    • @Elias-zn6og
      @Elias-zn6og 2 года назад +5

      I hear the spanish version too, but I'm learning english and I prefer to learn

    • @gbagba81
      @gbagba81 2 года назад

      Q loco no?

    • @blackpurple9163
      @blackpurple9163 Год назад

      How do you set a different language output? I only see captions but the voice is still English

    • @juanecheverry9055
      @juanecheverry9055 Год назад

      @@blackpurple9163 there’s a video settings button where you can change playback speed, cc, etc
      The option is called audio track. What device are you using to watch the video?

    • @blackpurple9163
      @blackpurple9163 Год назад

      @@juanecheverry9055 my smartphone, no RUclips app I've tried had this option

  • @brandonsayring
    @brandonsayring 2 года назад +10

    offsetHeight returns an integer value. Using document.querySelector('.primary-navigation').getBoundingClientRect().height will return a float value which will give you the precise height so you dont have to use the hacky -1 solution.

  • @fernandoboschiero1178
    @fernandoboschiero1178 Год назад

    Kevin, been learning so much from your videos. Thank you for the content.

  • @royjr.bustillo9813
    @royjr.bustillo9813 Год назад

    I have been fixing this problem on my code for like 3 weeks, and i just found the answer here. thanks, bro

  • @Demilada
    @Demilada 14 дней назад

    Thank you for always helping me solve my css issues!😅

  • @masterkratos2
    @masterkratos2 2 года назад

    WHAT THE!!! I was just searching for this, and here you upload the video about it.

  • @dx8pi6o48
    @dx8pi6o48 2 года назад +1

    I had no idea you could dynamically change css variables with javascript!!! That's so awesome! If only I knew this sooner xD

  • @oh-digital
    @oh-digital 9 месяцев назад

    Thanks, i had my scroll-padding-top on the element i was scrolling to rather than the root, saved me lots of time

  • @austinedwards6334
    @austinedwards6334 Год назад

    Perfect! Thank you! I've been racking my brains for 2 days trying to solve this issue!

  • @patelpriyank
    @patelpriyank 2 года назад +7

    Great video! I always used to do a margin-top for each section but this seems much better. Just out of curiosity, doesn't this affect any other scrolling element on the page? I can't think of anything at the moment but would like to keep in mind if this 'breaks' anything.

  • @yawnfranco
    @yawnfranco 2 года назад

    Literally perfect timing. Great vid

  • @MuhammadAbbas-bd4kg
    @MuhammadAbbas-bd4kg 6 месяцев назад

    Literally a life saver. Thanks alot Kevin

  • @ilyes-grine
    @ilyes-grine 2 года назад

    THANK YOU , every time a get a problem in this website am working on, i find that you have done a video about it. last time it was the hover in a mobile and this time the nav covering the content.

  • @frankiefab
    @frankiefab 2 года назад

    OMG! I just learnt something new which I'm going to apply on my portfolio website.
    Thank you for this useful tips.

  • @AbdicateDotNet
    @AbdicateDotNet 2 года назад +1

    Awesome video!! Thanks for using a smidgen of JS! :)

  • @TheCrazySoundStudio
    @TheCrazySoundStudio 2 года назад

    This way worked! I'm coming here for more CSS tricks.

  • @Gui-sector7
    @Gui-sector7 2 года назад

    Very interesting ! I used to to make it with scrollIntoView function in js, that helps to scroll at the center on the element. I even tried to create an absolute element and gave him the #id and put him like 50px on top of the article... that's a bit a diy thing 🤣 haha thank you I didn't know the property scroll-padding-top ! I will try this next project !

  • @AlJapairai
    @AlJapairai 2 года назад

    That is actually is going to help me, thank you

  • @MatthewMichalsky
    @MatthewMichalsky 2 года назад +27

    Another approach is to have a css variable that would control both the header’s height + the padding offset.
    Then, you wouldn’t need JavaScript or worry about window resizing

    • @pepperdayjackpac4521
      @pepperdayjackpac4521 2 года назад

      that would work? :o

    • @sktnml
      @sktnml 2 года назад

      can you please explain further?

    • @uziboozy4540
      @uziboozy4540 2 года назад

      @@sktnml doesn't really need more explanation, lol

    • @oriel-elkabets
      @oriel-elkabets 2 года назад +5

      ​@@sktnml
      :root {
      --navHeigth: 200px;
      }
      .nuv{
      height: var(--navHeigth);
      }
      html{
      scroll-padding-top: var(--navHeigth);
      }
      in the root selector you can create css variable
      the name of the variable start with two hyphen
      I think the reason Kevin didn't do it,
      It's because you don't always want to set your navigation height,
      Sometimes the height comes from the font size, and the padding and so on.
      In cases like this, you want to use js to get the final height and do the calculation by that

    • @betanpc8210
      @betanpc8210 2 года назад +1

      @@oriel-elkabets Ty for this friend

  • @ajinkya4658
    @ajinkya4658 Год назад

    thanks kevin, much needed video

  • @nickpapadatos7587
    @nickpapadatos7587 2 года назад

    This used to bother me as well - great video, thank you Kevin.

  • @pohanggg4589
    @pohanggg4589 2 года назад

    OMG I always face a problem like this, ALWAYS. So, usually when this happens, I would just simply set the #section_id to one div above its actual position, so when it scrolls, it would show the whole h2.section-title, but still, it does not feel right every time I do it.
    Sir, once again, thank you sooo much for helping me understand web programing interface, css, js, etc. Keep making videos, keep inspiring! God bless you, sir.

  • @marianagulias2711
    @marianagulias2711 2 года назад

    Awesome video!! Thank you kevin!

  • @michasosnowski5918
    @michasosnowski5918 8 месяцев назад

    Thanks, my friend linked me your video and I fixed my problem :)

  • @hikari1690
    @hikari1690 2 года назад +8

    Omg! This has bothered me forever cause I always need to hack a solution and am glad to find a css solution ❤️

  • @विवेकराजपूत
    @विवेकराजपूत 4 месяца назад

    This video saved my day. Thanks @kevin

  • @JackTheZuluman
    @JackTheZuluman 2 года назад

    I experienced the same thing with my portfolio. Thanks soo much for this

  • @paulmitchell2916
    @paulmitchell2916 2 года назад

    One of the other code tutorial guys uses a trick so the viewer doesn't have to watch and hear typing (and inevitable retyping).. I think he types in all his code and proofreads it, then deletes the bottom line, then the second to bottom line etc.. while shooting the actual video he re-does the lines one by one.. it's slick!

  • @daleryanaldover6545
    @daleryanaldover6545 2 года назад

    I always make my navigation bars at a fixed height, this fixes common annoying css layout shifts and inconsistent re-sizing. By default the anchor placement is on the top left edge of the anchor tag, the way I get around this anchor behaviour is by adding a padding-top to the actual anchor element equal to the navigation bar height. This way I don't need to change scroll offset. If you expect the navigation bar height to change that's when you turn to JavaScript for help.

  • @JimKernix
    @JimKernix 2 года назад +43

    You could also use Math.ceil() to round up in your JS constant and remove the + 1 or + 5 for the pixel value:
    const navigationHeight = Math.ceil(document.querySelector('.primary-navigation').offsetHeight);

    • @prokopistsesmetzis7974
      @prokopistsesmetzis7974 2 года назад +9

      You mean Math.floor()

    • @JimKernix
      @JimKernix 2 года назад +8

      @@prokopistsesmetzis7974 Oh, I forgot he changed it to -1 - then yes, Math.floor

    • @mykalimba
      @mykalimba 2 года назад +9

      @@JimKernix The real issue is that the "offsetHeight" property always returns an integer, so it's only moderately representative of the element's _actual_ height.

    • @dsdsspp7130
      @dsdsspp7130 2 года назад +2

      that just returns offset height itself.
      ceil and floor of an integer is the integer itself.

    • @JimKernix
      @JimKernix 2 года назад

      @@dsdsspp7130 What's your point? He notated that it was slightly below the actual number so using ceil will "rounds a number up to the next largest integer", floor "returns the largest integer less than or equal to a given number". So by using either of those removes the need to use +1 or -1 to get to the desired number. I'm not familiar with offsetHeight, I was making a suggestion as to an alternate method to arrive at the number he wanted.

  • @firelop
    @firelop 2 года назад +2

    I used to got the same problem, I fixed that by adding this css code: margin-top: -100px;padding-top: 100px;
    EXPLAIN: margin-top with negative value will destroy the space that padding-top makes. So it works.

  • @alexprofit4937
    @alexprofit4937 2 года назад

    Great trick , I love it . Thanks

  • @downpp2255
    @downpp2255 2 года назад

    Haha, wish i saw... wait this video came out now. I was tearing my hair away when trying to find a fix for it.. Thank you

  • @andrasjuhasz.
    @andrasjuhasz. Год назад +2

    A better way to do this without JavaScript:
    1. Set up a CSS var for the height of the top navigation, as the height of the element rarely change.
    2. Use that variable not just for setting top navigation height, but also for setting scroll padding top. Either directly the number, or using the var as part of a calc() function and adding a few extra pixels on top of the navigation height.

  • @DanielDeight
    @DanielDeight Год назад

    Love you Kevin!

  • @wernayasa
    @wernayasa 2 года назад

    Looks promising for my novel theme project.
    I'm struggle with JS, this help me understand JS little bit.

  • @keshavkishorram7678
    @keshavkishorram7678 2 года назад +1

    You are CSS hero ❤

  • @user-ni7ct5vr8u
    @user-ni7ct5vr8u 2 года назад

    Didn't know about scroll-padding. I usually have specific anchors for navigation (a.nav) and then I move them with CSS: a.nav { position: relative; top: 100px }. I will probably start using your solution instead in the future.

  • @eniolakayode2384
    @eniolakayode2384 2 года назад

    You know whats crazy, i was just experiencing this, i thought to use JS and your video pops up.... live saver

  • @universecode1101
    @universecode1101 2 года назад +1

    Css is so important ... Great Kevin 👏🏻

  • @ForgottenGames
    @ForgottenGames 2 года назад

    I just opened this video to drop a like because the title was all I needed to agree with

  • @elproductorrookie8385
    @elproductorrookie8385 Год назад

    Hi. I solved this problem, configuring a padding-top for the #. It was all due to an oversight, because I thought I was padding-topping my . I'm taking my first html and css lessons. I think that at first it happens to all of us that we do things without knowing.

  • @jornzander1285
    @jornzander1285 2 года назад

    Great tip! Thanx!

  • @lenninpalacios
    @lenninpalacios 2 года назад

    Kevin en Español, gracias.
    Me has ayudado tanto, y ahora en mi idioma!

  • @8koi245
    @8koi245 2 года назад

    Ahhhh I can't believe this! tysm!!

  • @chukwuebukaobiajulu2849
    @chukwuebukaobiajulu2849 2 года назад

    This is really awesome

  • @victoreleanya4375
    @victoreleanya4375 5 месяцев назад

    Thanks Kevin🎉

  • @supercoolninja
    @supercoolninja 2 года назад

    Nice tips !

  • @esprit-critique0.114
    @esprit-critique0.114 2 года назад

    i love it , thank you the king 🕺🏿.

  • @supTE
    @supTE Год назад

    Thanks for this cool tip.

  • @doubledeckercouch6251
    @doubledeckercouch6251 2 года назад

    Bummer - was hoping to see the window resize/observer fix covered in this video. Maybe in a ‘part two’? Pleaseeeeee :)

  • @HuynhLuong227
    @HuynhLuong227 2 года назад

    many thank, you are amazing

  • @rafaguzowski8104
    @rafaguzowski8104 Год назад

    i love your videos!

  • @Alex-xf2dd
    @Alex-xf2dd 2 года назад

    Thank you!

  • @libbeytds
    @libbeytds 2 года назад +3

    I just realized he says "Hello my frontend friends" in his intros, not "friend and friends" lol

  • @mochamadsyahrulakbar8850
    @mochamadsyahrulakbar8850 2 года назад

    Thank you very much sir

  • @normanejm
    @normanejm 2 года назад

    Right now I have to fix a problem like this in my work. thanks!!! jajaa

  • @rayrayaray
    @rayrayaray Год назад

    thank you kevin

  • @thanhnn16
    @thanhnn16 Год назад

    Thank you so much

  • @vohracodes
    @vohracodes Год назад

    youre savior thank you so much 😄

  • @tumbler9428
    @tumbler9428 2 года назад

    I did not know "scroll-padding" yet - thank you

  • @doctorbrad
    @doctorbrad Год назад

    I wish I could like this video twice... thank you!

  • @Steel0079
    @Steel0079 2 года назад +4

    I have never used observers, when you were talking about workarounds for resizing i thought you were going to mention event listeners. Is the observer a more convenient option? Or are there more advantages to observer approach?

    • @KevinPowell
      @KevinPowell  2 года назад +3

      tbh, you could probably just use window.resize. from what I understand (and my JS knoweldge is a bit limited, so take it with a grain of salt), but resize observer tends to be more performant than using an eventListener for resize, but that's more for elements, which can have trickle down effects (and potentially even lead to infinite loops). On the window, I'm not sure if there is a benefit.

  • @DMPDEV
    @DMPDEV Год назад

    awesome! I do have a question though, is there a way with only CSS and no Js?

  • @Obikin89
    @Obikin89 2 года назад +8

    I've fixed that issue by setting the height of my navbar in a CSS variable (with rem units so that it's responsive). No JS, and no need for any adjustment at all.

    • @rasmuslarsen77
      @rasmuslarsen77 2 года назад

      I’ve done the same. It works quite well.

    • @johnntinos1251
      @johnntinos1251 2 года назад

      This works only for a specific height navigation bar. So it is responsive on a static height element. Except, if you find all the braking points and use media query in CSS( that have problem in multi language sites).

    • @mykalimba
      @mykalimba 2 года назад

      This is the first solution that I thought of too. I would solve the problem in CSS using custom properties, and not bother with this JavaScript approach.

  • @tinayeisherobin
    @tinayeisherobin Год назад

    Thank you.

  • @sergejstojanovic2518
    @sergejstojanovic2518 2 года назад

    Thank You.

  • @omarnur2269
    @omarnur2269 Год назад

    What a legend

  • @soulvibration-groningen
    @soulvibration-groningen Год назад

    Great tutorial. 1 question; I've used a shrinking Navbar where after scrolling 80 px down the Navbar shrinks. The offset remains the initial height on page load. Is it possible to get the actual height? Or should I just subtract the difference from the JS var?

  • @mohamadybr
    @mohamadybr 2 года назад +1

    I had to create the entire functionality in JS, then I learned about scroll-padding-top. It was a frustrating situation xD

  • @mhcostapimenta
    @mhcostapimenta 2 года назад

    Awesome!!!!

  • @camilomolina2110
    @camilomolina2110 2 года назад

    Esta nueva función de audio traducido al español es genial para todos los latinos que aprendemos a programar gracias a este canal!!

    • @leo3464
      @leo3464 2 года назад

      Vos escuchas como un chirrido de fondo? Es re incomodo

  • @ubaidnoor6598
    @ubaidnoor6598 Год назад

    Amazing 🤩

  • @Edu4Dev
    @Edu4Dev 18 дней назад

    You can alse use a ::before pseudo-elemento with blank content and negative margin-top.

  • @Hefss
    @Hefss 2 года назад

    Thenks, I didn't know you could change css variables in Javascript

  • @katemc516
    @katemc516 2 года назад

    I have a similar problem, except the fixed nav pops up at the bottom (I have to use absolute in this case), it always blocks the last bit of the content when it does so I added conditional extra padding-bottom to the content when the nav pops up.
    Now the problem is the content is extended by that extra padding therefore pushing the nav further down so after it pops up I have to scroll down again to see it, so is there any CSS or JS code to make the content scroll down to the very bottom?
    Also love your videos 😊

  • @QwDragon
    @QwDragon 2 года назад

    Actually the was a way of making similar thing with paddings and margins, but a spicial property is much simplier!

  • @justkailash
    @justkailash Год назад

    Thankyou... save my life ...

  • @bwsince1926
    @bwsince1926 2 года назад

    Javascript functions: the bane of my existence. I haven't managed to get into Three.js or similar because even if I know Javascript well-ish, I don't get all the calculations!

  • @NeerajGupta-fy1bv
    @NeerajGupta-fy1bv 2 года назад

    Hi Kevin, any updates on your new course "Beyond CSS"?

  • @AutsajderRR
    @AutsajderRR Год назад

    is there a way to do different padding for different elements?

  • @cranjismcbasketball8113
    @cranjismcbasketball8113 9 месяцев назад

    Thanks for the video. Unfortunately, the link you provided to the code doesn't exist anymore

  • @Ownagin
    @Ownagin 2 года назад

    how about I just define my navbar height in a variable (let's stay --navbar-height) and have that same variable in my scroll-padding-top ?

  • @sakumi2941
    @sakumi2941 2 года назад

    what if i don't want to add a scroll padding to the whole document? Just one section

  • @RavianxReacts
    @RavianxReacts 10 месяцев назад

    My problem it doesn’t even nav to it or it does but it’s like 50 pixels away from the top

  • @re.liable
    @re.liable 2 года назад +1

    I thought this was going to be a video about those large af navbars in websites nowadays that take nearly half the screen and barely leaving anything for the scrollable content 😂

  • @outpost31737
    @outpost31737 2 года назад

    I use vanilla JS to solve this problem simply by executing a piece of code that calculates the height of the header (ScrollTop).