How should white space be managed in Bootstrap 4 cards when incorporating responsive column classes - wrap elements inside divs or apply classes directly?

In Bootstrap 4, when applying a responsive column class like "col-sm-8" directly to a "div" with the class "card", whitespace is added between the card border and content. This can be seen in the highlighted yellow area in the image below.

https://i.sstatic.net/iLhSU.png

Here is the code snippet:

<div class="row">
            <div class="col-sm-2">
            </div>
            **<div class="card col-sm-8">**
                <h3 class="card-header bg-primary text-white">Card...</h3>
                <div class="card-block">
                    <form>
                        <div class="form-group row">
                            <div class="col-sm-2">Number</div>
                            <div class="col-sm-6">
                                <input type="radio" class="col-sm-1" name="numGuests" id="numGuests1" value="1">
                                <label for="numGuests" class="">1</label>
                                <input type="radio" class="col-sm-1" name="numGuests" id="numGuests2" value="2">
                                <label for="numGuests" class="col-form-label">2</label>

                            </div>
                        </div>
                    </form>
                </div>
            </div>

However, wrapping the card in a separate "div" and then applying the class resolves this issue. Now, the card header and content occupy the full width of the card as shown in the image linked below.

https://i.sstatic.net/drhgQ.png

<div class="row">
            <div class="col-sm-2">
            </div>
            **<div class="col-sm-8">
                <div class="card">**
                    <h3 class="card-header bg-primary text-white">Card...</h3>
                    <div class="card-block">
                        <form>
                            <div class="form-group row">
                                <div class="col-sm-2">Number</div>
                                <div class="col-sm-6">
                                    <input type="radio" class="col-sm-1" name="numGuests" id="numGuests1" value="1">
                                    <label for="numGuests" class="">1</label>
                                    <input type="radio" class="col-sm-1" name="numGuests" id="numGuests2" value="2">
                                    <label for="numGuests" class="col-form-label">2</label>

                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

The reason behind this behavior and how to determine whether a responsive class should be applied directly or wrapped within a div in Bootstrap is not clearly documented. Examples from the official documentation show inconsistencies in the usage of classes in different scenarios.

Snippet from Bootstrap card documentation https://getbootstrap.com/docs/4.0/components/card/

https://i.sstatic.net/FgjNG.png Snippet from Bootstrap form label documentation https://getbootstrap.com/docs/4.0/components/forms/

https://i.sstatic.net/nn7vX.png

Answer №1

"Why does the whitespace disappear all of a sudden?"

This phenomenon occurs because columns contain padding on their left and right sides, which creates the space (gutter) between them.

The official documentation explains...

"In a grid layout, content should always be placed within columns"

Therefore, the card, along with any other content, must be positioned inside the designated column (col-*). To my knowledge, the col-* classes are typically only mixed with other classes when dealing with forms: https://getbootstrap.com/docs/4.0/components/forms/#form-grid

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 start-up process of AngularJS applications with Bootstrap

Curious about the predictability of the initialization flow in AngularJS apps? Want to understand the order of execution of different blocks within an HTML document? I came across a question on bootstrapping in Angular JS, but it didn't delve into th ...

Does CSS delayed visibility transition fail to activate JavaScript transitionend event for elements' visibility changes?

My current approach involves using a clever method to delay the transition of visibility, in combination with opacity. So far, everything is working smoothly: <body> <button>run</button> <div class="box"></div> & ...

Customize Bootstrap 4 Carousel: Set specific data-interval for each slide

I'm facing an issue with setting the data-interval for each slide of the carousel. I came across a JavaScript code snippet on stackoverflow, but it's not functioning as expected. (Twitter Bootstrap Carousel slide duration) In my HTML, each slide ...

navbar item arranged in a row using Bootstrap 5

I am looking to create a fixed top navbar with just one row, instead of two rows. Here is the code I currently have: <div class="col-md-3"> <ul class="navbar-nav mb-2 mb-lg-0"> <li class="nav-item"&g ...

