How can I ensure that the position of an image within a button remains fixed as the button's width expands upon hovering?

Whenever I hover over the button, it changes width as intended but the image shifts towards the center. My goal is to keep it fixed on the left so it doesn't shift when the button's width changes. Below is my React code:

<div>
        <button
          className={
            this.state.activecut === true
              ? "designstudiobuttonleftopactive"
              : "designstudiobuttonlefttop"
          }
          onClick={this.cutActiveHandler}
        >
          <img src={shirtlogo} id="cutlogo" alt="" />
        </button>
</div>

.designstudiobuttonlefttop {
 width: 60px;
 height: 60px;
 margin-top: 150px;
 display: block;
 border: 1px solid #574904;
 border-radius: 50%;
 background-color: white;
 color: #574904;
 text-align: center;
 margin-left: 50px;
 }

.designstudiobuttonlefttop:hover {
  background-color: #f2f2f2;
  color: #574904 !important;
  text-decoration: underline;
  transition-delay: 100ms !important;
  cursor: pointer !important;
  width: 200px;
  border-radius: 50px 50px 50px 50px;
}

#cutlogo {
  width: 50px;
  height: 50px;
  padding-right: 5px;
}

.designstudiobuttonleftopactive {
  color: #574904 !important;
  border: none;
  background-color: white;
  text-align: center;
  text-decoration: underline;
  cursor: pointer;
  margin-top: 100px;
  display: block;
  margin-left: 50px;
}

Answer №1

Experiment with switching text-align: center; to text-align: left; in both classes

Answer №2

I don't have much experience with React syntax, but one way to accomplish this is by placing a button inside an extra container within the same block.

There are actually various methods to achieve this without using an additional container. Check out an example below:

http://jsfiddle.net/wa2vht71/4/

Answer №3

Utilize the CSS property float: left to position it on the left side of the button.

.designstudiobuttonlefttop {
 width: 60px;
 height: 60px;
 margin-top: 150px;
 display: block;
 border: 1px solid #574904;
 border-radius: 50%;
 background-color: white;
 color: #574904;
 text-align: center;
 margin-left: 50px;
 }

.designstudiobuttonlefttop:hover {
  background-color: #f2f2f2;
  color: #574904 !important;
  text-decoration: underline;
  transition-delay: 100ms !important;
  cursor: pointer !important;
  width: 200px;
  border-radius: 50px 50px 50px 50px;
}

#cutlogo {
  width: 50px;
  height: 50px;
  padding-right: 5px;
}

.designstudiobuttonleftopactive {
  color: #574904 !important;
  border: none;
  background-color: white;
  text-align: center;
  text-decoration: underline;
  cursor: pointer;
  margin-top: 100px;
  display: block;
  margin-left: 50px;
}
<div>
        <button
          class="designstudiobuttonlefttop"
          onClick={this.cutActiveHandler}
        >
          <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT6vxI3cA6w8ocAr3KWNHdMmq5gkkhYSuacNYaFCrQxZmeBUo2m" id="cutlogo" alt="" width="40" height="40" style="float: left;"/>
        </button>
</div>

Answer №4

To ensure your image remains on the left side no matter the button width, utilize flexbox with the following code example:

<div class="button">
  <div class="imageContainer">
    <img src="url-to-your-image" />
  </div>
</div>

<style type="text/css">
    .button {
        display: flex;
        justify-content: flex-start;
        width: 100px;
    }
    .button:hover {
        width: 200px;
    }
    .button .imageContainer {
        height: 50px;
        width: 50px;
        align-self: center;
    }
    .imageContainer img {
        height: auto;
        width: 100%;
    }
</style>

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

Generate a link to download a music or video file

I have an HTML file containing both video and audio files. I want to link these files, such as MP3 or MP4, using the <a href> tag to allow users to download them. The video and audio files are stored in the same folder as my HTML file. I have attemp ...

Adjusting the heights of various divs as a percentage to fill the containing parent div

Is it possible for child divs to adjust their height automatically based on a percentage within a parent div with set max dimensions using CSS? HTML: <div id="parent"> <div id="top"></div> <div id="bottom"></div> < ...

I am struggling to move my dropdown list in HTML and CSS

I'm having trouble adjusting the position of a dropdown list on my website using CSS. Can someone help me move the dropdown list to the center? HTML Code: <header> <h1 id="my_cycle_head">MY CYCLE</h1> <ul id= ...

Tips for deactivating a text field once a selection drop-down option has been chosen

I have been searching for a while on the internet but I couldn't find what I was looking for. How can I disable a text field when a select drop down is selected with a value (not empty)? It would also be nice to toggle it. When there is no option se ...

