Jump to content

How to get the height of an <img>, divide by 2 and check if its greater than 200 pixel?


pstein

Recommended Posts

I want to perform the following operations for each element on a webpage:

1.) Get the height of it
2.) get 50% of the value
3.) check if the new height is still above 200 pixel
4.) set the new height (if necessary)

Something like

document().get(“img”).setHeight(max(currimg.getHeight() * 0.5,200));

The command above does nor exist. It should only demonstrate a near-to solution.

How would a real command look like?

Link to comment
Share on other sites

If you're comfortable with writing more than one line of code, the solution is really simple.

let img = document.querySelector("img"); // Get a reference to the image
let newHeight = img.offsetHeight * 0.5; // Get half of the image's height
if(newHeight < 200) newHeight = 200; // If the value is less than 200 then use 200 instead
img.style.height = newHeight + "px" // Assign a new height to the image

 

Link to comment
Share on other sites

  • 4 weeks later...
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Height Adjustment</title>
    <style>
        /* Basic styling for the images */
        img {
            display: block;
            margin: 10px auto;
            max-width: 100%;
            height: auto;
        }
        .container {
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Adjust Image Heights</h1>
        <img src="1.jpg" alt="Sample Image 1">
        <img src="2.png" alt="Sample Image 2">
        <img src="3.png" alt="Sample Image 3">
    </div>
<script>
// Function to adjust the height of each img element on the page
function adjustImageHeights() {
    // Select all img elements
    const images = document.querySelectorAll('img');

    images.forEach(image => {
        // Get the computed height of the img element in pixels
        let height = window.getComputedStyle(image).height;
        height = parseFloat(height); // Convert to a number

        // Calculate 50% of the height
        let newHeight = height * 0.5;

        // Cap the new height at a maximum of 200px, and ensure it's at least 200px
        if (newHeight > 200) {
            newHeight = 200;
        } else if (newHeight < 200) {
            newHeight = 200;
        }

        // Set the new height
        image.style.height = newHeight + 'px';
    });
}

// Run the function to adjust img element heights after page load
window.onload = adjustImageHeights;
</script>
</body>
</html>

This is a complete html sample that will check for all <img> elements on you page and return the image height to 200px.
If the 50% height of the original image is greater than 200px, then 200 px will be set as the new height if the image.

Link to comment
Share on other sites

The op only wants to adjust height to 200 IF it falls below 200 after being divided by 2, while the other img tag elements will use divided by 2 value.

Your method could easily be done by adding style height: 200px;

You only need this

if (newHeight < 200) {
            newHeight = 200;
        }
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...