How can I resize an image with JavaScript

I’m new to web development.
I have a webpage in PHP in which there is an image.
I want to post this photo on a blog with this script

<div id="x"></div>

var img = document.createElement("img");
img.src = "";
var src = document.getElementById("x");

how can I resize the image and put a link for the blog visitor to return to the image page?

16 thoughts on “How can I resize an image with JavaScript”

  1. Resize the image using = 50%; and = 50%; this will resize the images width and height by 50%. You can also use pixels or other size increments in CSS using

    As for creating a link, if you want to use the image as a link, you could create an a tag and wrap it around the image controlling the size of image in that element. Then add the href attribute pointing to the page you wish to open when the user clicks the image.

    let div = document.getElementById("x");
    let aTag = document.createElement("a");
    aTag.href = '';
    aTag.title = 'click to open image'; = '_blank';
    aTag.outline = 'none';
    let img = document.createElement("img");
    img.src = ""; = '40%'; = '40%';
    <div id="x"></div>

Leave a Comment