Learn Resize Observer In 5 Minutes

Поделиться
HTML-код
  • Опубликовано: 7 июн 2024
  • Resize Observer is an amazing tool in JavaScript that allows you to easily and efficiently do complex media query style calculations. What makes resize observer especially powerful is the fact that it can do things media queries cannot do such as container queries.
    📚 Materials/References:
    Resize Observer Article: blog.webdevsimplified.com/202...
    🌎 Find Me Here:
    My Blog: blog.webdevsimplified.com
    My Courses: courses.webdevsimplified.com
    Patreon: / webdevsimplified
    Twitter: / devsimplified
    Discord: / discord
    GitHub: github.com/WebDevSimplified
    CodePen: codepen.io/WebDevSimplified
    ⏱️ Timestamps:
    00:00 - Introduction
    00:25 - Why Use Resize Observer
    01:10 - Resize Observer Basics
    03:40 - Resize Observer Example
    #ResizeObserver #WDS #JavaScript

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

  • @UmarAlFarooq
    @UmarAlFarooq 2 года назад +97

    What I LOVE about your videos:
    1. You jump right into the topic. No wasting time.
    2. No sponsor or ads
    3. You don't keep telling your viewers to "like and subscribe" which wastes MORE time multiplied by the number of viewers
    4. Each second of your video is important. You don't have any pauses, non-content or even lame jokes.
    5. Audio, video and production quality is top notch.
    6. Your content is innovative, cutting edge and extremely helpful!
    YOU sir, deserve a Nobel prize instead of most of the jokers who usually get it. I hope all other content creators learn these things from you. I hope that all viewers also take notes and comment in other videos to be more like you.

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

      I'm gonna offer a different perspective, because most people fail to realize how RUclips works.
      1. No comments here
      2. Some people work using RUclips. Their income comes from their videos and RUclips's ad revenue is negligible for small channels.
      3. RUclips themselves instructs creators to "call to action", and therefore rely on this metric to promote the video, which in turn gives more views and more ad revenue for RUclips of course, but also for the channel. So it's a waste of time for you personally, but for the creator it's a necessary measure to stay relevant (also, what the hell do you mean be "multiplied by the number of users"? We don't have a collective maximum of time we all share. There's no more time wasted per person, what the hell?)
      4, 5 and 6. No comments here.
      Closing comments: you claim others are "jokers" because they don't make the styles of videos you like. It's okay to dislike. There's even a button for doing that. It's NOT okay to bash on those just because you don't like that.
      So in a nutshell, you frame your compliments to Kyle by critiquing others in a narrow minded way which not only doesn't help anyone, but is also really rude. For the sake of others, please refrain from looking at people as objects of your entertainment and start looking at them as individuals, which they are.

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

      @@patriciaverso the number of seconds you waste in the video is multiplied by the number of viewers. If you waste 5 seconds and a million viewers watch your video, you've effectively wasted 2 months of humanity's time.
      If everyone is "calling to action" it becomes a cheap trend rather than something essential. Try this experiment. Have videos with the same content in two different channels, with the only difference being that in one you pester the viewers to "like, subscribe, alert". See which channel gets more viewers.
      What's insulting and rude is treating viewers like children or senile people who need to be reminded constantly. There's not a single regular viewer who doesn't understand the "like" button or the subscribe. If you sum up all the videos that constantly badger people with this time wasting exercise every day across RUclips, that's billions of hours, essentially killing the equivalent of 10 human beings who'd live to be a hundred.
      What I refer to as jokers are people like Yitzhak Rabbin and Yasser Arafat and Obama getting Nobel prizes. The "Drone President" who took the 2 wars he inherited and turned them into 7 wars gets a peace prize.

  • @TheRealCAPerry
    @TheRealCAPerry 2 года назад +77

    It'll be interesting to see how this technique compares with Container Queries once they become available.

  • @adebiyial
    @adebiyial 2 года назад +172

    I don't think mentioning media queries in this context works, at least for me. Resize observer and media query are fundamentally two different things and work differently. In fact, if a media query can do it, I'd take that first. Anyway, nice and concise video - thank you.

    • @JNeathawk
      @JNeathawk 2 года назад +5

      agreed. a CSS solution is almost always better than a similar JS solution. Only time I would pick JS over CSS is if it absolutely needs to do or look the exact way it does in JS and I can't achieve it with CSS. I am never surprised at how much JS can slow things down when it's used willy nilly.

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

      Can be vary useful when adding attributes to an element via JavaScript if the width reaches a certain point.

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

      ​@@JNeathawk Totally agree. Maybe you won't notice it on a simple static website, but doing stuff in js when you can easily handle it in css is a performance killer.

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

      My thoughts exactly! The thumbnail and first minute of the video made me think, "is this guy really going to use an observer in place of a media query for the page?" Still, I'm glad to have learned about this as I've never messed with it in 5 years. I've used the more complicated mutation observer, and admittedly, my naivety would have made me write longer JS to do the same thing.

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

      Well, there are times where you'll actually need to make a component responsive, say you have the same component on two different page, in page 1 it takes the full screen width, but in page 2, it takes half the page, it'll be much better to make such component responsive based on the width and height of that component instead of the width and height of the screen.

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

    Dude, nice. Glad to hear the performance is good with this approach.

  • @Paninicus
    @Paninicus 2 года назад +6

    Amazing how Kyle keeps bringing up JS/HTML features I haven't heard of (after being a web developer since 1996).

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

    Hey Kyle, in your each video I am learning something new. Thanks a lot and please keep sharing such a knowledge in future as well 😊

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

    Good video. I like how you focus on one concept and leave other details out for the viewer to learn more about if they need to. Keep it up!

  • @billyfairbank
    @billyfairbank 2 года назад +29

    Cleanest explanation of this I have seen. Been a dev for 15 years and learned a couple new things. Well done!

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

    This is a banger Kyle🔥 thanks man for this awesome info🙌🏼

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

    One of my Favorite RUclipsr ! Thank you for sharing your knowledge!😊✌️

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

    Amazing video! Thank you Kyle and have an amazing new year!

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

    Thank God this is in my recommended videos...I needed this so bad thanks Kyle.

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

    Thank you, for still showing something new I didn't know

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

    Thank you, Kyle, many of your videos helped me a lot, this was as always excellent.

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

    You really save my life for this video.
    Thank you so much

  • @alvaroprietovideos
    @alvaroprietovideos 2 года назад +38

    I have been using this technique extensively in some of my projects, specially in those where certain reusable components can be used in very different context, allowing them to fit into their containers smoothly. Each reusable component gets a special sizing class according to the available space (xxl, xl, and so on...). Resize Observer can be combined with Mutation Observer to increase reusability of componentes.

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

      How does it perform when multiple elements have the observer attached?

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

    Hi Kyle thanks for this outstanding video!!, waiting for ur e-commerce website tutorial😀

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

    Your channel is the best dev channel on youtube

  • @j-lewis
    @j-lewis 8 месяцев назад

    You're da MAN, thanks for such a good video! Clear & fix info in here!

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

    Well explained! Big fan. Thx Kyle

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

    That's why we are waiting for Container Queries, can't wait to see it in action!

  • @djibrilm__-
    @djibrilm__- Год назад

    Everytime I watch your videos i get something new 🙂 thanks

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

    Very informative - thank you so much!

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

    Understood this concept only after watching this video. Thanks!

  • @bobdinitto
    @bobdinitto 2 года назад +5

    Wow! This is amazingly powerful since you can customize the sensitivity of the scene by modifying the granularity of the observable object network, since it's fully programmable. I'm not used to being able to do stuff like this with HTML. Thanks for the succinct explanation.

  • @RodrigoMendoza7
    @RodrigoMendoza7 2 года назад +57

    This is what the upcoming Container Queries in CSS are for, right? So as to accomplish the same but avoiding using JS.

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

      Yes.

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

      Yea, it will be pure CSS and full power.

    • @psilocybin-
      @psilocybin- 2 года назад

      Exactly.

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

      Pure CSS will be the way to go for this for the majority of use cases. This implementation (while handy, always happy to have another tool) feels bloated to me given what it accomplishes.

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

      Just gotta wait 4 years for full cross-browser compatibility.

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

    Nice. Can’t wait to use this.

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

    I didn't know about these. Thanks.

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

    Wow! Thanks Kyle! Will try that out as soon as tomorrow ;)

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

    Thank you again for the knowledge you are sharing

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

    thank you so much for your work!

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

    This is cool, thanks for sharing !!

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

    I can't tell you how subscribed I am to this channel.

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

    Really cool video! 👏👏👏

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

    Cool technique! I have been using CSS all the time until I saw your video :D

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

    Thanks, very helpful

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

    Wow, this is great!

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

    Great!! Thank you kyle

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

    You make the web simplified!!

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

    Great video. How do you manage the performance with a defer?

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

    thank you now we have container queries

  • @estebanfelipe3980
    @estebanfelipe3980 3 месяца назад

    You are the besttt! 🙌

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

    Bruh I just needed this 2 days ago, Pog

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

    This technique is incredibly handy, especially in scenarios where you need to dynamically adjust the dimensions of an HTML canvas based on changes in the height of a parent element, it's a lifesaver

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

    Thanks Kyle!

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

    Thx Web Dev, good content

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

    Gonna use this in my upcoming projects am sure

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

    Um dos melhores canais de trickster js do mundo.

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

    love this!

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

    Pretty cool video ❤

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

    Last time I used contenteditable, I ran into snags right away. Sure, you can edit the text in a contexteditable div. But my users had other plans. They thought they could cut and paste text from other web pages into my contenteditable div. And they could. But what you don't see when doing that is that if you cut text from a webpage, and paste it into a CE div, it cuts and pastes not just the text but in fact ALL of the HTML that they highlighted when selecting that text. I ended up with html tags inside my CE div, which, of course do not show up (they're html tags), but the *effect* of them was felt immediately. So, heads-up about CE elements.

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

    I can't thank you enough, Kyle

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

    Awesome video. Would be great if you make a video about how to use it with React.

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

    Something's it feels like your videos are just for me. Like the Intersection Observer video I used to to build a scroll spy, then this I need something to show mobile and desktop preview and this is the solution.

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

    VERY NICE!

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

    Awesome sir

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

    very helpful thx

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

    Will we able to do something similar with container queries (if they become supported) ?

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

    Awesome. 👏👏👏

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

    hi there! thanks for the tutorial I wanted to know which screen recorder do you use! thanks in advance!

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

      Most likely OBS (Open Broadcaster Software), most streamers use it, you can use it to stream or record, very powerful and free.

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

    awesome !

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

    Thanks Kyle

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

    Great little tool. Could add a whole new functionality to a website.

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

    Thank u😍

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

    It depends on the use case. If you have to adapt to exact size, or change of overflow state (to sync width between two zones), then ResizeObserver is the way to go.
    If you’re adapting to size *zones* to change certain settings, then container queries.

  • @Vlad-em1bx
    @Vlad-em1bx 2 года назад

    Nice!

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

    superb

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

    So this demo shows a single element with a Class - can you address what would happen both functionally and especially performance-wise with a BUNCH of elements with the same class? Is there a point at 10 or 100 or 1000 elements where the array gets out of control, making using that class selector just count-productive? And thanks for these vids!

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

    Does this affect performance when used extensively?

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

    Any suggestions on how to resize the height of an based on the content inside the ?

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

    It's interesting. But what about the performance if used so many element checks?

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

    Hey Kyle. Thanks.

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

    This was epic

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

    I really hate media queries so this video makes me happy

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

    Very cool. I don't know right know the benefit... But I never know... Thanks

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

    You are awesome and the guitar behind you is fantastic btw can you play heavy metalcore riff

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

    Would this be a good solution for a horizontal menu that needs to dynamically hide/show overflow items as the menu container changes size? I've been using a window resize trigger, but sometimes the offsetWidth of the menu elements returns 0 instead of their actual width. I'm sure adding up the offsetWidths of all the elements inside the box and seeing if the exceed the container width is a terrible way to do what I'm trying to do but I'm fairly new to front end development as I've been a back end dev my whole career and our front end guy just quit and now I've inherited his job in addition to my own. :\

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

      Without more context on the code and the problem, it sounds like this horizontal menu is always present and stretched to the size of the page, in which case I would think media queries would be the best approach. Good luck!

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

    Yeah you are the best one

  • @2009Spread
    @2009Spread 2 года назад +1

    Why do I see the error in the console "TypeError: Failed to execute 'observe' on 'ResizeObserver': parameter 1 is not of type 'Element'."?

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

    Wait a minute. Can't we just listen to resize events on elements? Or does that only work on the "window"?

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

    Resize observer seems very handy but is it compatible across all browsers? and does it affect the website's performance like the "resize" event?

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

    can I use this resize observer to work with the height property?

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

    Could you show us how to keep the height of columns in a flex container regardless the number of lines of text in them? Thanks

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

    One gotcha I ran into with Resize Observer, is that it doesn't fire for display:inline elements when you resize the window and the height/width changes due to the number of lines of text changing.

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

    awesome

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

    How do you open the dev tools on another window???? Love your vids!!!

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

      Assuming chrome, open dev tools, at the right in the first bar the 3 vertical dots icon, first entry Dock side you can have your tools in a new window (first option) left, bottom, right :)

  • @a-ezzat5677
    @a-ezzat5677 10 месяцев назад

    you are my hero , i like the subjects you raised , if you explain a few about code written in complex scenarios, e.g."this code is to do bla bla , it's written here because bla bla" it would be more awesome in general not for that video, greetings from Egypt

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

    Good to know this is around but it’s hard to figure a practical use case for this in production.

  • @leaxem
    @leaxem 2 года назад +6

    Great video as always Kyle. Please do one for MutationObserver.

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

      I already have that video recorded and it should release in a week or so.

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

      @@WebDevSimplified You are the best bro.

  • @Euquila
    @Euquila 2 года назад +5

    I wonder if we should introduce some kind of polling behaviour in our stream (switchMap?) so that we don't get so many callback calls as the component resizes

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

    Nice

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

    GoodJob!

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

    Hey Kyle, quick question, did you have to go through an algorithms interview for your full stack dev role?

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

      I can answer this as I just got a full-stack role. For me, no. My technical interview was split into Javascript, NodeJs+Database, TypeScript, and React.
      Let me add that I had to pass a full-stack take-home assignment first.

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

      @@hn031 Thank you so much! And congrats on the role!

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

    Doesn't addEventlistener('resize", callback) do the same thing?

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

    Can't wait for wider adoption of container queries

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

    wow... nice hair style :) and nice video :)

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

    can we do the same by putting an if statement on the elem.getBoundingClientRect().width and change the color base of the return value

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

      well that is going to be very inconvenient and non performant, as you will need a loop to periodically check the size of your object, this is better because it acts like a listener

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

    Can you help me to resize column width in my page? I've try many way, but it doesn't work.

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

    How can we do this for different device types?

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

    Hey Kyle, isn’t this what container queries are going to be?