Ways to adjust the positioning of antd card?

Is it possible to display my antd card in the center directly when a button is clicked? Currently, I have implemented the code to achieve this behavior but upon clicking the button, the card initially appears on the left before aligning itself in the center. How can I prevent this initial displacement and have the card displayed directly in the center on button click?

const [show, setShow] = useState(false);


<div>
<Button onClick={() => setShow(true)}>Hi</Button>
{show ? (

  <Col style={{ height: "750px", width: "100%" }}>
    <Card className="cardStyle">
      <p>Card title will come here!!!</p>
    </Card>
  </Col>
) : null}
</div>

Css:

.cardStyle {
flex-direction: row;
justify-content: center;
align-items: center;
width: 300px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

Could the issue be caused by existing css styles for other components?

Answer №1

To center your column and its contents, the key is to apply proper styling rather than relying on transform and top/left positioning which may not always yield consistent results.

Take a look at this codepen where I have reorganized your code by moving the column styling into the CSS and utilizing flex position:

.colStyle {
  height: 750px; 
  display: flex;
  justify-content: center;
  align-items: center;
}

.cardStyle {
  flex-direction: row;
  justify-content: center;
  align-items: center;
  width: 300px;
}

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

When using React.Children.toArray with arrays, the console continues to prompt for the key prop

Despite using the code React.Children.toArray(array.map(...)) in my Next.js MUI Project to fix the unique key error in the console, the problem persists. I've searched for a solution online but haven't found anything that works. Here's the s ...

Encountered an issue while using react-native-navigation after updating from react-native 0.59 to 0.61

Seeking assistance with react-native-navigation after upgrading from react59 to react61. Can anyone provide guidance on this issue? Additionally, a screenshot is available for reference https://i.sstatic.net/hxPbd.jpg ...

Exploring nested checkboxes in ReactJS using mapping techniques

I have a function that triggers child checkboxes once the main checkbox is checked, and all these checkboxes are mapped from JSON data. The main checkboxes (highest level) and all of their children checkboxes (2nd level) underneath them are shown on click ...

Unable to resolve host name for registry.npmjs.org on port 80

Hello everyone! I'm currently working on getting my angular library published on npm, but I seem to be running into an issue. When I try to log in, I encounter the following error: npm ERR! code EAI_AGAIN npm ERR! errno EAI_AGAIN npm ERR! request to h ...

What are the top recommendations for implementing typography in SCSS?

As I ponder the best approach to take, I find myself torn between two options and wondering what the 'best practice' in this situation might be. My objective is to implement a selector that removes the standard global margin of 20px that I have ...

Is it possible to duplicate the geometrical shape within the scene?

I'm currently experimenting with Three.js to recreate a flower as part of my learning process for transformations. Below is the code I used to generate the stamen, stem, and petals. My goal is to have multiple petals stemming from the same Petal mesh ...

Despite importing jQuery, the variable '$' cannot be located

Whenever I try to click the button labeled test, it doesn't do anything. However, an error message appears in the console debug indicating: Error: Unable to locate variable '$'. I suspect this might be a jQuery issue, even though I' ...

I am currently experiencing difficulties with react-navigation as I am unable to successfully pass a parameter through a nested navigation stack

Despite attempting all recommended methods to pass a parameter through a nested navigator in react-navigation, I have hit a dead end. Previous solutions and documentations did not yield results, and even chatGPT failed to provide assistance. So now, with h ...

Steps for generating a unique object with the field name as the key in MongoDB

Modifying the outcome of an aggregation query where the field name serves as the key in the database. I attempted the following: How to utilize field value as key name in Mongodb result However, it resulted in the subsequent error: MongoError: $array ...

The JQuery condition is failing to accurately recognize the span checkbox

I am facing an issue with checking the status of a span checkbox to see if it is checked or not. For some reason, I believe that the detection of the checkbox's status is not working correctly. Below is a simplified version of my current code: ...

What is the process for appending a value to an array of JSON objects?

I have a JSON array containing objects which I need to pass the values to the DataTables. [{ _id: '58a2b5941a9dfe3537aad540', Country: 'India', State: 'Andhra Pradesh', District: 'Guntur', Division: ...

What is the reason behind being able to use any prop name in a React function without explicitly mentioning it?

Currently, I'm delving into the world of mobX-state-tree, and in the tutorial code that I'm exploring, there is an interesting piece that caught my eye. const App = observer(props => ( <div> <button onClick={e => props.store. ...

Issue with DragControls in Three.js not detecting intersections

My current challenge involves setting up DragControls in my Three.js project. After researching online, I found that the process should be quite simple. The code I have written is as follows: let dragObjects = [obj] const dragControls = new DragControls(dr ...

Is there a way to connect the horizontal scrolling of a table header with the horizontal scrolling of a table body while keeping the vertical scrolling of the table body separate from the table header?

Looking for a solution that provides a fixed header and frozen pane within a table, similar to the example at . However, I need the header to scroll horizontally with the body of the table. Here is an example based on the aforementioned link. The problem ...

ERROR: Could not locate the 'ANDROID_HOME' environment variable

Solution: Eliminate platform using ionic platform rm android Modify permission with chmod 777 /path/to/update-notifier-cordova.json Add platform by using ionic platform add android The issue may be related to another project that was created, leading t ...

Enhancing an identity function with type hinting

I am looking to define a specification for a function that returns its input argument, regardless of its type. The crucial aspect here is to mirror the interface of the argument in the return value. For instance, when I type z. on line 6 as shown in the i ...

Learn the technique of swapping out a portion of a string with a value from an input field in real-time

My webpage has a form on the left side where users can input text, and on the right is some static text. I want the text on the right to update automatically as the user types in the input field on the left. In my HTML code, it looks something like this - ...

Creating a fixed sidebar that remains visible while scrolling in Next.js

Currently, I am faced with the challenge of implementing two components - a feed and a sidebar. The sidebar contains more content than it can display at once, so I want it to be able to overflow. My goal is to have the sidebar scroll along with the content ...

React - Incorrect use of hooks and class components

Understanding hooks as the opposite of a class component can be confusing. A simple line of code can trigger an error when trying to adapt it for use in a class component. Take, for example, this situation: .jsx files and functional components work seamles ...

Customize Bootstrap 5: Changing the default color scheme

Currently, I am utilizing Bootstrap 5 (v5.2.1) along with a form on my website. I am attempting to customize the default styles for form validation. Specifically, I want to modify the colored 'tick' or 'check' icon that is displayed wh ...