How to align images on a card with a dynamic title in Javascript?


We can align images in a card by using CSS to set the position and margins of the image within the card container. We can also use Flexbox or Grid to align the image and title in a specific way. By using dynamic title, we can change the text displayed in the card title based on user input or data from a database.

Approach

  • Start by creating a container for your card. This can be a div or a section element.

  • Within this container, add a div or a header element to hold your dynamic title. Make sure to give it a unique class or ID so you can style it later.

  • Next, add your image element within the card container. You can use the img tag or a background image in a div.

  • Use CSS to align your image and title within the card container. You can use the display: flex property to align the items horizontally or vertically.

  • To center the image and title, you can use the justify-content and align-items properties to center them both horizontally and vertically.

  • If you want to add a border or other styling to the card, you can use the border and background properties to customize its appearance.

  • To update the title dynamically, you can use JavaScript to change the innerHTML or textContent of the title element.

  • Finally, test your card to ensure that the image and title are aligned correctly and that the title updates dynamically as desired.

Example

Here is a complete working example of a card with a dynamic title and an aligned image −

<!DOCTYPE html>
<html>
<head>
   <title>Align Images</title>
   <style>
      .card-container {
         display: flex;
         flex-wrap: wrap;
         align-items: center;
         justify-content: center;
         border: 1px solid #ccc;
         padding: 10px;
         margin: 10px;
      }
      .card-title {
         font-size: 24px;
         font-weight: bold;
         text-align: center;
         margin-bottom: 10px;
      }
      img {
         max-width: 100%;
      }
   </style>
</head>
   <body>
      <div class="card-container">
      <header class="card-title">My Title</header>
      <img src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__340.jpg" class="card-image">
      </div>
      <script>
         let title = document.querySelector('.card-title');
         let newTitle = "New Dynamic Title";
         title.innerHTML = newTitle;
         title.style.color = 'black';
      </script>
   </body>
</html>

Explanation

This example creates a container div with the class "card-container" which contains a header element for the title and an image element. It uses CSS to align the elements within the container, center the elements, and add a border and padding to the container. It also uses JavaScript to change the inner HTML of the title element to update it dynamically.

Updated on: 13-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements