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

What causes delayed state updates in React/NextJS until another state is updated or a fast refresh occurs?

UPDATE: Version 13.3.0 is coming soon! In my code, I have a state variable named localArray that I need to update at a specific index. To achieve this, I decided to create a temporary array to make modifications and then set the state with the updated val ...

Using object bracket notation in TypeScript to retrieve values from props with the help of string interpolation

I encountered an issue in my code while attempting to utilize a variable within my TSX component. This problem arises due to the dynamic props being passed into the component, which are always a string that matches one of four keys in the "characters" obje ...

Utilize date-fns to style your dates

Struggling to properly format a date using the date-fns library. The example date I'm trying to work with is 2021-09-20T12:12:36.166584+02:00. What am I doing wrong and what is the correct approach? Here's the code snippet I have so far: import ...

What are some successful methods for displaying extensive data lists to users in a meaningful and organized manner?

From dropdown lists to hover menus with fly-out options, there are various ways to present content on a website... I am managing a comprehensive list of U.S. military bases across the world for my website. This list includes both active and inactive bases ...

React Weather App: difficulties entering specific latitude and longitude for API requests

I have successfully developed an app that displays the eight-day weather forecast for Los Angeles. My next objective is to modify the longitude and latitude in the API request. To achieve this, I added two input fields where users can enter long/lat values ...

How can one manage styling choices in R Markdown while creating a PDF document?

Currently, I am utilizing R Markdown within the RStudio platform to produce documentation. When selecting the "Knit HTML" option, my output appears satisfactory. The ability to modify styling options and apply CSS Files directly is a beneficial feature. ...

Efficiently adjusting the height of a sticky sidebar

I am currently implementing a Bootstrap grid with two divs in a row. I need my reply-container to be fixed (sticky). However, when setting position: fixed; it is affecting the element's width by adding some additional width. With position: sticky, set ...

I can't understand why my body width is so disproportionately larger than usual

https://i.stack.imgur.com/jbeyI.png Encountering a new issue now. The width of my body is unusually high and I can't seem to identify the cause. I have included the code below. Tried adjusting margins but no luck. Searched for solutions online wi ...

Color in the diamond shape only to a designated height

I am in search of a unique diamond shape that allows me to fill the color up to a specific height based on an attribute or variable provided. https://i.stack.imgur.com/62U6h.png. I attempted creating the diamond shape and initially considered placing it ...

"Border-radius becomes less prominent on pointer interaction in Chrome and Safari due to WebKit's behavior

Having an issue with a list inside a div. The problem arises when there is a div containing a list, which limits the visibility of items for users. If the limit is exceeded, a scroll bar appears inside the div to allow users to see more items. Additionally ...

Display user's name in navigation bar using asp.net mvc

When working on an ASP.NET MVC project, I wanted to include a short message for the user, such as 'Hello, username!' In the navigation bar, specifically when the user is signed in, I encountered some display issues with the standard bootstrap la ...

utilizing the active class with react js

Apologies for the question, but I am facing an issue where adding an active class to a button is affecting all buttons when clicked. How can this be fixed? Here is the code snippet: <div className=""> {category.items.length === 0 ? ( ...

Managing navigation tabs in React Material UI when there are no matches for the provided value

I'm currently working on a NavBar for my application that utilizes the Material UI v5 - Nav tabs component along with React Router v4. The issue arises when navigating to child pages like /about-us/john or /about-us/deborah, resulting in an error mess ...

I find the JSX syntax to be quite perplexing

While examining some code, I came across the following: const cardSource = { beginDrag(props) { return { text: props.text }; } }; When working with JSX block code or building objects, I usually use {}. The cardSource variable in this co ...

Does the <cite> tag go inside a link in HTML?

Whenever I include a citation in HTML using the <cite> tag, I typically place the link around the citation itself. For example: <a href="http://example.com/"><cite>Example Citation></cite></a> This method makes sense to m ...

Optimizing Your HTML/CSS/JavaScript Project: Key Strategies for Modular

When it comes to web frontend projects (html/css/javascript), they are often perceived as more complex to read and maintain compared to Java/C#/C/C++ projects. Is it possible to outline some optimal strategies for enhancing the readability, modularizatio ...

Using ajax to submit a form in CodeIgniter and displaying the returned data in a table view

When I submit a form on the view page, I want the form data to be saved in a database table and displayed without refreshing the page. Below is my view code: <div class="col-md-12"> <div class="row"> <div> <table class="table table-s ...

AngularJS 1: Dynamically updating input values in real-time

Hey everyone, I'm experimenting with Angular and have encountered a roadblock. I am trying to assign values to an input element using ng-repeat for a list of parameters. <input type="text" ng-model="parameters.inParameterName" class="form-control ...

Implementing a Scalable Application with React Redux, Thunk, and Saga

What sets Redux-Saga apart from Redux-Thunk? Can you explain the primary use of redux saga? What exactly is the objective of redux thunk? ...

Looking to incorporate nested rules in `JSS` using `CSS`?

Can someone guide me on how to use CSS class-nesting in Material-UI or JSS in general? I have been attempting the following: card: { cardHeader:{ marginTop:"30px" } } ...