What is causing justify-content to not function properly in React?

Struggling to center two divs within a container using flexbox. Flex-wrap and align-items are working fine, but justify-content just won't cooperate. :(



      <div className="container">
        
        <div className="card-container">

          <h1>Computer</h1>
          <Card cardValue={sendCardToComputer} />
          <p>Points: {computerPoint}</p>
          <p>Round Won: {props.roundLost}</p>
          <br />

        </div>

        <hr />
      
       <div className="card-container">
          <h1>{props.player}</h1>
          <Card cardValue={sendCardToPlayer} />
          <p>Points: {playerPoint}</p>
          <p>Rounds Won: {props.roundWin}</p>
          <br />
        </div>
      </div>


CSS:


.container{
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content:center;
  /* padding: 30px; */
  height: 400px;
  width: 100%;
  background-color: gray;
}

.card-container{
  width: 200px;
  height: 300px;
  background-color: salmon;
  border: 2px solid black;

}
.card{
  width: 60px;
  height: 100px;
  border: 1.5px solid blue;
  border-radius: 8px;
  margin: auto;
}

Unable to get justify-content to work properly... Any suggestions on how to use it effectively in this scenario?

Answer №1

To fix the issue, simply remove the <hr> tag from the code. The hr element is set to display: flex, which is causing the boxes to be pushed to the sides of the screen. This additional flex element is disrupting the layout:

.container{
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content:center;
  /* padding: 30px; */
  height: 400px;
  width: 100%;
  background-color: gray;
}

.card-container{
  width: 200px;
  height: 300px;
  background-color: salmon;
  border: 2px solid black;

}
.card{
  width: 60px;
  height: 100px;
  border: 1.5px solid blue;
  border-radius: 8px;
  margin: auto;
}
<div class="container">
  <div class="card-container">
    <h1>Computer</h1>
    <Card cardValue={sendCardToComputer} />
    <p>Points: {computerPoint}</p>
    <p>Round Won: {props.roundLost}</p>
    <br />
  </div>

 <div class="card-container">
    <h1>{props.player}</h1>
    <Card cardValue={sendCardToPlayer} />
    <p>Points: {playerPoint}</p>
    <p>Rounds Won: {props.roundWin}</p>
    <br />
  </div>
</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

Is there a way to substitute all underscores in HTML tag IDs and classes with a hyphen symbol?

I recently realized that I used underscores instead of hyphens in some of my CSS class and id names. To correct this, I want to replace all underscores with hyphens using the sublime text replace feature (ctrl + H). Is there a way to change underscores to ...

Ways to incorporate a notification feature with an icon or image

I'm looking to create a visually dynamic icon/image similar to mobile device notifications, but primarily for desktop use. This image will be positioned before some text. For instance: https://i.sstatic.net/MHocy.pngSome Text This design features tw ...

What is the best way to center align an element vertically in my specific situation

Struggling to vertically align elements within a div? Here's a snippet of code that might help: Here's a sample of what you might have: html <div class="test"> <a href="#"><img src="test.png"/></a> <a href="# ...

It seems that an issue has occurred indicating that something is not defined in the current context

Hello everyone, I'm struggling with a problem that I just can't seem to figure out. I've tried multiple solutions but this error keeps popping up and now I'm completely lost. Can someone please help me resolve this issue? The code in q ...

What is the most effective method for showcasing HTML content in a recyclerView?

Within the recyclerView, I showcase comments that are stored in HTML format. My attempt to display the comments in a webView was successful, allowing me to use custom *.css, but it caused significant lag in the recyclerView when scrolling. Additionally, m ...

Refresh meta tags in a Next.js/React app

In the process of optimizing a website for SEO, I am facing the challenge of updating the meta description's content dynamically without using any third-party libraries. While I have successfully implemented the functionality, it currently requires a ...

Issue with Axios Get method: Data not displaying in table

Can someone assist me with displaying JSON data on my website? I am using an API with a security token, and everything seems to be working fine until I try to display the JSON data on my page. I can see the data in my console, but not on the actual page. ...

The html viewport size is altered by the mobile keyboard

One issue that arises when using percentage or viewport unit width and height for the <body> element is that the size of these elements will change when a mobile keyboard is invoked due to an input. I have extensively researched this problem without ...

When you utilize the overflow:hidden property in an inline-block list, you may notice some

Referring to this example: http://jsfiddle.net/CZk8L/4/ I'm puzzled by why the CSS style overflow:hidden is creating extra space at the bottom of the first li. Can anyone shed some light on this? This has been bothering me for hours, as I need the p ...

Spacing between table cells is only applied to the first row and footer

In an attempt to add spacing between the first row, second row, and footer of a table, I have experimented with cell spacing combined with border-collapse. However, the spacing is being applied all over the table instead of in specific areas. I am utilizin ...

Display the div without using javascript/jquery if the radio button is chosen

Could someone help me find a solution similar to the one mentioned here: If Radio Button is Selected Show Div I am specifically looking for a way to achieve this using only HTML and CSS, without involving JavaScript or jQuery. Any suggestions would be gre ...

Troubleshooting problem with Graphql API pagination

I am attempting to retrieve all repositories from a specific user. If the user has numerous repositories, pagination would be optimal. Upon observation, it seems that we can provide first and last properties. However, how can I fetch repositories between ...

Material UI Appbar is throwing an error with an invalid hook call

For my project, I needed to create a simple app bar, so I decided to use the code provided on the Material UI website. Here is the component's code that I used: import React from 'react'; import { fade, makeStyles } from '@material-ui/c ...

Looping CSS text animation with typing effect

I am struggling with a CSS code that types out text, but it only does so once and I need it to repeat infinitely. Despite adding animation-iteration-count: infinite;, it still doesn't work as intended. Another issue is that it slows down at the end. H ...

Unable to choose the specific div element within the container that has an inset shadow applied by utilizing Pseudo

It seems that I am encountering an issue when trying to select the div elements within a container that has a defined shadow using pseudo elements ::before OR ::after. Once the shadow is applied, the div elements become unselectable. Please refer to the c ...

How can you retrieve the value of a CSS file in Javascript and CSS?

Imagine there is an element with the class .selector. This class defines its style. Once the page has finished loading, some JavaScript code is executed - the specifics are not important. What matters is that the CSS properties have set the object's ...

What methods can I use to toggle between different divs using navigation buttons?

There are 4 divs on my webpage that I need to toggle between using the up and down arrows. Despite trying an if else statement, it doesn't work as intended. <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.c ...

Incorporating and modifying a component's aesthetics using styled-components: A comprehensive guide

My OverviewItem component has 2 props and is a styled-component. I want to change just one style on this component, which can be done by using the technique of styling any component. Does creating a wrapper component remain the only option for sharing st ...

What is the best method for retrieving data from a specific collection in MongoDB?

Within the code snippet below, on the 4th line, 'Messages' is the name of my MongoDB collection that I created in another file. When I attempt to retrieve data from this collection, no errors occur. However, when I specify the name of a differen ...

Improper layout on laptop screens with smaller dimensions

The design looks great on a 24-inch laptop, but on an 11-inch screen, the layout becomes distorted. The right column shifts down... Check out the layout here Below is my code: <div class="row" style="margin-left: 325px;"> <div class="span12 ...