The expandable card header in Bootstrap 4 is experiencing problems with spacing and wrapping, as well as issues with the rotating

I'm currently in the process of developing a Bootstrap4 card that consists of a title, body, and close button that can expand and collapse. I have been closely following this tutorial from Card collapse tricks!

While I appreciate the example provided in the tutorial, I would like to make a few modifications:

  • Repositioning the expand icon to the left of the card header
  • Including a close button on the right side of the card header

In an attempt to implement these changes, I created a jsfiddle: https://jsfiddle.net/s0ygr35L/1/

<head>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>

My implementation:

<div class="card ml-5 mr-5 mt-5">
    <h5 class="card-header expandable">

        <!-- Method 2 -->
        <a data-toggle="collapse" href="#collapse-example" aria-expanded="true" aria-controls="collapse-example" id="heading-example" class="d-block">
            <div class="row " style='margin-bottom:0;'>
                
                <div class="col-md-1 ">
                    <i class="rotate fa fa-chevron-down "></i>
                </div>
                
                <div class="col">
                    <p>Collapsible Group Item #1</p>
                </div>
                
                <div class="col">
                    <i class="fa fa-close pull-right"></i>
                </div>
             
            </div>
        </a>
         
        
    </h5>
    <div id="collapse-example" class="collapse show" aria-labelledby="heading-example">
        <div class="card-body">
            Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon
            officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3
            wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et.
            Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan
            excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt
            you probably haven't heard of them accusamus labore sustainable VHS.
        </div>
    </div>
</div>



Example:

<div class="card ml-5 mr-5 mt-5">
    <h5 class="card-header expandable">
        <!-- Method 1  -->
        <a data-toggle="collapse" href="#collapse-example2" aria-expanded="true" aria-controls="collapse-example" id="heading-example" class="d-block">
            <i class="fa fa-chevron-down pull-right"></i>
            Collapsible Group Item #1
        </a>
        
        
    </h5>
    <div id="collapse-example2" class="collapse show" aria-labelledby="heading-example">
        <div class="card-body">
            Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon
            officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3
            wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et.
            Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan
            excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt
            you probably haven't heard of them accusamus labore sustainable VHS.
        </div>
    </div>
</div>

In my initial attempt at creating the card, I added a column/row section for the header, but this resulted in some unwanted blank space at the bottom. You can view the issue here: https://i.sstatic.net/bFHjR.png

I attempted to adjust the styles by setting padding-bottom=0 and margin-bottom=0, but unfortunately, it did not resolve the problem. Furthermore, the close button located on the right side of the card title rotates whenever the card is expanded or collapsed due to the CSS code. Despite my efforts to specify the CSS identifier more precisely, the rotation persists.

Could someone provide assistance with the following:

  • Removing the excess space at the bottom of my card title
  • Preventing my close icon button from rotating unnecessarily

Answer №1

The issue arises from inserting extra elements into the card header unnecessarily. The grid for the header, expander, and close icons is redundant as Bootstrap classes already dictate their placement. This unnecessary grid is what causes the excess spacing and disrupts the layout on smaller screens.

The following minimal code snippet suffices for your header:

<h5 class="card-header expandable">
    <a data-toggle="collapse" href="#collapse-example" aria-expanded="true" aria-controls="collapse-example" id="heading-example" class="d-block">
        <i class="rotate fa fa-chevron-down "></i>
         Collapsible Group Item #1
        <i class="fa fa-close pull-right"></i>
    </a>
</h5>

Regarding the rotating close icon issue, the problem lies in the application of rotation code meant for the expander icon to the close icon as well. To rectify this, you can define a separate rotate class in your CSS that targets only specific elements:

.expandable .collapsed .rotate.fa{
      transform: rotate(180deg);
}

You can witness the corrected implementation below:

.expandable .fa {
      transition: .3s transform ease-in-out;
    }
.expandable .collapsed .rotate.fa{
      transform: rotate(180deg);
}
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
      <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
      <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">


<div class="card ml-5 mr-5 mt-5">
    <h5 class="card-header expandable">

        <!-- method2 -->
        <a data-toggle="collapse" href="#collapse-example" aria-expanded="true" aria-controls="collapse-example" id="heading-example" class="d-block">
                    <i class="rotate fa fa-chevron-down "></i>
                     Collapsible Group Item #1
                    <i class="fa fa-close pull-right"></i>
        </a>
         
        
    </h5>
    <div id="collapse-example" class="collapse show" aria-labelledby="heading-example">
        <div class="card-body">
            Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon
            officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3
            wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et.
            Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan
            excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt
            you probably haven't heard of them accusamus labore sustainable VHS.
        </div>
    </div>
</div>

Answer №2

To make the elements flex and wrap them all inside, include the flex property in your code as shown below.

.expandable .fa {
      transition: 0.3s transform ease-in-out;
      display: flex;
      margin-right: 10px;
    }
    .expandable .collapsed .fa{
      transform: rotate(180deg);
    }

    .card-header .d-flex{
      display: flex;
      justify-content: space-between;
      align-items: center;
    } 
    .card-header p{
      display: flex;
    }
<head>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>

