DEV Community

alexpaper
alexpaper

Posted on

1 1

📏 Resize Observer API

Resize Observer interface reports changes to the dimensions of an HTML elements .

How to use

It is very simple to use and in some special cases, can be used as an javascript alternative to CSS @media query.

You need to create a new ResizeObserver instance

const observer = new ResizeObserver(entries =>{
    // console.log(entries[0]);
    ...
});
Enter fullscreen mode Exit fullscreen mode

then you need to *observe * the HTML element, in this case the main container

observer.observe(container);
Enter fullscreen mode Exit fullscreen mode

Now whenever the container size changes, the observer returns an object with the new width and hight values,
values that can be used to manipulate elements within the page.

In this video you can see a simple example

have a good day đź‘‹

Top comments (0)

AWS Q Developer image

Build your favorite retro game with Amazon Q Developer CLI in the Challenge & win a T-shirt!

Feeling nostalgic? Build Games Challenge is your chance to recreate your favorite retro arcade style game using Amazon Q Developer’s agentic coding experience in the command line interface, Q Developer CLI.

Participate Now

đź‘‹ Kindness is contagious

Discover fresh viewpoints in this insightful post, supported by our vibrant DEV Community. Every developer’s experience matters—add your thoughts and help us grow together.

A simple “thank you” can uplift the author and spark new discussions—leave yours below!

On DEV, knowledge-sharing connects us and drives innovation. Found this useful? A quick note of appreciation makes a real impact.

Okay