Ways to align antd Card at the middle of the Col component in a React application

I'm trying to position a card in the center of the screen using the code below:

import React from "react";
import "./index.css";
import { Card, Col } from "antd";

const App = () => (
  <Col style={{height: '500px'}}>
    <Card style={{marginTop: '70%'}}>
      <p>Card content Card content Card content</p>
    </Card>
  </Col>
);
export default App;

However, when I set the height of Col and add marginTop to the Card, the height increases by the same amount. I want the height to remain fixed as specified in Col. How can I achieve this?

Answer №1

The method for aligning columns within rows seems to be lacking. Even applying span={24} to the row still results in a block display. Adding flex doesn't help with vertically centering the Card within the Row.

Instead, I decided to stick with plain CSS Flexbox to structure the content layout.

  • Row
    • height: 500 ~ sets the height of the row
    • background: LightGrey ~ visually shows the boundaries of the Row
    • justify-content: center ~ horizontally centers the column
  • Col
    • display: flex ~ Enables Flexbox (default for Col is block)
    • background: yellow ~ visually displays the boundaries of the Col
    • align-items: center ~ vertically centers the Card
    • justify-content: center ~ horizontally centers the Card
    • flex: 1 ~ Expands to fill the width of the Row
  • Card
    • background: magenta ~ visually shows the boundaries of the Card

App.jsx

import "./styles.css";
import React from "react";
import { Card, Col, Row } from "antd";

const App = () => (
  <Row
    style={{
      height: 500,
      background: "LightGrey",
      justifyContent: "center"
    }}
  >
    <Col
      style={{
        display: "flex",
        background: "yellow",
        alignItems: "center",
        justifyContent: "center",
        flex: 1
      }}
    >
      <Card style={{ background: "magenta" }} title="Card title">
        <p>Card content, Card content, Card content</p>
      </Card>
    </Col>
  </Row>
);
export default App;

styles.css

*, *::before, *::after {
  box-sizing: border-box;
}

html, body, #root {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

Here's a link to a CodeSandbox that showcases the described layout.


Resources

If you want to learn more about Flexbox layout, check out:

These resources are also helpful:

Answer №2

To center something, you can use: top: 50%, left: 50%, translate-x: -50%, translate-y: -50%. This will position it in the middle of the screen.

If you want it centered within a column, you can apply display: flex to the column with justify-content: center and align-items: center.

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

Error: The function "utils.useControlled" was not caught as a TypeError

There was a problem encountered when trying to render the Autocomplete component from @material-ui/lab Here are the versions of material-ui being used: "@material-ui/core": "4.9.5", "@material-ui/lab": "4.0.0-alpha.45", ...

Utilizing Bootstrap exclusively within a designated container div

Incorporating bootstrap into my TYPO3 frontend plugin has been beneficial, but I am looking to apply it selectively within a specific div. This way, the class without_bootstrap remains unaffected by any changes made by the bootstrap classes. Consider this ...

Positioning the tiny rectangle containing the information icon within the larger horizontal rectangle

In the image attached, I am creating a horizontal rectangle.horizontal rectangle Using the following CSS, I am able to create a horizontal rectangle on my div: .Rectangle { width: 516px; height: 50px; border-radius: 4px; border: solid 1px rgba(78 ...

Is it possible to customize BOOTSTRAP using media queries in a CSS File?

1) To create a webpage, I relied on Bootstrap. 2) In the CSS FILE, I implemented the following media query to supersede the Bootstrap design. @media screen and (min-width: 500px){ body {color: #ccc;} } Despite this, the body color remains unchanged afte ...

What is the most effective method for retrieving data from an API and integrating it into a Redux container using Reselect within a ReactJS application

Although this question may seem familiar, I have yet to come across a satisfactory solution, especially when combined with reselect. Take a look at the following Redux container component: export default connect((state, props) => { return { ...p ...

PHP not compatible with Fancybox iframe

I'm facing a simple problem that I can't seem to figure out. I have a button that, when clicked, opens a fancybox with an iframe displaying a page from my website. This page includes a form for sending an email. Everything works fine except that ...

JavaScript - Interactive sidebar that expands and collapses upon mouseover, maintaining its state when navigating between different pages

I am currently developing a multi-page web application using HTML, CSS, JavaScript, and jQuery. My focus is on creating a sidebar component that expands and collapses when the user hovers over it or moves the mouse out. This sidebar contains links to vario ...

Ajax causing unreliable posts

I am facing an issue with sending and receiving data in my mobile application. I currently use the jquery $.post function, but it seems to be quite unreliable. Issue: Occasionally, about 1 out of 10 times, the POST request is sent successfully, but the ca ...

Unable to initiate npm/yarn

My journey to create a React app on Linux led me to use the following commands: npm install -g create-react-app create-react-app my_app_name npm start After executing these commands, I encountered the following message: A dilemma with the project depen ...

creating a responsive form with angular flex layout

We are currently utilizing the following library: https://github.com/angular/flex-layout Our current code for achieving fxlayout is as follows, however, it is not functioning correctly on mobile devices. <div fxLayout="column" id="width& ...

Tips for storing an image from an HTML file input element into a specific folder

One of the elements on my webpage is as follows: <input type="file" accept="image/*" id="fileAdd" name="image" size="30" /> I'm looking for a way to store the image selected by the user into a specific folder within the project. How can I ach ...

How to Integrate AngularJS into an HTML Document?

I have a specific goal in mind: to create a single HTML file without separate JavaScript files. While I know that it's possible to include JavaScript inline within HTML, I'm unsure if the same applies to AngularJS. I've attempted to integrat ...

Utilizing the toggle switch functionality in Django with a boolean field implementation

My work_experience model includes an "is_working" field which is set to true when a user is currently employed at a company. I am using a toggle switch on the front end and would like to update the boolean field value for "is_working" with a click. What lo ...

"npm is the go-to tool for managing client-side JavaScript code

As I delved into a document outlining the npm Developer Guide, a question sprang to mind. Is it within the realm of possibility to construct a web client application using javascript/css/html with npm at the helm? And if so, might there be examples avail ...

Performing bulk operations on all selected rows in a table using Angular 6

Within my Angular 6 web application, there is a table with checkboxes in each row. My goal is to be able to perform bulk actions on the selected rows, such as deleting them. One approach I considered was adding an isSelected boolean property to the data m ...

Ways to activate the input focus event within a method in Vue.js

With an input that has the following event implemented: <b-nput class="input" id="link" v-model="link" placeholder="link" @focus="$event.target.sel ...

Issue with Access-Control-Allow-Origin header encountered while interacting with Reddit's JSON API

Currently working on a React app that fetches data from the reddit JSON api. During development, I used the cors-anywhere demo assuming that CORS errors wouldn't appear in production. After researching CORS errors, I learned that they occur because t ...

Utilize the toggle feature by retrieving the dynamic ID from ng-repeat within the scope

Hey there! I'm currently working on a UI that contains multiple links, and when clicked, each link should toggle to display different data. The challenge I'm facing is that the data is being generated dynamically using ng-repeat. How can I ensure ...

Appearance template failing to load

After setting up a local website on a new IIS server 8, I encountered an issue where the style sheet was not loading properly. The master page is referencing the style sheet like this: href="/HeadOffice/Styles/HeadOfficeCalendar.css" For example: <l ...

Using React Hooks, implement the useEffect hook with the specified className

Why is updating classNames not possible on rerender? const [selected, setSelectedState] = useState(true); let className = "none"; useEffect(() => { className = "appointment-item " + (selected ? "selected" : ""); console.log(className ); }, [se ...