How to Create a Flipping Bootstrap 4 Card with Hover Effect

I am currently in the process of creating a Bootstrap 4 and HTML5 dashboard. My goal is to have each card on display flip to reveal additional information when the 'i' button is hovered over.

Although I have managed to get the flipping animation to work, there are a few issues that I seem to be facing:

  • The additional info is not being displayed on the flip (it only flips the initial view)
  • There seems to be a displacement of the card to the left when it is flipped

You can view the current state by checking out this fiddle with my existing HTML and CSS code.

// Values for x-axis labels
var month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

// Data points for drawing lines
var exampleChart = [0, 114, 106, 106, 107, 111, 133, 221, 783, 2478, 2485, 654];


var ext = document.getElementById("exampleChart");
var exampleChart = new Chart(ext, {
    type: 'line',
    data: {
        labels: month,
        datasets: [
            {
                data: exampleChart,
                borderColor: "#155724",
                pointBackgroundColor: '#155724',
                pointBorderWidth: '0',
                pointRadius: '4'
            }
        ]
    },
    options: {
        scales: {
            yAxes: [
                {
                    gridLines: {
                        display: false
                    },
                    ticks: {
                        display: false 
                    }
                }
            ],
            xAxes: [
                {
                    gridLines: {
                        display: false
                    },
                    ticks: {
                        display: false 
                    }
                }
            ]
        },
        legend: {
            display: false
        },
        tooltips: {
            callbacks: {
                title: function () { }
            }
        },
        layout: {
            padding: {
                left: 0,
                right: 10,
                top: 5,
                bottom: 0
            }
        }
    }
});

If possible, I would like to avoid using jQuery. However, if necessary, I would like a dissolve effect on the top card and for it to reappear smoothly when moved away from.

Just to clarify, I want the flipping action to occur only when hovering over the 'i' icon.

Answer №1

Here is a customizable code snippet for creating a flip container effect:

.flip_container {
  width: 100%;
  height: 450px;
  cursor: pointer;
}
.flip_container:hover .flip {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
}
.flip {
  -webkit-transition: 0.75s;
  transition: 0.75s;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  position: relative;
  height: 100%;
}
.flip_front,
.flip_back {
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  position: absolute;
  top: 0;
  left: 0;
}
.flip_front {
  -webkit-transform: rotateY(0deg);
  transform: rotateY(0deg);
}
.flip_back {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
  padding: 10px;
  background: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div class="container">
  <div class="row">
    <div class="col-sm-12 col-md-6 col-lg-4 flip_container">
      <div class="flip">
        <div class="flip_front">
           <div class="card">
            <div class="card-body">
                <div class="form-group row">
                    <div class="col-11">
                        <div class="alert-success card-icon">
                          <div class="chartjs-size-monitor">
                            <div class="chartjs-size-monitor-expand">
                              <div class=""></div>
                            </div>
                            <div class="chartjs-size-monitor-shrink">
                              <div class=""></div>
                            </div>
                          </div>
                          <canvas id="exampleChart" class="chartjs-render-monitor" width="400" height="200" style="display: block; width: 400px; height: 200px;"></canvas>
                        </div>
                    </div>
                    <div class="col-1">
                        <i id="extInfoIcon" class="fa fa-info-circle fa-4x text-info" style="cursor: pointer"></i>
                    </div>
                </div>
                <div class="form-group row dashboardCardHeadingRow">
                    <div class="col-12">
                        <div class="card-subtitle text-muted">Card Footer</div>
                    </div>
                </div>
                <div class="row text-center">
                    <div class="col-6">
                        <div class="card-subtitle text-muted">Heading 1</div>
                        <h4 class="mb-0">0</h4>
                    </div>
                    <div class="col-6">
                        <div class="card-subtitle text-muted">Heading 2</div>
                        <h4 class="mb-0">0</h4>
                    </div>
                </div>
            </div>
          </div>
        </div>
        <div class="flip_back w-100">
          <div class="card">
          <div class="card-body">
            <div class="form-group p-5">
              <h1 class="m-0">John Doe</h1>
              <p class="m-0">Architect & Engineer</p>
              <p class="m-0">We love that guy</p>
            </div>
          </div>
        </div>
        </div>
      </div>
    </div>
  </div>
</div>

Answer №2

Upon reviewing, I have identified the following errors:

 - It is advisable to remove the custom CSS properties that were added (.dashboardCardHeadingRow, .card, .card-icon)
 - Please ensure to check the HTML syntax (the "flip-card-inner" div should be closed at the end)

// The x-axis labels for our chart
var month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

// Data points for the chart
var exampleChart = [0, 114, 106, 106, 107, 111, 133, 221, 783, 2478, 2485, 654];


var ext = document.getElementById("exampleChart");
var exampleChart = new Chart(ext, {
    type: 'line',
    data: {
        labels: month,
        datasets: [
            {
                data: exampleChart,
                borderColor: "#155724",
                pointBackgroundColor: '#155724',
                pointBorderWidth: '0',
                pointRadius: '4'
            }
        ]
    },
    options: {
        scales: {
            yAxes: [
                {
                    gridLines: {
                        // color: '#000',
                        display: false
                    },
                    ticks: {
                        display: false // This will remove only the label
                    }
                }
            ],
            xAxes: [
                {
                    gridLines: {
                        display: false
                    },
                    ticks: {
                        display: false // This will remove only the label
                    }
                }
            ]
        },
        legend: {
            display: false
        },
        tooltips: {
            callbacks: {
                title: function () { }
            }
        },
        layout: {
            padding: {
                left: 0,
                right: 10,
                top: 5,
                bottom: 0
            }
        }
    }
});
.flip-card {
  background-color: transparent;
  width: 300px;
  height: 200px;
  border: 1px solid #f1f1f1;
  perspective: 1000px; /* Remove this if you don't want the 3D effect */
}

/* Container for positioning front and back sides */
.flip-card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  text-align: center;
  transition: transform 0.8s;
  transform-style: preserve-3d;
}

/* Horizontal flip on mouse hover */
.flip-card:hover .flip-card-inner {
  transform: rotateY(180deg);
}

/* Positioning front and back sides */
.flip-card-front, .flip-card-back {
  position: absolute;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
}

/* Style the front side (fallback if image is missing) */
.flip-card-front {
  background-color: #bbb;
  color: black;
}

/* Style the back side */
.flip-card-back {
  background-color: dodgerblue;
  color: white;
  transform: rotateY(180deg);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>

<div class="form-group row">
  <div class="col-4">
    <div class="flip-card">
      <div class="flip-card-inner">
        <div class="flip-card-front">
          <div class="card">
            <div class="card-body">
              <div class="form-group row">
                  <div class="col-11">
                      <div class="alert-success card-icon">
                        <div class="chartjs-size-monitor">
                          <div class="chartjs-size-monitor-expand">
                            <div class=""></div>
                          </div>
                          <div class="chartjs-size-monitor-shrink">
                            <div class=""></div>
                          </div>
                        </div>
                        <canvas id="exampleChart" class="chartjs-render-monitor" width="400" height="200" style="display: block; width: 400px; height: 200px;"></canvas>
                      </div>
                  </div>
                  <div class="col-1">
                      <i id="extInfoIcon" class="fas fa-info-circle fa-4x text-info" style="cursor: pointer"></i>
                  </div>
              </div>
              <div class="form-group row dashboardCardHeadingRow">
                  <div class="col-12">
                      <div class="card-subtitle text-muted">Card Footer</div>
                  </div>
              </div>
              <div class="row text-center">
                  <div class="col-6">
                      <div class="card-subtitle text-muted">Heading 1</div>
                      <h4 class="mb-0">0</h4>
                  </div>
                  <div class="col-6">
                      <div class="card-subtitle text-muted">Heading 2</div>
                      <h4 class="mb-0">0</h4>
                  </div>
              </div>
          </div>
        </div>
      </div>
      <div class="flip-card-back">
        <div class="card">
          <div class="card-body">
            <div class="form-group row" style="height: 150px">
              <h1>John Doe</h1>
              <p>Architect & Engineer</p>
              <p>We love that guy</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</div>

Link to jsFiddle

I trust this information proves to be useful.

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

Incorporate jQuery UI Datepicker with the ability to trigger an Ajax function upon selecting a

Is it possible to execute Ajax on date select from a datepicker and how can I handle it? I am not receiving any errors in the console regarding the Ajax part. Instead of getting the pro-rated subscription value, I am seeing "success" in the div with ID &ap ...

What is the process for implementing a third-party component in my web application?

After some experimentation, I've discovered that it's necessary to include the link to the css files in the header and then mention the link to the js files before the closing tag. However, I encountered difficulties when trying to use a compone ...

The curious case of CSS nth-of-type quirks

I am using CSS nth-of-type(even) to organize divs. It usually works fine, but when I send an AJAX request and add new HTML div after another one, the selector starts behaving strangely. .row > .cd-timeline-block-sort:nth-of-type(even) > .cd-timeline ...

Construction of jQuery Events

I have experience working with jquery tools and recently started exploring the twitter bootstrap src. One interesting thing I've observed is the utilization of the $.Event constructor for event triggering. For example, in cases like the bootstrap mo ...

Are you searching for the origin of a dynamic dropdown widget or database?

Currently, I am browsing through and I have a query regarding accessing all available classes/options for the courses in a semester. There is a class locator on the top left of the page with a dynamic drop-down menu. Can anyone suggest how I can access ...

"The functionality of the Bootstrap4 tab panel is not working properly for switching

I am currently working on creating 4 tab panels using Bootstrap 4. My goal is for the tab contents to fade in when I click on them. However, I am facing an issue where nothing happens when I click on the other panels. Do you have any insights on why this ...

What could be causing my CSS navbar with display: inline-block to not center properly?

How can I use display:inline-block in CSS to center my navigation bar? nav { position: fixed; width: 100%; } nav ul { width: 100%; margin: auto; } nav ul li { display: inline-block; margin: 5px 20px 5px 20px; text-align: center; } ...

Steps for updating images and text on a live webpage after launching the web application

I added an image to a jsp file. The image, named kid.jpg, is downloaded and stored in the /resources/images folder as shown in the code snippet below. <li style="background-image: url('resources/images/kid.jpg');"> Now that my web app is ...

What is the best way to use JavaScript to fill in missing images with alternative HTML content?

As a provider of a service that helps users find the location of pictures, I face a challenge due to the fact that the pictures are stored on another internal site. Some users may not have access to this site as my service offers additional information. Th ...

Ways to adjust the visibility of a div element multiple times using javascript and css

I implemented a popup feature in my application: <div id="modal"></div> let modal = document.getElementById('modal') modal.innerHTML = ` <button class="close-button" onclick="close_modal()" ...

Failure of event watcher on dynamically updated content

Any help would be greatly appreciated! :) I am currently using JavaScript to dynamically add fields to a document, but I have noticed that the event listener only works on predefined fields. For instance, in the code snippet below, the 'lozfield&apo ...

Reveal or conceal information with a dropdown menu feature

I am attempting to implement a feature where the image file upload section is hidden or displayed based on the selection made in a dropdown list. If the user selects option "2", the image upload section should be hidden. Conversely, if they choose option " ...

There is a significant spacing issue between the header and main category when viewed on a mobile device

Experiencing issues with element distances on different screen sizes? While the website looks fine on normal screens, there's a noticeable gap between the header and main sections on mobile devices. See below for the provided HTML and CSS code. ...

What is the best way to retrieve a specific value from a multi-dimensional JSON object array?

I'm struggling to retrieve a value from the JSON object below array(3) { [0]=> object(stdClass)#1 (11) { ["Group"]=> string(2) "18" ["GroupName"]=> string(8) "Wireline" ["Region"]=> string(2) "15" ["RegionName"]=> string(8) "Atlantic" ...

What is the best way to use a button to hide specific divs upon clicking?

Is there a way to use a button onclick event to hide specific divs within a parent div? I've tried using .toggleClass('.AddCSSClassHere') but I'm not sure how to apply it to multiple divs. The jQuery snippet provided only allows me to h ...

Bootstrap 5 introduces a new feature where col-sm-6 will take precedence over col-md

When using Bootstrap 5, I encountered an issue where the columns were not sizing evenly in a row. Specifically, on small screens, I wanted them to be exactly size 6. In this case, the columns should have been size 3 for screens larger than md, but they end ...

The Bootstrap modal will not appear when the parent container's position is fixed

I am having an issue with a bootstrap modal that I am using as an instruction guide. My goal is to keep it fixed at the top-right corner of the viewport, so I tried using the fixed position. However, when I do this, the modal turns grey. On the other han ...

Adding Angular directives to the DOM after the document has finished loading does not function properly in conjunction with ngAnimate

I've been working on developing an Angular service that can dynamically append a notification box to the DOM and display it without the need to manually add HTML code or write show/hide logic. This service, named $notify, can be used as follows: $no ...

The radio button in the HTML form is disabled when the user selects

I have two radio buttons labeled Billable "Y" and Billable "N". These radio buttons are linked to a database with corresponding values of "Y" or "N". If the user selects "Y" using the radio button, then the NonBillableReason text box should be disabled. ...

How to modify checkbox and textarea values in a MySQL database with Jquery

In my MySQL database, I have a textarea and three checkboxes. By default, the textarea value is null and the checkbox values are set to zero (0). When I enter text into the textarea, I am able to update the text value in my database. However, I am facing ...