Tips on automatically centering an image within a div as it is resized

I'm currently working on a website that showcases cards featuring Discord users. Each card includes an avatar image within a div element, and when the user hovers over it, the image expands. However, I've noticed that as it expands, it shifts to the left side. To illustrate this issue, I have added a border to the card in the example below:

Upon hovering over the image, you'll notice that it moves closer to the left edge of the box rather than centering itself. Is there a way to make the expanded image self-center within the card? Alternatively, is it possible for the width of the box to increase proportionally along with the height as the image expands?

@import url('https://fonts.googleapis.com/css2?family=Quicksand&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Nunito:ital,wght@1,200&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@300&display=swap');
* {
  margin: 0;
  padding: 0;
}

body {
  background-color: black;
  margin: 0px;
}

#accounts {
  margin: 0px;
  padding: 0px;
  text-align: center;
}

.acc {
  display: inline-block;
  padding: 60px;
  border-style: solid;
  border-width: 1px;
  border-color: aqua;
  width: 200px;
}

.icons {
  border-radius: 50%;
  width: 200px;
  transition: 0.75s;
  object-fit: contain;
  overflow: hidden;
}

.icons:hover {
  width: 250px;
}

.nick {
  color: white;
  font-family: 'Quicksand', sans-serif;
  font-weight: 500;
  white-space: nowrap;
}

#nc1 {
  margin-right: 13px;
}

#nc2 {
  margin-left: 9px;
}

#sc-btn {
  background-color: black;
  display: flex;
  justify-content: center;
  text-align: center;
}

#sc-btn a:visited {
  text-decoration: none;
}

#sc-btn a {
  text-decoration: none;
  margin-right: 8px;
  border-radius: 30px;
}

#server-button {
  padding: 0;
  width: 250px;
  height: 50px;
  border-style: solid;
  border-width: 1.5px;
  border-radius: 30px;
  border-color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: 0.75s;
}

#sv-btn-text {
  font-weight: 100;
  font-size: 28px;
  font-family: 'Montserrat', sans-serif;
  color: white;
  margin: 0px;
  padding: 0px;
  transition: 0.75s;
}

#server-button:hover {
  background-color: white;
}

#server-button:hover #sv-btn-text {
  color: black;
}

Answer №1

Instead of utilizing the width attribute when hovering, consider using the transform: scale() attribute along with the transform-origin attribute set to the center. You have the flexibility to use pixels, percentages, or any other unit with the transform property. For demonstration purposes, a number was arbitrarily chosen. More information can be found here: https://developer.mozilla.org/en-US/docs/Web/CSS/transform

@import url('https://fonts.googleapis.com/css2?family=Quicksand&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Nunito:ital,wght@1,200&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@300&display=swap');

* {
    margin: 0;
    padding: 0;

}

body {
    background-color: black;
    margin: 0px;
}

.container {
  display: flex;
  flex-direction: row;
  justify-content: center;
  gap: 5px;
  padding: 5px;
  margin: 0 auto;
}

.accounts {
    margin: 0px;
    padding: 0px;
    text-align: center;
}

.acc {
    display: inline-block;
    padding: 60px;
    border-style: solid;
    border-width: 1px;
    border-color: aqua;
    width: 200px;
    height: 250px;
    margin: 0 0;
}

.icons {
    border-radius: 50%;
    width: 200px;
    transition: 0.75s;
    object-fit: contain;
    overflow: hidden;
}

.icons:hover {
    transform-origin: center;
    transform: scale(1.2);
    padding-bottom: 15px;
    }

.nick {
    color: white;
    font-family: 'Quicksand', sans-serif;
    font-weight: 500;
    white-space: nowrap;
}

#nc1 {
    margin-right: 13px;
}

#nc2 {
    margin-left: 9px;
}

#sc-btn {
    background-color: black;
    display: flex;
    justify-content: center;
    text-align: center;
}

#sc-btn a:visited {
    text-decoration: none;
}

#sc-btn a {
    text-decoration: none;
    margin-right: 8px;
    border-radius: 30px;
}


#server-button {   
    padding: 0;
    width: 250px;
    height: 50px;
    border-style: solid;
    border-width: 1.5px;
    border-radius: 30px;
    border-color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: 0.75s;
}

#sv-btn-text {
    font-weight: 100;
    font-size: 28px;
    font-family: 'Montserrat', sans-serif;
    color: white;
    margin: 0px;
    padding: 0px;
    transition: 0.75s;
}



#server-button:hover {
    background-color: white;
}

#server-button:hover #sv-btn-text {
    color: black;
}
<html lang="pt-BR">
    <head> 
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>$$$$$</title>
        <link rel="stylesheet" type="text/css" href="styles.css" media="screen">
        <link rel="icon" type="image/x-icon" href="./src/favicon.ico">
    </head>
    <body>
    <div class="container">
        <div class="accounts">
            <div class="acc" id="luiz">
                <a href="https://instagram.com/luizzz28_" target="_blank">
                    <img class="icons" src="https://images-ext-1.discordapp.net/external/YBEo2ls8xQBT9HVGHsYwkZm3dddiQNZ_njgmk7kHClc/%3Fsize%3D1024/https/cdn.discordapp.com/avatars/707608125825482894/7c8b1fc2a14f2b47122bb3ac5c5bedb1.png">
                </a>
                <h2 class="nick" id="nc1">luizzz#0001</h2>
            </div>
            </div>
       <div class="accounts">
            <div class="acc">
                <a href="https://instagram.com/dmn.juniorr" target="_blank">
                    <img class="icons" src="https://cdn.discordapp.com/avatars/799372092108832778/6788af224fd81da075b0b05f4e42d4a6.png?size=1024">
                </a>
                <h2 class="nick" id="nc2">juniorr ƉємƠη#5456</h2>
            </div>
        </div>
           </div>
        <section id="sc-btn">
            <a href="https://discord.gg/tcQhcVG">
                <div id="server-button">
                    <h1 id="sv-btn-text">demoninhos</h1>
                </div>
            </a>
        </section>

        <div id="lateral-menu"></div>  
    </body>
