Thursday, July 4, 2024
HomeLanguagesJavascriptHow to align images in card with dynamic title?

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 defines 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




<!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</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>


Output:

 

HTML is the foundation of web pages and is used for webpage development by structuring websites and web apps. You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.

CSS is the foundation of web pages and is used for webpage development by styling websites and web apps. You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments