How to align images in card with dynamic title?

Several times it is needed to display information on a website in the form of cards. Cards are basically a component containing a heading, an image, and an optional description. The way in which we align the images, headings, and text in our cards define the aesthetics, that is, the look and feel of the card.

However, there might be problems in designing such a card when the title of the card may dynamically change  and also contain an image that should be displayed properly even when the title is of different lengths.

This problem can be solved using CSS flexbox with its related properties to align the title and the image when creating the card. In the below example we create a card with a heading and an image that can handle dynamic changes in the title.

Example:

html

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" 
        content="width=device-width, 
                 initial-scale=1.0">
  <title>
    Dynamic Title with Cards
  </title>
  <style>
    body {
      background-color: #0f9d58;
    }
  
    /* Style the wrapper that would be
    around all the cards */
    .wrapper {
      width: 100%;
      display: flex;
      flex-wrap: wrap;
    }
  
    /* Style the card itself */
    .card {
      display: flex;
      flex-direction: column;
      text-align: center;
      color: #0f9d58;
      background-color: white;
      width: 20%;
      margin-right: 10px;
      padding-bottom: 10px;
    }
  
      
    /* Use flexbox on the title text */
    .title {
      flex: 1;
    }
  
    /* Use flexbox on the heading
    of the card */
    .heading {
      display: flex;
      flex: 1;
      flex-direction: column;
    }
  
    /* Style the heading of the card */
    .card h1 {
      padding-bottom: 5px;
      border-bottom: rgb(140, 216, 140) solid 3px;
    }
  
    /* Style the image of the card */  
    .card img {
      height: 90%;
      width: 50%;
      margin: 2%;
    }
  
  </style>
</head>
<body>
  <div class="wrapper">
    <div class="card">
      <div class="heading">
        <div class="title">
          <h1>A Short Heading</h1>
        </div>
        <div class="image">
          <img src=
        </div>
      </div>
    </div>
    <div class="card">
      <div class="heading">
        <div class="title">
          <h1>A Very Very Long Heading for the Card</h1>
        </div>
        <div class="image">
          <img src=
        </div>
      </div>
    </div>
    <div class="card">
      <div class="heading">
        <div class="title">
          <h1>A Medium Heading</h1>
        </div>
        <div class="image">
          <img src=
        </div>
      </div>
    </div>
  </div>
</body>
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.