Resize an image from the center while using top-left tags

Alright, let's tackle an issue.

Currently, there is an image nested within a div, and here's the corresponding CSS:

top: 420px; 
left: 517px;
position: absolute;
padding-top: 25px;
height: 60px;
width: 60px;

-webkit-transition: all 0.4s ease;
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-ms-transition: all 0.4s ease;
transition: all 0.4s ease;

Additionally, there's a :hover CSS effect to resize the image (creating an enlarging effect).

top: 415px;
left: 512px;
height: 75px;
width: 75px;

The current setup does the trick, but the real quandary lies in...

How can one determine the ideal top and left properties for triggering the enlargement from the center? It seems that reducing both values by 5px achieves the desired outcome (centered expansion). However, calculating these precise numbers becomes challenging, especially when dealing with larger images where the adjustments would differ. Is there a method to compute these dimensions accurately? Even though the logo may visually appear as if it's zooming perfectly from the center, lingering doubts persist regarding its exact alignment after observing it for some time.

Hoping my predicament makes sense.

Best Regards, Mike

Answer №1

If my interpretation of the question is correct, you're looking to evenly expand the image from its center when hovering over it. The mathematical calculation for this task is relatively straightforward. The goal is to ensure that the image expands proportionally in all directions.

Assuming the original image has a height of h, width of w, top position of t, and left position of l, and after enlargement the new height becomes h' and width becomes w', we can determine the new top (t') and left (l') positions using the following formulas:

t' = t - ((h' - h) / 2)
l' = l - ((w' - w) / 2)

To elaborate on this process, let's focus on the height. Essentially, we calculate the difference between the new height and the old height, then move the image vertically by half of that difference value both upwards and downwards. By dividing by 2 and subtracting this result from the original top position, we achieve the desired vertical expansion effect.

Answer №2

Let's break it down with a bit of math. Your image is expanding from 60px to 75px in both directions, resulting in an increase of 15px on each side. When we divide this by 2, we find that each side is growing by 7.5px. This value should be used to adjust your top and left properties accordingly.

The formula can be represented as: (newSize - originalSize) / 2

This calculation applies to both horizontal and vertical growth.

Check out this example for a visual representation. The demonstration features two images to illustrate the expansion happening symmetrically from the center. While there may be slight discrepancies due to CSS rounding decimals, the effect still presents itself as centered. Ultimately, only you will notice any minor deviations.

Updated CSS:

