The Cascading of Bootstrap Card Designs

Looking for some assistance with my TV Show Searcher project that is based on an API. The functionality is complete, but I'm struggling to get the Bootstrap cards to stack neatly without any empty space between them. I want it to resemble the image gallery layout of unsplash.com where images stack seamlessly regardless of size. Any help or advice would be greatly appreciated.

View Image Here

I have a single row div in which I dynamically add col-4 divs using JavaScript to display 4 TV shows per line based on the API data. Below are the HTML, CSS, and JS files containing the code snippet:

console.log('Connected')
const form = document.querySelector('#searchForm');
const imgs = document.querySelector('#imgsDisplay')
const search = document.querySelector('#searchDisplay')
const row = document.querySelector('#row')

form.addEventListener('submit',async function (e) {
    e.preventDefault();
    row.innerHTML='';
    const searchTerm = form.elements.query.value;
    const config = { params: { q: searchTerm } }
    const res = await axios.get(`http://api.tvmaze.com/search/shows`, config);
    search.textContent = form.elements.query.value
    makeImages(res.data)
    form.elements.query.value = '';
    
})


const makeImages = (shows) => {
    
    for (let result of shows) {
        if (result.show.image) {
            const col = document.createElement('div')
            col.classList.add('col-3', 'h-100')

            const imgDiv = document.createElement('div')
            imgDiv.classList.add('card', 'cardStyle', 'm-0')

            const img = document.createElement('IMG');
            img.classList.add('card-img-top')
            img.src = result.show.image.medium;


            const label = document.createElement('h5')
            label.textContent = result.show.name;
            label.classList.add("card-title")

            const text = document.createElement('p')
            text.classList.add('card-text')
            text.innerHTML = result.show.summary;

            const cardBody = document.createElement('div')
            cardBody.classList.add('card-body')

            const showSite = document.createElement('a')
            showSite.href = result.show.officialSite;
            showSite.textContent = 'Official Website'
            showSite.classList.add('btn', 'btn-outline-success')

            cardBody.append(label)
            cardBody.append(text);
            cardBody.append(showSite)

            imgDiv.append(img);
            imgDiv.append(cardBody);
            col.append(imgDiv);
            row.append(col);
            imgs.append(row);
        }
    }
}
#searchForm{
    width: 40%;
}

