What is the best way to align individual images in the middle of their respective background containers?

As a novice coder working on a dice rolling app using React, I've hit a roadblock with a CSS problem that's proving to be quite challenging. My goal is to have a row of 6 dice, each enclosed in a background box that changes color when the React state changes. While I have the React functionality in place, aligning the background boxes correctly has been a struggle.

I'm not well-versed in CSS and my main focus is on learning React/Redux rather than diving deep into CSS intricacies. The issues I'm facing with the code are quite baffling to me, particularly the usage of 'display: flex'. Any insights on why my current code isn't producing the desired effect would be highly valuable to me.

Below is the snippet of my code along with a screenshot showcasing the issue:

#css
.diceContainer {
  display: flex;
  justify-content:center;
  padding:3vw;
  margin-right: 4%;
}

.diceContainer img {
  height: 70px;
  width: 70px;
  padding: 15px 0 0 15px;
  margin-right: .5px;
  filter: drop-shadow(0 0 3px);
}

.dice_background_active {
  background: linear-gradient(#5F8EC9,#2255BE) 0 0 / 80px 80px no-repeat;
}
#jsx
 <div className="diceContainer">
       
          <div className={this.state.oneSelected ? "dice_background" : "dice_background_active"}>
          <img onClick={this.handleOnClick.bind(this, 1)}  src="img1"
           
          /></div>

          <div className={this.state.twoSelected ? "dice_background" : "dice_background_active"}>
          <img onClick={this.handleOnClick.bind(this, 2)}  src="img2"
            
          /></div>

          <div className={this.state.threeSelected ? "dice_background" : "dice_background_active"}>
           <img onClick={this.handleOnClick.bind(this, 3)}  src="img3"
          /></div>

          <div className={this.state.fourSelected ? "dice_background" : "dice_background_active"}>
           <img onClick={this.handleOnClick.bind(this, 4)}  src="img4"
            /></div>

          <div className={this.state.fiveSelected ? "dice_background" : "dice_background_active"}>
           <img onClick={this.handleOnClick.bind(this, 5)}  src="img5"
            /></div>

          <div className={this.state.sixSelected ? "dice_background" : "dice_background_active"}>
           <img onClick={this.handleOnClick.bind(this, 6)}  src="img6"
            />
            
        </div>

</div>

https://i.sstatic.net/JMQ3Q.png

Answer №1

It was an easy solution to this issue.

.diceContainer img {
  height: 70px;
  width: 70px;
  padding: 15px 0 0 15px;
  margin-right: .5px;
  filter: drop-shadow(0 0 3px);
}

The misalignment was due to the way the padding was set up. I simply adjusted it to

 padding: 4px 4px 4px 4px;

https://i.sstatic.net/4pz6b.png

A big thank you to @G-Cyrillus, @Drew Reese and @Shanie for guiding me in the right direction.

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

Is there a way to create a new perspective for Ion-Popover?

Looking for a solution: <ion-grid *ngIf="headerService.showSearch()"> <ion-row id="searchbar" class="main-searchbar ion-align-items-center"> <ion-col size="11"> ...

What are the best practices for creating documentation in ReactJS?

I need to generate documentation in a .doc file format for each component defined in our ReactJS application. I am seeking an npm package that can assist with this process by extracting code and comments from my components and converting them into docume ...

NextJS getStaticProps failing to pass fetched data to the page component

Struggling with passing props from getStaticProps function into template pages in NextJS. Below is the code for the dynamic page template that I am using. // src/pages/blog/[slug].js import React from 'react'; import matter from 'gray-matte ...

The table borders in IE 11 do not display properly when using Html5 and CSS

In a previous version, the table had visible borders. However, when I added the property text-align:center; to my container, the table did not align to the center. I then tried adding display:inline-block; to the table, which successfully centered it. It l ...

Disable automatic playback of HTML video

There is an HTML video with an image that loads initially and then disappears to play the video. I would like the image to always be visible until I click on it. Once clicked, the video should start playing. You can view the code on JSFiddle: http://jsf ...

Sleek dialog sliding animation with Svelte

I'm struggling with a svelte component that I have and I'm trying to implement a slide down animation when it closes. The slide up animation is functioning correctly, but for some reason the slide down animation is not working. Does anyone have a ...

The :not() selector does not currently have the capability to support complex selectors within it

Combining the CSS3 :not() selector with the attribute contains selector can be a challenge, as it seems to not work properly. Here is an example of the markup: <input type='text'/> <input type='text' id="dasd_Date_asd"/> ...

caption sliding into the incorrect div box

As I continue my journey of learning CSS, I've encountered a puzzling issue involving slide-in captions within a different div element. The problem arises when I try to customize the appearance of the caption to better fit the overall design of my web ...

Is there a way to completely remove all styling from a specific child element?

Struggling with a drop-down menu issue - the parent element has a border, but I don't want that style to be inherited by the child sub-menu. Despite my attempts to remove the border using border: 0px, it's not working as expected. Is there any wa ...

Is there a way to receive real-time updates from a Firestore document in ReactJS?

Having an issue with real-time updates: I'm working with a firestore document to display data on the screen. Everything works perfectly when retrieving and displaying the data initially. However, I am struggling to figure out how to get new data auto ...

Position the div to the right of a left navigation bar instead of beneath it using Bootstrap 5

After using the code snippet from , I'm struggling to align content to the right of the navigation bar. It seems like a simple issue related to changes in Bootstrap 5 that I can't seem to figure out. <div class="d-flex flex-column vh-100 ...

What are some ways to enhance the speed of swipe and scrolling on mobile devices using Javascript, CSS, and HTML?

Dealing with slow scrolling on iPhones has been a challenge for me. The application was developed using Vue.js. I was able to increase the scrolling speed on desktop using Javascript, but unfortunately, it doesn't translate well to mobile devices due ...

Trouble with `getStaticProps` and SWR: "Serialization error encountered in `.initialData.config.transformRequest[0]` provided by `getStaticProps`"

Currently, I am experimenting with implementing SWR in a Next.js environment. const Best = (props: InferGetStaticPropsType<typeof getStaticProps>) => { const { data } = useSWR('/best', apis.getBestProduct, { initialData: props.init ...

Implement Google Authentication for a system with client and server operating on separate ports using ReactJS and NodeJS

Currently, I am working on a project using different ports for the client and server. I am confused about handling events in the Client whenever a successful login occurs on the Server. I do not know how to process the login on the Client side as well. I a ...

Unable to fix the MongoDB/Express issue

I am encountering an issue with my React, Redux, and MongoDB application. I have a users collection where each user has a favorites array. I want to update this array with a patch request whenever a user adds a new favorite element. However, I keep receivi ...

Adjust the input value with CSS when hovering

Whenever I click on a button, the name of that button is displayed on the site. However, I am looking to change this button name when hovering over it but I am not sure how to do it. echo '<form id="button" method="POST" >&ap ...

Tips for setting up a popup menu when clicking a button on a webpage

I am currently working on developing a popup menu with a greyed-out background that appears when the user clicks on a button in React. My code implementation is as follows: // The ifButtonClicked function is called when another button is clicked // Some ...

Issue with parent-child communication in React.js causing malfunction

I am facing an issue with maintaining state between two different JavaScript files using React. The parent class, break.js, is defined as follows: export default function AlertDialog(props) { const [demoOpen, setDemoOpen] = React.useState(true); ...

The slideToggle function in jQuery seems to have trouble working on a Joomla platform, however, it functions perfectly on jsbin

I'm currently working on a website and I'm trying to implement the jQuery slideToggle feature to move some blocks. Everything seems to be working fine when I test the code on jsbin.com, but for some reason, it's not functioning properly on m ...

Is there a way for me to increment the value of 'sessionStorage.fgattempt' whenever either 'fgMade()' or 'threeMade()' are called?

Currently, I am developing a basketball game stat tracker and need to update the field goal attempts every time I make a field goal or three-pointer. Additionally, I am looking for ways to optimize the javascript code provided. The main requirement is to ...