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.