Bootstrap - the width of cards in a row appears odd

Here is the HTML structure for the cards:

<div class="container-fluid">
  <img src="http://clipart-library.com/img/1279251.png" style="max-width: 100px; filter: invert(1); float: left">
  <h1 style="color: white;">Favourite <br>apps </h1>
  <div class="row flex-row overflow-auto flex-nowrap" id="favourites" style="max-height: 400px">
    <div tabindex="0" class="card" style="max-width: 18rem;" id="ytdroid2">
      <img src="https://tse2.mm.bing.net/th?id=OIP.qHEnapkicCACp91KvwDYUAHaFj" class="card-img-top">
      <div class="card-body" style="height: 200px;">
        <h1 class="card-title" style="color: white;">
          <b>Test 2</b>
        </h1>
      </div>
    </div>
    <div tabindex="0" class="card" style="max-width: 18rem;" id="ytdroid">
      <img src="https://tse2.mm.bing.net/th?id=OIP.qHEnapkicCACp91KvwDYUAHaFj" class="card-img-top">
      <div class="card-body" style="height: 200px;">
        <h1 class="card-title" style="color: white;">
          <b>Test 1</b>
        </h1>
      </div>
    </div>
  </div>
</div>

This arrangement results in: https://i.sstatic.net/3zoam.png

Currently, both the card and the card body have a background color of white with an opacity of 0.4 set by a CSS sheet. The issue lies in the width discrepancy between the card, image, and card body. Removing row-related classes would make them the same size but vertically stacked, which isn't desired. Using a card deck doesn't work for fixed width and horizontal scrollability. How can the width of the card match that of the image and the body?

Answer №1

Make sure to always place the cards inside columns for proper organization.

"Remember, rows are like wrappers for columns. Each column has spacing between them known as gutters... "

In addition...

"When creating grid layouts, all content should be placed within columns. The structure of Bootstrap’s grid system goes from container to row to column to your actual content. "

Avoid...

  • Using a col-* without a row as its immediate parent.
  • Placing content directly into a row without using columns.

Answer №2

Irrespective of how the grid classes are utilized (appropriately or otherwise), in this scenario, the source of the extra space originates from the padding BootStrap applies to .card.

You have the option to add .p-0 to the .card or adjust the padding to zero on the element. In the code snippet below, I've only applied .p-0 to the first card for comparison:

div.container-fluid {
  background-color: DarkSlateBlue;
}

