What is the best method for creating uniform cards with different amounts of text that won't cause overflow issues?

At the moment, I have a container that acts as a boundary for a group of cards:

.cards-container {
    display: flex;
    align-items: stretch;
    justify-content: center;
    flex-wrap: wrap;
    margin-top: 20px;
    border: 5px solid violet;
}
.card {
    margin: 10px;
    transition: all 0.4s;
    width: 25em;
    /* height: 25em; */
    border: 5px solid black;
}

In my React code, I render them on the page like this:

<div className={classes['cards-container']}>
    {
        articlesData.length > 0 ?
        articlesData.map( (article) => 
        <div key={article.id} className={classes.card}>
            <Card 
                itle={article.title}
                body={article.abstract}
                />
        </div>
        ) :
        null
    }
</div>

The Card component has the following CSS styles:

.mycard {
    background-color: white;
    display: block;
    padding: 1rem;
    border-radius: .25rem;
    /* box-shadow: 0 2px 8px 0 rgba(0,0,0,0.9); */
    border: 5px solid red;
}

.mycard-body {
    font-size: 0.7rem;
}

.mycard-header {
    /* font-size: 1.5rem; */
    font-size: 1rem;
    margin-bottom: .5rem;
}

.mycard-footer {
    margin-top: 1rem;
}

Lastly, here's how the card component is defined:

const Card = (props) => {
    
    const mainText = props.body.slice(0,300);

    const [textBody, setTextBody] = useState(mainText + '...');

    

    const onSeeMore = () => {
        setTextBody(props.body);
    }

    return <>
        <div className={classes.mycard}>
            <div className={classes['mycard-header']}>
                {props.title}
            </div>
            <div className={classes['mycard-body']}>
                {textBody}
            </div>
            <div className={classes['mycard-footer']}>
                <button type='button' onClick={onSeeMore}>See More.</button>
            </div> 
        </div>
    </>
};

The text within these cards can vary, causing some to not fill the entire box assigned to a card, resulting in irregular sizes like shown in the image below: https://i.stack.imgur.com/UOsGz.png

I require the uniform bounding box for consistency in transitions and effects when hovering over a card. How can I ensure that the cards maintain the same size but accommodate varying content lengths?

Thank you for any assistance provided.

Answer №1

Make sure to apply your transitions, such as background-color: white, to the .card elements instead of the .mycard. This will ensure that they have the same height.

For an example, check out: https://example.com/codepen/sample

Answer №2

If you happen to encounter a similar issue, here's the workaround that worked for me:

I passed the background color of the card from the parent component using the color prop. Then, in the Card component, I customized the styling as shown below:

In the parent component,
const cardColor = 'white'
<Card 
   title={article.title}
   body={article.abstract}
   color={cardColor} 
/>

In Card.js:

<div className={classes.mycard} style={{'backgroundColor': props.color}}>

To limit the text within the header and body ('mycard-header', 'mycard-body') to a specific number of lines, I used the following CSS:

.mycard-header {
    font-size: 1rem;
    margin-bottom: .5rem;
    line-height: 1rem;
    width: 100%;
    height: 2.2rem;

    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
}

.mycard-body {
    font-size: 0.7rem;
    -webkit-line-clamp: 7;
    -webkit-box-orient: vertical;

    overflow: hidden;
    text-overflow: ellipsis;

    display: -webkit-box;
}

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

The issue of the keyboard disappearing on Chrome for Android while switching between different input

Issue with Input Switching on Chrome for Android I am facing difficulty when trying to switch between two input fields. The HTML code is as follows: <input type="text"/> <input type="text"/> When I enter text in the first input and click on ...

Creating a personalized Select component with MUI in React: A step-by-step guide

I'm currently working on a front-end task that involves creating a Select menu in React + TypeScript using Material UI (MUI). The menu should allow users to choose from different period options such as the last 7 days, 15 days, etc. The challenge I a ...

The slider feature is malfunctioning on Internet Explorer version 8

Hello everyone, I am facing an issue with my website located at: http://210.48.94.218/~printabl/ When viewed in IE 8, the slider is not functioning properly (it is showing collapsed between the menu and product images section) The theme used for my site ...

What is the best way to retrieve the values of all the keys in Firebase Realtime Database?

At first, I only retrieved the values associated with each key. However, upon further reflection, I realized that I actually need to obtain the keys themselves and then access the corresponding values. I have now commented out my previous approach. https: ...