</html>

Answer №2

In regards to CSS:

background-image: background-posution: center;

When using an image tag:

<div align=center><img src=... style=width:50vw;></div>

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

React Material Design Cards for Responsive Layouts

Hi there, I am currently navigating my way through Material Design and attempting to ensure that the cards section is responsive by utilizing media queries and flex. However, I have encountered an issue where only a portion of the image is displayed when t ...

Trouble displaying certain HTML code and its output on the browser?

I am in the process of developing a user profile page. I have designed a form with various fields, but the code within the section is not displaying correctly in the browser. I attempted to inspect the elements, but the code within that section remains inv ...

What is the best method for loading multiple HTML files into a Div container?

Recently, I made the decision to improve the look of an online manual I have been working on for my company by incorporating Bootstrap. The manual is structured with a tree-view that contains titles linking to HTML files with information and CSS stylesheet ...

How can I showcase both a username and email address in a Material UI chip?

I'm currently using Material UI chip to show name and email next to each other. However, when the name is long, the email goes beyond the chip boundary. Here's my function that generates the chips: getGuestList() { let {guests} = this.sta ...

Circular CSS menu

What I'm Trying to Achieve: I am interested in developing a unique radial menu that includes interactive elements, such as the central image and the surrounding sections. It is imperative that the solution is compatible across all browsers. The examp ...

Enhance the bubble charts in Kendo UI Graph by adding dimension and depth to the bubbles for a more visually

Is there a way to create a relief effect on the bubbles in Kendo UI Bubble charts? Currently, all bubbles appear flat with the 15 provided themes. You can see an example here: It would be great to have a 3D-style option similar to the pie chart (Uniform s ...

Centering loaders within elements of an unordered list

I am working on a navbar that uses Bootstrap 3 and AngularJS. Within this navbar, I have an unordered list with li elements. Below is a snippet of my navbar: https://i.stack.imgur.com/o75Lp.png This is the code for my navbar: <div class="navbar-head ...

How to align scrolling images with their scroll origin - a step by step guide

Curious to know the name of the effect where images scroll in a different orientation than the page, creating a 2D appearance. Take a look at the Google Nexus website and scroll down - do you see the effect? What is this effect called? Is it created usin ...

The attempt to unserializing the configuration schema in Yii2 was unsuccessful

My brain feels scrambled... I am attempting to insert an image in HTML within Yii2. I retrieve it from the database and place it into the view file, but when I try to display it using HTML tags, an error is thrown. However, the image is being added perf ...

Link the parameters of two polymer elements within a repeat template

In this scenario, my goal is to securely bind attr and attr1. These two variables should be independent from the variable data, unlike other attributes in use. I want each iteration to have its own set of bound components, rather than sharing one data obje ...

Opening an HTML file in Eclipse is a simple process that can be

Q1) Is there a way to open this file in a browser for testing? Q2) Can I quickly open this in Chrome from Eclipse and test it? click here for image ...

The HTML component fails to acknowledge width settings

I seem to be having trouble identifying an issue or perhaps I am misunderstanding something (I am using daisyui + nx + tailwind css + angular). To simplify my problem, let's consider the following scenarios: In the first scenario, I have: <div cl ...

Tslint is notifying that JSX elements without any children must be self-closing to prevent errors [Error]

Recently, I encountered an issue while trying to build my solution using the command npm run build. The error message displayed was: JSX elements with no children must be self-closing. I came across a similar problem on Stack Overflow but unfortunately ...

.click function failing to trigger on dynamically loaded form

I'm facing an issue with a form that displays images from my database. After retrieving the filepaths, they are loaded dynamically into the form along with a "Delete" <button> for users to delete the image via AJAX. Although I can successfully ...

Ensuring the accuracy of user input in JavaScript, even after fixing a mistake and submitting again, will not cause the code to exit the else clause

I am currently working on a coupon code field that needs to accept a specific set of coupon keys. Below is the code I have written for validation. If a user enters an invalid key at first, they receive an alert saying "Invalid Key." However, if they corr ...

Chrome now supports clickable circular canvas corners

I have a unique setup with two canvases. I've customized them to be circular by utilizing the border-radius property. The second canvas is positioned perfectly within the boundaries of the first one using absolute positioning. This is where it gets e ...

Trouble with jQuery addClass function when trying to add to a specific object variable

I'm attempting to give an error class to an input textbox to inform the user that their input is invalid. Within the change event, I am referencing what seems to be the input field and storing it in a variable. However, calling addClass on the var ...

Show a grid layout featuring varying heights using HTML

I am trying to create a grid view for displaying images in an HTML document. I want each li tag to have a different height, but currently they are all the same height. Below is my HTML code: <div class="blog_main_midd_section"><ul> ...

Information backed by the HTML5 Local Storage feature

Is it possible to use a Local Storage object to store another Local Storage object? Thank you in advance. ...

Adjusting containers for optimal display during browser zoom and resize operations

Currently, I have designed a banner using a triangle and rectangle to overlay an image. However, when the browser is zoomed in, there seems to be a gap between these two shapes. I am looking for suggestions on how to better connect or approach this banner ...