.cardStyle{
    width: 18rem;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TV Show Search</title>
    <link rel="stylesheet" href="app.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

</head>

<body>

    <section>
        <div class="container d-flex flex-column align-items-center">
            <h1 class="display-1">TV Show Search</h1>
            <form id="searchForm">
                <div id="inputForm" class="container input-group mb-3">
                        <input id="search" type="text" class="form-control" placeholder="TV Show Title" aria-label="TV Show Title" aria-describedby="button-addon2" name="query">
                        <button class="btn btn-outline-success" id="button-addon2">Search</button>
                </div>
            </form>
            
        </div>
    </section>
    
    <section>
        <div class="container ms-1 mt-5">
            <h1 id='resultsHeader' class="display-3">Results for: <b><span id="searchDisplay"></span></b></h1>
        </div>
    </section>

    <section id="imgsDisplay" class="container mt-5">
        <div id = "row" class="row d-flex">

        </div>
    </section>
  
    
    <script src="app.js"></script>
</body>

</html>

Answer №1

To ensure your cards have the same height, apply overflow:hidden and a fixed height. This will prevent any extra text from causing display issues.

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

The toggle for hiding and showing, along with changing the button color, is not functioning properly due to

I'm encountering a puzzling issue with a toggle that hides and displays information and changes color on click. The functionality works flawlessly on the page where I initially wrote the code. For instance, the button's background shifts colors w ...

Tips for sending a changing mouse scroll value as a property in VueJS

I am currently utilizing the Laravel - VueJS framework. My goal is to detect the Y position of the mouse scroll and pass it dynamically as a prop to a Navbar component. To achieve this, I set up an eventListener and stored the window.scrollY value in a va ...

Using AngularJS to prevent HTML injection in input fields

Is there an effective method to prevent HTML injection in input fields? As an example, if I have a search input field: <input id="search" type="text" ng-model="search" placeholder="search..."> I want to ensure that any attempts to input malicious c ...

Omit punctuation from the key name in the JSON reply

Hey there, I'm a bit confused about why periods are being used in JSON key names. Basically, what I'm trying to accomplish is passing a JSON response through EJS variables into a page template and extracting the data into separate fields. Here& ...

What is the method for adding a prefix to every line in JavaScript?

I'm currently working on a React project and dealing with a string that may contain newlines. I'm looking for the most efficient way to inject a symbol at the start of each line. I've considered exploding the string into an array and adding ...

How can I align two div buttons at the top and bottom to appear on the left and right sides?

How can I change the position of two buttons from top and bottom to left and right as shown in the second image? I am utilizing Floating Vue, so the buttons will be contained within a div. Is it feasible to adjust their position accordingly? Check out th ...

When using onclick="location.href='page.html'", the page fails to load on Safari browser

I've been having trouble with getting onclick="location.href='link.html'" to work and load a new page in Safari (5.0.4). In my project, I am creating a drop-down navigation menu using the <select> and <option> HTML tags. I have ...

What is the best way to modify the state of a particular element in an array when using useState in React?

In my Next.js application, I am using a useState hook to manage state. Here is how my initial state looks like: const [sampleData, setSampleData] = useState({ value1: '', value2: '', value3: [] }); To update the state ...

Obtaining exchange rate data in JSON format from fixer.io and displaying it in

Looking to extract the current USD to EUR exchange rate from fixer.io and display it as a single line in an HTML file, with the USD value represented using commas instead of periods. Any assistance would be greatly appreciated! Find the link here: { ...

One of the functionalities is not functioning properly: either the validation or the AJAX

My validation code was working fine until I added some ajax code. Now, when I include the onclick="return chk()" in the submit input field, the validation code stops working. But if I remove it, the ajax code doesn't work. How can I resolve this issue ...

How should one correctly align various classes in a cascading manner within CSS?

Here is an interesting challenge - I want each of these buttons to have their background image change when hovered over. However, I'm struggling with the class setup in my code. I've been trying to understand how to distinguish between divs and c ...

Extract information from a lengthy text (vcard)

While scanning data from a vcard QR-code, the string I receive always follows this format: BEGIN:VCARD VERSION:2.1 N:Lastname;Firstname FN:Firstname Lastname ORG:Lol Group TITLE:Project Engineer TEL;WORK:+32 (0)11 12 13 14 ADR;WORK:Industrielaan 1;2250 Ol ...

What is the best way to choose two <li> elements with multiple classes in my WordPress navigation menu?

I am looking for a JavaScript function that will add the class "current_highlight" when an element with the class "activo2" also has the class "active". Here is my HTML code: <div class="navbar-header"> <button type="button " class="navbar-to ...

Tips for retrieving a selected date from an HTML textbox labeled as "Date"

My goal was to find the differences between two dates by utilizing an HTML Date textbox. <input type="text" name="datein" id="datein" value="" class="inputtexbox datepicker" style="display: none" is-Date/> <input type="text" name="dateto" id=" ...

Locate all buttons on the page that have an onclick function attached to them, and

I seem to have run into an issue. I am currently working with Java and WebDriver. My goal is to navigate to , locate the item "ipod", receive 4 results, and then compare them by clicking on the "compare" button beneath each item. However, I am encounteri ...

Safari on the iPad does not support opening debugging through the HTML tag

Currently, I am attempting to debug a website using Safari on a MacBook connected to an iPad. However, I am facing an issue where the tags cannot be opened for inspecting the content. When I try to open the wrapper, it does not expand to reveal the content ...

Switch the checkbox to a selection option

Is there a way to switch the selection method from checkboxes to a dropdown menu? $("input[name=koleso]:first").prop("checked", true); $('body').on('click', '.koleso label', function (e) { koles ...

The function of style.marginRight differs from that of style.marginLeft

One function aligns content to the left while the other doesn't align it to the right function openNavLeft() { document.getElementById("mySidenavLeft").style.width = "100vw"; document.getElementById("main").style.marginLeft = "100vw"; } function ...

Loading asynchronous select options with a knockout observable array

I have an ajax-based asynchronous loader for select options. It accepts a remote address and returns an observable array that is correctly populated with descriptions and key values to be used in the following binding. <select data-bind="value: select ...

Activate the next tab by clicking a button in ReactJS

I currently have a web page with 4 tabs, each containing different sets of data. Within the first tab, I have a button that should activate the next tab's content when clicked by the user. render(){ return ( <MuiThemeProvider> ...