<div class="card ml-5 mr-5 mt-5">
    <h5 class="card-header expandable">
        <a data-toggle="collapse" href="#collapse-example" aria-expanded="true" aria-controls="collapse-example" id="heading-example" class="d-block">
            <div class="row " style='margin-bottom:0;'>
                
                <div class="col d-flex ">
                   <div class="d-flex">
                      <i class="rotate fa fa-chevron-down "></i>
                      <p class="mb-0">Collapsible Group Item #1</p>
                   </div>
                  <i class="fa fa-close pull-right"></i>
                </div>
             
            </div>
        </a>
    </h5>
    <div id="collapse-example" class="collapse show" aria-labelledby="heading-example">
        <div class="card-body">
            Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon
            officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3
            wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et.
            Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan
            excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt
            you probably haven't heard of them accusamus labore sustainable VHS.
        </div>
    </div>
</div>

For more details, visit https://jsfiddle.net/

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

Generating nested arrays using Vue.js

Calculating the total costs of products selected by a user for each company is my current task. Below is the code snippet I am using: let companiesProductPrices = []; let prices = []; this.selectedCompaniesDetails.forEach ...

Find and retrieve all data attributes with JavaScript

What is the method to retrieve all data-attributes and store them in an array? <ul data-cars="ford"> <li data-model="mustang"></li> <li data-color="blue"></li> <li data-doors="5"></li> </ul> The resultin ...

Why isn't Latex rendering when called from Javascript?

Below is the code I'm working with: function test() { document.getElementById('demo').innerHTML="$$\left[ x=0 \right] $$";//same code from demo1.but not rendered } test(); <script type="text/javascript" src="http://latex.co ...

Utilizing a single controller in multiple locations within AngularJS

I am currently working on developing an Ionic application which includes screens with lists containing checkboxes and the option to select all items. My experience with AngularJS and Ionic is fairly limited. The "Select All" functionality works as intende ...

Transitioning webpage from .html to .php

As my website has grown significantly, I am considering converting the pages to PHP in order to make future updates easier. However, a challenge arises as there are numerous links across the web pointing to www.example.com/page1.html, and now the page will ...

Align the prices of products in a uniform position within each table cell

Apologies for the lengthy explanation. I am working with osCommerce and have a page that displays all our product specials in a 3 column table layout. My challenge is to align the price of each product so that they are aligned perfectly across the screen. ...

Is it possible to craft poetic lines with indents in HTML using blockquotes, pre, or dd tags?

I am struggling to format a few lines of text with indentations using HTML. Here is an example I found: HERE Below is my attempt: <p><code>"Knowing is not enough. We</code></p> <p><blockquote><code>must apply. Wi ...

The function is not defined, even though it is located within the same file

I've encountered an issue with my web page where I'm trying to read data from an HTML data table and update it on a PHP file using AJAX queries in JavaScript. Everything was working fine until recently, even though I didn't make any signific ...

Activate the ability to enter data into a specific cell

I am trying to change the color of an input cell to pink when a user types an incorrect key. Although I can successfully change the color, I am facing difficulty in resetting it after they click on the cell. I have attempted using .prop("disabled", false), ...

Automatically updating values in AngularJS

I currently have an array of dates stored in $scope.dates = [] ($scope.dates[0].date). My goal is to create a new array with durations that update automatically. $scope.dates[i].duration = Date.now() - $scope.dates[i].date. The objective is to display a ...

How to assign a class to a dynamically created span in HTML using AJAX and PHP arrays

I'm puzzled as to why this keeps breaking. In my array, I have multiple words stored: $poolOne=array("sparkly","sporty","happy","confident","awesome","funny","awkward","mad","silly","dynamic", "handsome","merry","horrid","funky","loud","chirpy", ...

Django works perfectly on local host, but unfortunately, hosting servers are not cooperating

Programming is a new skill for me and I recently created a weather app using Django. The app functions perfectly when I run it locally with the server, but I've encountered difficulties when trying to host it on various platforms. Here is the link to ...

Saving the output of an AngularJS expression in an input field

Looking at the calculations below, I am currently evaluating an expression, {{ annualIncome * 4.5 }}, in one input and then re-evaluating the same expression in another input. Instead of saving the result and transferring it to the other input, I am repeat ...

Web Development: Custom Search Form

I am looking to create a website with a form similar to this one, but I'm struggling to figure out how to incorporate all three components into a single form using only HTML and CSS - no javascript. Do you have any suggestions or advice on how to achi ...

Checking for CSS-truncated text with JavaScript

I am currently working on a JavaScript project to determine if text is truncated. While I found a useful solution mentioned here, there is one edge case that needs to be addressed. Despite the visual truncation of the text, the first block on mouse hover i ...

Stylesheet for a React component

Why is the CSS code in my React component file not working even though I have imported it? import React from 'react'; import { Carousel } from 'react-bootstrap'; import './Banner'; ...

Fixed first column in a spacious data grid

The layout I have created can be viewed in the Fiddle http://jsfiddle.net/5LN7U/. <section class="container"> <section class="field"> <ul> <li> Question 1 </li> <li> question 2 </ ...

The state of the Toggle Button in Material UI

I am currently working with Material UI's ToggleButton and ToggleButtonGroup components. However, I'm facing an issue where the ToggleButton remains highlighted even after clicking on another button in the group. While the buttons are registering ...

The Angular 6 ngb-bootstrap modal fails to display due to conflicting bootstrap css within a div element

I've encountered numerous inquiries about modal not displaying, and although I've successfully implemented it in other scenarios, this one presents a unique challenge! In my Wordpress project, I've embedded an Angular app within the admin a ...

How can I apply a border to individual table cells in HTML without affecting the entire table?

Struggling to apply a border on table cells, especially when using rowspan. Desiring a table layout with 4 cells - one large cell on the left and three smaller cells on the right side of it. Below is the code snippet I attempted: jsfiddle[dot]net/1fv4dz ...