Resizable HTML Elements with Pure Javascript: A How-To Guide

If you want to make your HTML elements resizable, there are several ways to achieve this. One of the most common approaches is to use a plugin or framework. However, if you prefer to do it in pure Javascript, there are also some options available.

The first step is to create a function that will handle the resizing. This function should take the element you want to resize as an argument, and then add event listeners for the mouse down, mouse move, and mouse up events. During the mouse move event, you can update the size of the element based on the distance the mouse has moved, and during the mouse up event, you can remove the event listeners.

Here's an example of what the code might look like:

function makeResizable(element) {
  const handle = document.createElement('div');
  handle.className = 'resize-handle';

  handle.addEventListener('mousedown', function(event) {
    document.addEventListener('mousemove', resize);
    document.addEventListener('mouseup', stopResize);

  function resize(event) { = (event.clientX - element.offsetLeft) + 'px'; = (event.clientY - element.offsetTop) + 'px';

  function stopResize() {
    document.removeEventListener('mousemove', resize);
    document.removeEventListener('mouseup', stopResize);


In this example, we create a resize handle element and add it to the element we want to resize. We then add event listeners for the mouse down event on the resize handle, and use the mouse move event to update the size of the element based on the distance the mouse has moved. Finally, we remove the event listeners during the mouse up event.

By using this approach, you can make your HTML elements resizable using pure Javascript. Just be sure to test your code thoroughly to ensure it works as expected.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information