img {
    top: 120px; 
    left: 117px;
    position: absolute;
    padding-top: 25px;
    height: 60px;
    width: 60px;
    -webkit-transition: all 0.4s ease;
    -moz-transition: all 0.4s ease;
    -o-transition: all 0.4s ease;
    -ms-transition: all 0.4s ease;
    transition: all 0.4s ease;
}
img:hover{
    top: 112.5px;
    left: 109.5px;
    height: 75px;
    width: 75px;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Create two unique tooltips with different colors using only CSS

There are 2 tooltips that I want to style differently - one needs to be green, while the other should be red. I have tried various solutions, but so far both tooltips end up having the same color. $('.fa-edit').tooltip(); $('.fa-trash-alt ...

What is the correct way to accurately determine the width of a block being displayed with the flex-direction:column property?

For instance: HTML console.log($('.container').width()); // 600px as we defined in css. console.log($('.list').width()); // 600px console.log($('.box').eq('-1').position().left + $('.box').outerWidth( ...

Unconventional choice for website navigation bar

I'm encountering an issue with my navigation bar. I have implemented a script to fix its position at the top of the site, but the base position for the navigation bar is not at the top by default. To workaround this, I made it jump to the top when the ...

Unveil Secret Divs with a Click

I am in search of a way to display a hidden div when I click on a specific div, similar to the expanding images feature in Google's image search results. I have made progress with my limited knowledge of javascript, as shown in this CodePen: http://co ...

Having trouble with changing images or background images in JavaScript?

Encountering issues with dynamic elements. When I click a button, the following JS function is executed. It displays a stylish CSS alert upon postback and then runs the function. function scrollToTop(sender, args) { window.scrollTo(0, 0); document. ...

Interactive pop-up window featuring conversational chat boxes

Trying to create a comment box within a Modal dialog box that is half of the width. The comments in this box should be read-only and created using div tags. Attempted reducing the width and using col-xs-6, but ending up with columns spanning the entire w ...

Display or conceal a div element depending on the user's choice

I am looking to hide inactive divs, but whenever I reload the page, all tab contents merge into one. The screenshot below shows the issue I'm facing. Screenshot Below is the code snippet: $('.tab a').on('click', function ...

Combining an Image with a CanvasJS Graph and Generating a Downloadable Image of the Composite

I am attempting to combine an image (a background image for my graph) with my canvasJS chart. Once these elements have been merged on a canvas, I aim to obtain a DataURL of this canvas, enabling me to download an image of it (depicting the graph along wit ...

JavaScript form button press tracker

Hello! I've been tackling a challenge involving a JavaScript function to count button clicks. The catch is, the button type is set to submit, causing the page to reload every time I click it. Below is the snippet of code that contains the problemati ...

Can the bottom border on an input textfield be removed specifically under the new character?

This is the new visual design I received: https://i.stack.imgur.com/kiQGe.png The label CLG is represented as a label, with the line being an input type=tel. Disregard the purple overlay... The designer has requested that I remove the border when a user ...

CSS - overcoming the challenge of border not extending over floated left elements

I encountered an issue with the following code: <div style="border:3px solid #808080;"> <h1 style="text-transform: uppercase;font-size: 38px;color: #808080;text-align: center;">Lowell</h1> <div class="column-1"> &l ...

Tips for Improving the Naming Conventions of CSS Modules

I currently have the following setup in my webpack.config.js: { test: /\.css$/, use: [ {loader: "style-loader"}, { loader: "css-loader", options: { modules: true, importLoaders: 1, s ...

Issue with login form in IONIC: Form only functions after page is refreshed

Encountering an issue with my Ionic login form where the submit button gets disabled due to invalid form even when it's not, or sometimes displays a console error stating form is invalid along with null inputs. This problem seems to have surfaced afte ...

Aligning content in CSS for cross-browser compatibility with Internet Explorer and Chrome

My CSS grid has grid items that can easily be centered in Chrome by using the justify-items:center property, creating a magical effect. However, this solution does not work in Internet Explorer as the items remain stuck to the left side. For reference: ht ...

Is there a way to showcase the information contained within a JSON array on an HTML webpage?

Looking to incorporate the data from a Json array retrieved from an API into my HTML. What steps should I take to achieve this? Below is the Json array in question: { page: 2, per_page: 3, total: 12, total_pages: 4, data: [ { id: 4, ...

The iframe is not adjusting its size in relation to the parent window

For more information, click here: http://jsfiddle.net/5HYve/ <div style="position: fixed; top:0; right:0; width:300px; height: 100%; text-align:justify; overflow:hidden;" class="chat-container"> <iframe id="chat_1" style="width:300px;height:1 ...

What is the process for modifying the django password reset confirmation page?

As a fresh Django developer, my goal is to customize the django password_reset_confirm.html page with a new HTML file called change_pwd.html. <!DOCTYPE html> {% load static %} <html lang="en"> <head> <meta http-equiv="Conte ...

Show the checked options retrieved from controller

I am facing an issue with displaying certain checkbox values from the controller. In order to properly save these values, I believe it would be best to declare them in the controller and then use them in the HTML. However, my current code is not successful ...

What are the steps to setting up my custom index.html as the main landing page for a Wordpress site?

Currently, I am in the process of creating a portfolio website for a client. Utilizing the Semplice template on Wordpress seemed like a good option, but unfortunately, it lacks some customization features that I require. As a result, I have taken matters i ...

Issue with Leaflet.js: GeoJSON layer obscured by tilelayer, causing interactivity problems

I am currently working on a leaflet.js project that involves a baselayer, a polygon geojson layer, and another tilelayer positioned above the geojson layer displaying placenames. I have managed to successfully display the placenames tilelayer above the geo ...