Hovering over a dropdown menu results in the color not completely filling the background of the desired element

.nav-main { background-color:#09F; width:100%; height:100px; color:#FFF; line-height:40px; } .nav-main .logo{ float:left; padding: 40px 5px; font-size:1.4em; line-height: 20px; } .nav-main > ul { margin:0; ...

The rc-form package in npm is issuing a Warning for the getFieldDecorator method when `defaultValue` is not being used as an option

Currently, I am on the rc-form 2.4.8 version and I am utilizing the getFieldDecorator method in my codebase. However, an issue has arisen: Warning: defaultValue is not a valid property for getFieldDecorator; the correct use is to set value, so please uti ...

An image inside this stylishly framed photo with rounded corners

.a { clip-path: polygon(10% 0, 100% 0%, 100% 100%, 10% 100%,10% 60%, 0% 50%, 10% 40%); } .a { position: relative; width: 500px; height: 500px; background:url("https://cdn.pixabay.com/photo/2020/02/16/07/55/thailand-4852830_960_720.jpg"); border-radi ...

There don't seem to be any errors, but for some reason, the entries aren't getting inputted into

I have recently started learning PHP and MySQL, and I've been troubleshooting this issue for the past three days without any success. Despite no errors being displayed, the database does not receive any new query. Upon submitting the register.php page ...

Designing table rows to resemble full-width cards

Looking for a way to transform the rows of a table into full-width cards while maintaining the row layout? Here's what I have tried so far: tbody>tr { display: block; padding: 1rem 0.5rem 1rem 0.5rem; margin: 1.5rem; border: 1px solid rgba( ...

Acquire a handle on the object being hovered over in three.js using react

Currently, I am utilizing the react-three-fiber library to render a scene containing numerous smaller objects. My goal is to make all of these objects hoverable within the scene. Additionally, I aim to have access to the hovered objects so that I can manip ...

Align elements horizontally

Currently, I am developing a blog application using React, Material UI, and Tailwindcss. One issue I am facing is that each postcard in the grid has a different height, and I want all cards to have the same height while keeping all children aligned in the ...

"Creating a user-friendly interface design with two separate divs, each showcasing a

I am looking to create a UI/UX design that includes a logo and two menus, one for language change and one for navigation. https://i.sstatic.net/Tb6zc.png My current approach involves two div elements: 1. The first div contains the image and language men ...

Explore the versatility of jQuery by utilizing multiple objects in your POST and GET requests to PHP for enhanced

I am trying to send multiple arrays to a PHP page using jQuery and retrieve results, but I am encountering an issue where the array values are not being properly passed to the PHP page. Notice: Undefined index: $name Notice: Undefined index: $type Notice ...

Is optgroup malfunctioning in IE 10?

My main question is: does optgroup not function properly in IE? The Plnkr code works fine in Chrome but encounters issues in IE 10. I'm trying to find a solution that will work across both browsers. Is this a known problem? In Chrome, I can expand/co ...

The Challenge of Referencing Javascript Files (including jQuery)

Previously, I had the following code snippet: <head> <script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script> <script type="text/javascript"> var optPrompt = "- Select One -"; var subCats ...

What functions do the accelerometer, gyroscope, and picture-in-picture serve in the HTML code for embedding a YouTube video?

After successfully uploading a video on Youtube, you will be provided with the following HTML code for embedding: <iframe width="560" height="315" src="https://www.youtube.com/embed/UF8uR6Z6KLc" frameborder="0" allow="accelerometer; autoplay; encrypt ...

Is it feasible to programmatically define the onClick action for an element within a ReactNode?

Let's discuss a function called addAlert that adds messages to an array for display as React Bootstrap alerts. While most alerts are simple text, there's one that comes with an "undo the last action" link. The challenge is that when this "undo" l ...

What are the steps for applying a Bootstrap class to an element?

I keep encountering this error in the console: Uncaught DOMException: Failed to execute 'add' on 'DOMTokenList': The token provided ('si col-md-4') contains HTML space characters, which are not valid in tokens. Below is a ...

How can you customize gutter widths for specific rows in Bootstrap 3?

Currently, I am constructing my own website using Bootstrap 3 and have come across a challenge that I would like to address: My goal is to assign distinctive gutter widths to certain rows in my design. The remaining sections of the site will adhere to th ...

Only the last MenuItem onClick action in the Material-UI Menu component is triggered

A recent attempt was made to create a basic dropdown menu using MaterialUI(4.3.3)'s Menu and MenuItem components. However, there seems to be an issue with the onclick event on the MenuItem. When console.log(id) is triggered, each component echoes the ...