Adding variables and appending using jQuery

When using the append method to add HTML content dynamically to a div, I encountered an issue while trying to insert an iframe. Specifically, I needed to pass a variable's value to this frame for it to function properly. Here is the code snippet: va ...

Implement CSS to layer HTML 5 canvases while including a margin

I have a design conundrum in my app where I am using multiple stacked HTML canvas elements for drawing. My goal is to make the canvases fit perfectly within their containing div while also maintaining a left and bottom margin. This is how my HTML structur ...

Exploring the Variance between 'npm run serve' and 'npm run dev' Commands in Vue.js Development

Can you explain to me the distinction between npm run serve and npm run dev in vuejs? Additionally, can you clarify why it is recommended to use the npm run serve command when running a project? ...

Is there a way to see the countdown timers in Angular 7?

Is there a way for me to view my timer's countdown as it starts? I have attempted to bind it to a variable, but all I see is [object Object]: setTime(time) { if (this.settime >= 5 ) { this.currentTime = time; this.alerttime = thi ...

Styling CSS to place an image in between text and background coloring

As I try to replicate a happy accident of mine, which originally occurred during my first attempt at CSS: I was just randomly adding selectors when I stumbled upon this unique layout. However, as I proceeded with rewriting the file, I failed to save the o ...

Instructions on how to save HTML "innerHTML" along with attributes to a text document

I'm currently developing an HTML export feature from a DIV tag that includes various elements and attributes. HTML: <div id="master"><span class="classname">content goes here</span></div> <span class="download" onclick="ca ...

The height of the TinyMCE editor refuses to adjust

Hello, I'm new to utilizing Tinymce within an AngularJS environment. Specifically, I am working with the Tinymce library version 4.3.13. I have constructed some HTML and angularJS code to implement the Tinymce editor, but I've encountered a probl ...

Using a batch file to send an email

I have a goal to create an autorun file that can be placed on an external drive, flash disk, or CD. When executed, this file will run a series of commands that eventually send the computer's IP information back to me. Here is what I have so far... Th ...

Semantic UI table with rowspan feature

I am a beginner with semantic ui and I have been trying to replicate this specific layout. While going through semantic ui's documentation, I found something similar but not quite identical. Below is the HTML code: <div class="ui celled grid cont ...

Using React, we can create a component by defining it as a constant and then

Currently, I have a React component that is created as a const and takes props. In another file, there is a function called selectChanged() {} which returns undefined every time the select value is changed. The code for the component is as follows: ... ...

Identifying the presence of a particular cookie

I'm currently working on a project that already has several cookies stored. My goal is to determine if the cookie labeled "login" exists. Below is the code snippet I am using: if (document.cookie.indexOf("login") >= 0) { alert("login cookie ex ...

What is the best location to manage errors within a sequelize ORM query?

I am working with the Sequelize ORM within a Node/Express environment. My database has two tables: one for Users and another for Items. The Item table includes a foreign key that is linked to the UserId in the User table. Whenever I attempt to create an ...

There is no Api.js file in the Cordova iOS platform

I have been attempting to integrate the iOS platform into a new Cordova 7.0.1 project, but I continuously encounter the error mentioned in the title of this post. Despite following the steps outlined in this thread here, including removing and re-adding ...

What is the most effective method for displaying error messages in Extjs?

I am currently using the store.synch() method to post data, with validation being done on the server side. I am currently displaying error messages in a message box, but I want to explore alternative ways to show errors without having to use markInvalid( ...

Is there a way for me to determine when AJAX-loaded content has completely loaded all of its images?

After making an AJAX request to load HTML, it includes img tags within it. I am in need of a way to detect when these images have finished loading so that I can adjust the container size accordingly. Since I do not know how many images will be present in ...

Utilize the fetch function within the useEffect hook to generate a new

How can I effectively implement a component using useEffect? useEffect(() => { fetch('https://website') .then((res) => res.json()) .then((data) => { setData(data) // Utilize fetched data t ...