What steps can be taken to customize this code in order to develop a dictation application?

Currently, I have a code that functions by comparing two strings: str2 (which represents user input) and str1 (our reference string). The purpose is to determine whether any words in str2 are spelled correctly or incorrectly. So far, the code works well. H ...

Tips for restraining browser history navigation in Next.js after using router.push

Currently, I am working on developing a web app using Next.js. I have a confirmation page that is displayed when the user clicks the confirm button. Upon clicking the confirm button, I use the nextjs router.push method to navigate to the index page. Howe ...

The inline variables in CSS transitions are failing to be detected

The CSS transitions are not recognizing the inline variables, or if they are, they are not receiving the correct information. Below is an illustration of how the inline variables are defined in the HTML: <p class="hidden" style="--order ...

Troubleshooting: React Js Template Missing When Using npx create-react-app with <app name>

I'm starting a new React project with the command: npx create-react-app <app name> But I'm encountering an error saying that no template was provided. I tried uninstalling create-react-app globally using: npx uninstall -g create-react-app ...

When adding margin-left and margin-right, images do not appear in their designated positions

I have a chart displaying images, which are showing up correctly. However, I am facing an issue when I try to add some spacing to the chart by using margin-left and margin-right. Here is the CSS code I included: #chart1 { margin: 0 auto; ...

Ensure that both tables contain columns of equal size

I am facing a challenge with 2 HTML tables that are positioned directly on top of each other. Each table has the same number of columns, however, the text within them may vary. Additionally, both tables can consist of multiple rows. My goal is to ensure th ...

Can a DjangoREST+React project be deployed on a single Google Cloud App Engine instance?

My setup includes a Django backend that interacts with the React.js frontend through REST APIs created with the DjangoREST Framework. In past projects, I've always used separate gcloud projects/app engine instances for Django and React. However, this ...

Emmet snippet for React JSX files in Visual Studio Code

Struggling to make Emmet snippets work in my VS Code for JSX files. I'm trying to get the 'div' shorthand to expand into a complete div element when writing JSX HTML code, but no matter what I do, I can't seem to get it to work. I even ...

Challenges with displaying content in Bootstrap tabs

I am currently working on the following code: HTML <div class="card"> <div class="card-header"> <ul class="nav nav-tabs card-header-tabs" role="tablist" id="tabs_list"> <li class="nav-item"> <a class="nav-li ...

Filtering data in an antd table by searching

Just starting out with React hooks, specifically using TypeScript, and I'm struggling to implement a search filter with two parameters. Currently, the search filter is only working with one parameter which is 'receiver?.name?'. However, I wo ...

Implementing a PHP/HTML caching system is a crucial step in optimizing website

Having delved into various guides about implementing a PHP cache system for my custom-coded, query-heavy, and expanding site, one resource I found useful is this one: While I grasp the concept, there are specific parts of my page that cannot be cached. Wh ...

Dropdown menu similar to the one utilized in Bootstrap

I am curious to understand how the drop-down menu system in Bootstrap3's tutorial page operates. It seems that it collapses all subtopics under a main topic by default, allowing users to either click or scroll to expand further. Additionally, the menu ...

"Utilizing Bootstrap for a Dynamic Display: Implementing Multiple Carousels with Multiple Images within

I have incorporated several Bootstrap carousels on one page, each with its own unique identifier. These carousels are generated dynamically based on user interactions, such as uploading images. My goal is to display 3 images at once in each carousel. I am ...

Email Form Application: Utilizing NodeJs and Express - Error: URL Not Found /

I'm encountering a "cannot GET" error whenever I try to run my application on a live server using VS Code. My assumption is that the issue lies within my routing configuration, but I'm struggling to identify the exact problem. Any assistance woul ...

Place a div element immediately following a table row in the HTML document

Welcome to my hotel and cabin availability page! I've created a PHP query that displays the available hotels and cabins in a table format. However, I want to enhance the user experience by displaying a Google Maps location and some pictures of each ho ...

Incomplete Rotation CSS Animation

I am attempting to create a horizontal spinning image using only CSS3. Check out my webpage here: You can find the CSS file here: www.csupomona.edu/~lannguyen/ISSM_WEB/css/main.css The code for calling the spin effect is at the top, with the actual imp ...