Implementing CSS to effectively scale images within a fixed box size

I am attempting to create a collection of images with rounded borders that will expand on hover, while staying within a specified box size. I want the effect to give the illusion that the image is only zooming inside the box and not physically enlarging on the webpage. Despite trying various methods, I have been unsuccessful in achieving this desired outcome. Any guidance or advice would be greatly appreciated.

https://i.sstatic.net/xnOIN.jpg

     .foto{
       overflow: hidden;
       max-width: 100%;
       border-radius: 25px;
    
    }
    .popis{
       position: absolute;
       bottom: 5px;
       left: 50px;   
    }
    .popis h3{
      font-weight: 600;
      color: white;
    }
    main img{
       filter: brightness(60%);
       width: 100%;
       overflow: hidden;
       border-radius: 25px;
       -webkit-transform: scale(0.9);
       -ms-transform: scale(0.9);
       transform: scale(0.9);
       transition: transform 0.5s, opacity 0.5s;
    }
    main img:hover{
       opacity: 1;
       -webkit-transform: scale(1);
       -ms-transform: scale(1);
       transform: scale(1);
       transition: transform 0.5s, opacity 0.5s;
    }
    <main role="main" class="container">

        <div class="row" id="nprace">
            <div class="col-lg-12 col-md-12 text-center nadpis">
                <h2>Lorem ipsum</h2>
            </div>
            <div class="row">
                <div class="foto col-lg-4 col-md-4">

                    <img src="https://i.sstatic.net/XZ4V5.jpg">
                    <div class="popis">
                        <h3>Hagendaz</h3>
                    </div>
                </div>
                <div class="foto col-lg-4 col-md-4">

                    <img src="img/hagendaz.jpg">
                    <div class="popis">
                        <h3>Hagendaz</h3>
                    </div>
                </div>
                <div class="foto col-lg-4 col-md-4">

                    <img src="img/hagendaz.jpg">
                    <div class="popis">
                        <h3>Hagendaz</h3>
                    </div>
                </div>
                <div class="foto col-lg-4 col-md-4">
                    <img src="img/hagendaz.jpg">
                    <div class="popis">
                        <h3>Hagendaz</h3>
                    </div>
                </div>
                <div class="foto col-lg-8 col-md-8">
                    <h3>Text</h3>
                    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus porttitor turpis ac leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Fusce nibh. Aenean vel massa quis mauris vehicula lacinia. Nulla quis diam. Etiam quis </p>
                </div>
                <div class="foto col-lg-4 col-md-4">
                    <img src="img/hagendaz.jpg">
                    <div class="popis">
                        <h3>Hagendaz</h3>
                    </div>
                </div>
                <div class="foto col-lg-4 col-md-4">
                    <img src="img/hagendaz.jpg">
                    <div class="popis">
                        <h3>Hagendaz</h3>
                    </div>

                </div>
                <div class="foto col-lg-4 col-md-4">
                    <img src="img/hagendaz.jpg">
                    <div class="popis">
                        <h3>Hagendaz</h3>
                    </div>
                </div>
            </div>
        </div>

    </main>

Answer №1

Alright, let's give this a shot

To center your image when zooming in, simply adjust the margins on hover to remove any difference. This will create a centered effect.

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <style type="text/css">
        img{
            height: 300px;
            width:300px;
            transition: 0.5s; /*Include transition for smooth zoom effect */
            cursor: pointer; 
            border-radius: 25px;
        }
        img:hover{ 
            margin-left: -20px; /*Adjust margins to keep it centered while zooming (e.g., -20px) */
            margin-right: -20px; /*Adjust margins to keep it centered while zooming (e.g., -20px)*/ 
            margin-top: -20px; /*Adjust margins to keep it centered while zooming (e.g., -20px)*/
            margin-bottom: -20px; /*Adjust margins to keep it centered while zooming (e.g., -20px)*/
            height: 320px; /*Resize image on hover*/
            width:320px; /*Resize image on hover*/
        }
        #imgWrapper{
            height: 300px;
            width:300px;
            margin-left: auto;
            margin-right: auto;
            overflow: hidden; /*Ensure wrapper content does not overflow */
            text-align: center;
            border-radius: 25px;
        }
    </style>
</head>
<body>
    <div id="imgWrapper">
        <img src="https://jessehouwing.net/content/images/size/w2000/2018/07/stackoverflow-1.png">
    </div>
</body>
</html>

Answer №2

Almost there, make sure to check the feedback.

.image{
       overflow: hidden;
       max-width: 60%; /* Adjusted for better visibility */
       border-radius: 25px;
       width: 20rem;
       padding: 1rem
    }
    .description{
       position: absolute;
       bottom: 5px;
       left: 50px;   
    }
    .description h3{
      font-weight: 600;
      color: white;
    }
    main img{
       filter: brightness(60%);
       width: 100%;
       overflow: hidden;
       border-radius: 25px;
       transform: scale(1.1); /* Resetting back */
       transition: transform 0.5s, opacity 0.5s;
    }
    main img:hover{
       opacity: 1;
       transform: scale(1.2, 1.2); /* Increase both X and Y transforms by 40% */
       transform-origin: center center; /* Transform origin at center */
       transition: transform 0.5s, opacity 0.5s;
    }
