DEV Community

Cover image for Angular 2+ dynamic input width 📏
Răzvan Stătescu
Răzvan Stătescu

Posted on

Angular 2+ dynamic input width 📏

I've recently had a project where I had to dynamically adjust the width of an input, based on the content size.

It might sound hard to do, but in reality is very simple. We achieve this by taking advantage of the size attribute.

We start by creating a new Angular directive using ng g directive [path]

After that, we write our directive as follows:

Now, you can use it like this: <input type="text" dynamic-input />

Top comments (1)

Collapse
 
kottartillsalu profile image
Maxime

This doesn't work if the input has an initial value.