div.card,
div.card-body {
  background-color: rgba(255, 255, 255, 0.4);
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e6848989929592948796a6d3c8d7c8d6">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">

<div class="container-fluid">
  <img src="http://clipart-library.com/img/1279251.png" style="max-width: 100px; filter: invert(1); float: left">
  <h1 style="color: white;">Favourite <br>apps </h1>
  <div class="row flex-row overflow-auto flex-nowrap" id="favourites" style="max-height: 400px">
    <div tabindex="0" class="card p-0" style="max-width: 18rem;" id="ytdroid2">
      <img src="https://tse2.mm.bing.net/th?id=OIP.qHEnapkicCACp91KvwDYUAHaFj" class="card-img-top">
      <div class="card-body" style="height: 200px;">
        <h1 class="card-title" style="color: white;">
          <b>Test 2 .card.p-0</b>
        </h1>
      </div>
    </div>
    <div tabindex="0" class="card" style="max-width: 18rem;" id="ytdroid">
      <img src="https://tse2.mm.bing.net/th?id=OIP.qHEnapkicCACp91KvwDYUAHaFj" class="card-img-top">
      <div class="card-body" style="height: 200px;">
        <h1 class="card-title" style="color: white;">
          <b>Test 1 .card</b>
        </h1>
      </div>
    </div>
  </div>
</div>


<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e0c01011a1d1a1c0f1e2e5b405f405e">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-U1DAWAznBHeqEIlVSCgzq+c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj" crossorigin="anonymous"></script>

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

Determine the total number of rows present in a Selenium web table

I have a web table in a page that only has one attribute, which is the class. I am trying to determine the number of rows in this table. The code below works, but it provides me with the count of all web tables that have their parent class set as OverviewT ...

Enable the entire button to be clickable without the need for JavaScript by utilizing a Bootstrap 5 button

Is there a way to make a button redirect when clicking anywhere on it, not just the text inside? Here is the button code utilizing Bootstrap 5: <button class="btn btn-rounded btn-primary" type="button">Placeholder Text</button& ...

The HtmlHelper ActionLink consistently establishes the current controller instead of allowing for the specification of another controller

I have been utilizing the HtmlHelper class to incorporate some code into my layout Navbar as a menu. Here is the code snippet: public static MvcHtmlString MenuLink(this HtmlHelper helper,string text, string action, string controller) { ...

What is the best way to run a script following the completion of an external asynchronous script within the header tag?

Utilizing Webflow's custom code editor, I incorporated an external script from Finsweet within the <head> tag as per their instructions. Although I would like to modify the page with my own script following the execution of Finsweet's scri ...

Is there a way to navigate back and forward in browsers for single page applications?

Working on a Single Page Application(SPA) has led me to utilize htmlPushState and window.onpop in order to maintain browser back and forward functionality while changing CSS styles and calling backend APIs. Currently, my SPA project consists of 5 pages, b ...

Ruling out the use of jQuery script within a dynamic framework

I have a unique jQuery script to create a "Back to Top" functionality: <script type="text/javascript"> $(document).ready(function(){ // Initially hide the #TopButton $("#TopButton").hide(); // Fade in the #TopButton on scrolling $(function () { ...

Is there a way to hide a specific character within a span element as securely as a password input field using

Can I use CSS to mask a span character similar to an input type password? Are there any properties like type="password" for the span or a specific class in Bootstrap such as class="mask" that can achieve this effect? https://i.stack.imgur.com/b8WQ4.png ...

Capture table screenshot using Selenium with unique font style

Below is a Python script that uses Selenium to load a specific page and capture a screenshot of the table on the page. from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from sel ...

I am unable to activate Wicket's onchange event

I've been trying to automate the population of three dropdown selection elements using a Chrome extension. I'm able to change the value of the first dropdown, but it doesn't trigger the necessary action that populates the second and third dr ...

What could be causing the sudden glitch in my sidebar animation?

I'm encountering issues with the sidebar opening smoothly. The width of the column seems to change as it opens and there's a choppy jump. I've attempted to resolve this by applying transitions to the CSS and using flex-grow / shrink. Unfort ...

What is the best way to display a collection of square Views in a FlatList in a react native application?

How can you create a scrollable square grid with two columns of squares in a FlatList using react-native and ensure it is responsive to different screen sizes? Which CSS properties in react-native are necessary to achieve square shapes for each <Item/& ...

Tips for styling custom buttons within the active editor popup of tinyMCE using CSS

Looking to enhance my tinyMCE active editor popup with custom CSS. Check out the screenshot provided for reference https://i.sstatic.net/vdVDF.png. Below you can find the code snippet used as a reference: tinymce.activeEditor.windowManager.open({ tit ...

Getting the selected value from a dropdown button that utilizes the @Html.DropDownListFor tag helper in ASP.NET Core MVC

I am currently working on a dropdown button that is populated using the HTML tag helper @Html.DropDownListFor. The code snippet for this button looks like: <div class="col-sm-3" id="valueSetParent"> @Html.DropDownListFor(model ...

Customize the appearance and text in the navigation bar

Currently, I am in the process of creating my own personal website template. However, I have encountered an issue with styling the image and text within the navbar. I am looking to adjust the aspect ratio of the image in order to center it within the n ...

Create a website that can expand in size without relying on percentage values

At the moment, I am in the process of building a responsive website. The issue I am currently facing is that the layout for the responsive design (which was created by someone else and not me) has a fixed width of 745px. Obviously, this width does not acco ...

Facebook pages that exceed 700px in length without the need for scrollbars

Is there a way to extend the length of a Facebook page without scrollbars when using an iframe, so that it doesn't crop at 700px? ...

Prevent click event listener from activating if the click results in a new tab or window being opened

Occasionally, when using my web app, I need to prevent click event listeners from activating if the click is meant to open a new window or tab. For instance, in my single page application, there are links to other content. While they function well, there i ...

Having trouble with the Ajax load function not functioning in your HTML code?

The issue has been resolved. Thank you to everyone who helped! Initially, I was attempting to run a file offline instead of on a web server (XAMPP server). Once I uploaded the file to the web server, it started working properly. I had been trying to load ...

Can someone help me with combining these two HTML and JavaScript files?

I successfully created an HTML/JavaScript file that functions properly: <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor ...

Modify the click function from <tr> to <td> tag

I want to create an HTML page that functions as a digital library for books. Users should be able to click on a rate button to save the selected book to local storage, allowing them to see their rating whenever they revisit the page. However, I'm enc ...