<main role="main" class="container">

        <div class="row" id="work">
            <div class="col-lg-12 col-md-12 text-center title">
                <h2>Lorem ipsum</h2>
            </div>
            <div class="row">
                <div class="image">

                    <img src="https://i.sstatic.net/XZ4V5.jpg">

                </div>
                <div class="image">

                    <img src="https://i.sstatic.net/XZ4V5.jpg">

                </div>
                <div class="image">

                    <img src="https://i.sstatic.net/XZ4V5.jpg">
                    <div class="description">
                        <h3>Hagendaz</h3>
                    </div>
                </div>
                <div class="image">
                    <img src="https://i.sstatic.net/XZ4V5.jpg">
                    <div class="description">
                        <h3>Hagendaz</h3>
                    </div>
                </div>
                <div class="image">
                    <h3>Text</h3>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus porttitor turpis ac leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Fusce nibh. Aenean vel massa quis mauris vehicula lacinia. Nulla quis diam. Etiam quis </p>
                </div>
            </div>
        </div>
    </main>

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

Repairing a CSS file for Internet Explorer versions 7, 8, and 9

Does anyone have experience with troubleshooting HTML5 CSS3 layouts across different browsers? I recently created a website layout from scratch that looks perfect in Firefox 5, Safari 5.1 and Chrome 12 on Mac. However, when using tools like Adobe BrowserL ...

Designing a drop-down menu that appears visually above its parent

As I design a menu positioned at the bottom using position:absolute; bottom:0, I want the submenu items to align perfectly above their parent. Each li should appear above the ul itself. The trick to achieving this is by applying a negative margin. However ...

What is the best way to conceal part of a chart or a div in Angular?

I have a large chart that I need to showcase on my website, but due to its size it would take up too much vertical space and potentially overwhelm the user upon their first visit. My goal is to provide a preview of the chart, similar to an excerpt of text ...

Envelop the phrases onto a fresh line

I'm having trouble getting the text inside a div to display correctly. When the text is too long, it just keeps stretching instead of breaking into new lines. How can I make sure that the content displays as a block within the div? Thank you. .box ...

Display an image with HTML

My current project involves creating a chrome extension that will display a box over an image when hovered, while also zooming the image to 1.5 times its original size. In my research, I came across a similar example. .zoomin img { height: 200px; wi ...

Display identical text using JavaScript filter

My search filter highlight is currently displaying [object Object] instead of <mark>match values</mark> when replacing the values. This is the code I am using: this.countries.response.filter((val) => { const position = val.value.toLowerCa ...

Unable to achieve full width for a div within a scrollable container

Currently, I am in the process of constructing a new website using Gatsby.js. However, I have encountered a minor issue with some CSS related to code blocks. Since CSS is not my strong suit, I have been unable to resolve it thus far. Below is a runnable sn ...

What are some alternatives to using iframes?

Currently, I am working on a project for a client where I need to display multiple websites on a single page in a browser. To achieve this, I initially used the iframe element, but encountered an issue with the openerp frame. Despite setting up the openerp ...

I noticed a significant space below the footer on my webpage. As a first-time website creator, are there any solutions to this

I'm currently in the process of working on my very first website for a study project using HTML and CSS. There's an issue where there is a large gap below the footer that I can't seem to eliminate. I've only completed 3 pages so far, bu ...

Show the res.send() method from express.js without replacing the existing html content

Currently, I am in the process of developing a calculator application using express.js. The app needs to handle get requests for an HTML file and post requests that capture user input and provide the response accurately. My challenge lies in showcasing the ...

Is it feasible to capture a screenshot of a URL by using html2canvas?

Is it possible to take a screenshot of a specific URL using html2canvas? For example, if I have the following URLs: mydomain.com/home mydomain.com/home?id=2 mydomain.com/home/2 How can I capture and display the screenshot image on another page? window ...

What could be causing the partial transparency in my SVG mask?

I have been working on an interesting SVG code that creates a rectangle with a cutout circle. However, I am facing an issue where the non-cutout part appears to be partially transparent. https://i.sstatic.net/1PLHL.gif Can anyone provide me with guidance ...

JavaScript query-string encoding

Can someone clarify why encodeURI and encodeURIComponent encode spaces as hex values, while other encodings use the plus sign? I must be overlooking something. Appreciate any insights! ...

The content on the right side is spilling over my footer and causing

I am struggling to understand why the content in the right column is overlapping my footer. I believe it has something to do with a float, but I can't seem to pinpoint the exact issue. Could you please take a look at this page for me and provide some ...

Is there a way to update components in Angular without affecting the current URL?

I want to update a component without changing the URL. For example, I have a component called register. When I visit my website at www.myweb.com, I would like to register by clicking on sign up. How can I display the register component without altering the ...

Setting a constant width for text characters across all devices using CSS and HTML

I am looking to display text in my textarea with the same width in pixels across all devices. Essentially, I want to set the character width in pixels so that it appears consistently on any device (Desktop/Mobile). <html> <head> <styl ...

Having trouble connecting local stylesheet on server?

Currently, I am developing an HTML-based game with Node.js, focusing on enhancing the front-end interface. While trying to include a CSS stylesheet, I encountered an unusual issue. The structure of my folders is organized as follows: C:\Users\m ...

Is there anyone who can provide a comprehensive explanation for what is going on here?

{ // Let's figure out how to launch my HTML file as a webpage in Chrome. "version": "0.2.0", "configurations": [ { "type": "pwa-chrome", &q ...

Creating an inclusive h1 header with a logo: A step-by-step guide

Is there a way to have a logo linked to the homepage and also have an h1 element that is invisible? This is my current layout: <header id="pageHeader"> <h1> <a href="<?php bloginfo('url'); ?>"> & ...

The issue with the page not appearing correctly on IE9 is currently being resolved

I am utilizing bootstrap alongside a bootswatch theme and HTML5 boilerplate to design a webpage. While it appears correctly on Chrome and Firefox, the display is off on IE9 and IE8 mode. However, everything works fine when compatibility mode is enabled in ...