Using position absolute to flip a card within a Bootstrap 4 container

I am aiming to implement a card flip effect using the provided code within a row of a bootstrap 4 container.

<div class="container">
    <div class="row" >
        <div class="card-container col-sm-4">
            <div class="card card-front" >
                <img class="card-img-top" src="image/1.JPG" alt="Card image cap">
                <div class="card-body">
                <h5 class="card-title text-center">1</h5>
                </div>
            </div>
            <div class="card card-back" >
                <div class="card-body">
                    <p  class="font-weight-light">
                        Some back text
                    </p>
                </div>
            </div>

        </div>
        <div class="card-container col-sm-4">
            <div class="card  card-front" >
                <img class="card-img-top" src="image/2.JPG" alt="Card image cap">
                <div class="card-body">
                <h5 class="card-title text-center">2</h5>    
                </div>
            </div>
        </div>
        <div class="card-container col-sm-4">
          <div class="card  card-front">
                <img class="card-img-top" src="image/3.JPG" alt="Card image cap">
                <div class="card-body">
                <h5 class="card-title text-center">1</h5>
                </div>
          </div>
        </div>
    </div>
</div>

Both the second and third cards do not have a backside yet. Nevertheless, the backside of these cards will be similar to the first card's design. Provided below is the CSS for achieving this effect:

.card-container{
    position: relative;
    perspective: 200rem;
}

.card-container:hover .card-front{
    transform: rotateY(180deg);
}

.card-container:hover .card-back{
    transform: rotateY(0deg);
}

.card-back{
    transform: rotateY(180deg);
}

.card{
    position: absolute;
    transition: all 0.9s;
    backface-visibility: hidden;
}

The issue arises when implementing position: absolute, causing the height of the container to shrink to 1px. Is there a workaround for this problem? Alternatively, are there better methods to achieve the desired outcome? Attempts such as adding position: relative to both row and container have been futile so far.

Answer №1

There's an image on the front side and nothing on the back. The solution is to place the same image on the back side but hide it.

See the code snippet below:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

<div class="container">
  <div class="row">
    <div class="card-container col-sm-4">
      <div class="card card-front">
        <img class="card-img-top" src="https://i.sstatic.net/gQGYY.png" alt="Card image cap">
        <div class="card-body">
          <h5 class="card-title text-center">1</h5>
        </div>
      </div>
      <div class="card card-back">
        <img class="card-img-top" src="https://i.sstatic.net/gQGYY.png" alt="Card image cap" style="visibility:collapse">
        <div class="card-body">
          <p class="font-weight-light">
            Some back text
          </p>
        </div>
      </div>

    </div>

  </div>
</div>
<style>
  .card-container {
    position: relative;
    perspective: 200rem;
  }
  
  .card-container:hover .card-front {
    transform: rotateY(180deg);
  }
  
  .card-container:hover .card-back {
    transform: rotateY(0deg);
  }
  
  .card-back {
    transform: rotateY(180deg);
  }
  
  .card {
    position: absolute;
    transition: all 0.9s;
    backface-visibility: hidden;
    border: 1px solid lightpink;
  }
</style>

Answer №2

The .card-front class needs to have a relative position, while the .card-back class should have an absolute position with specified width, height, top, left, and overflow properties to allow for content display through scrolling. By implementing this technique, the height of the containing element, .container, will adjust automatically.
The code snippet provided below demonstrates how to achieve this:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

<div class="container my-2">
  <div class="row" >
    <div class="card-container col-sm-4 my-3">
      <div class="position-relative">
        <div class="card card-front">
          <img class="card-img-top" src="https://via.placeholder.com/400x200/22ff22" alt="Card image cap">
          <div class="card-body">
            <h5 class="card-title text-center">1</h5>
          </div>
        </div>
        <div class="card card-back">
          <div class="card-body">
            <p  class="font-weight-light">Some back text #1
              Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
              tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
              quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
              consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
              cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
              proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
            </p>
          </div>
        </div>
      </div>
    </div>
    <div class="card-container col-sm-4 my-3">
      <div class="position-relative">
        <div class="card card-front" >
          <img class="card-img-top" src="https://via.placeholder.com/400x200/ffff22" alt="Card image cap">
          <div class="card-body">
            <h5 class="card-title text-center">2</h5>    
          </div>
        </div>
        <div class="card card-back" >
          <div class="card-body">
            <p  class="font-weight-light">Some back text #2</p>
          </div>
        </div>
      </div>
    </div>
    <div class="card-container col-sm-4 my-3">
      <div class="position-relative">
        <div class="card card-front">
          <img class="card-img-top" src="https://via.placeholder.com/400x200/2222ff" alt="Card image cap">
          <div class="card-body">
            <h5 class="card-title text-center">3</h5>
          </div>
        </div>
        <div class="card card-back">
          <div class="card-body">
            <p  class="font-weight-light">Some back text #3</p>
          </div>
        </div>
      </div>
    </div>
    <div class="card-container col-sm-4 my-3">
      <div class="position-relative">
        <div class="card card-front">
          <img class="card-img-top" src="https://via.placeholder.com/400x200/ff2222" alt="Card image cap">
          <div class="card-body">
            <h5 class="card-title text-center">4</h5>
          </div>
        </div>
        <div class="card card-back" >
          <div class="card-body">
            <p  class="font-weight-light">Some back text #4 <br>
              Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
              tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
              quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
              consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
              cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
              proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
            </p>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<style>
.container{
  box-shadow: 0 0 0 1px red;
}
.card-container {
  perspective: 200rem;
}
.card {
  position: relative;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transition: all 0.9s;
  backface-visibility: hidden;
  border: 1px solid lightpink;
}
.card-front{
  background-color: #ccc;
}
.card-back {
  top: 0;
  position: absolute;
  transform: rotateY(180deg);
  height: 100%;
  overflow: hidden;
  overflow-y: auto;
}
.card-container:hover .card-front {
  transform: rotateY(180deg);
}
.card-container:hover .card-back {
  transform: rotateY(0deg);
}
</style>

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

Tool for Generating HTML Comments

Is there a tool out there that can generate coded comments for HTML? I've noticed some developers use elaborate 'commented out titles' to keep track of their code. These titles are made up of characters that resemble actual text, creating a ...

Ways to emphasize the currently active tabs on a navigation bar

How can I highlight the active tabs and shift it when clicking on another link? $(function() { $('.nav ul li').on('click', function() { $(this).parent().find('li.active').removeClass('active'); $(this).a ...

Capturing information from a modifiable table using Javascript

Creating an HTML Editable table with the property CONTENTEDITABLE, each TD has a unique ID (even though it's a long process and I only have basic JS knowledge) to keep track of the information inside. The table is wrapped by a form. At the end of the ...

Creating an animated CSS growth effect using inline SVG

I'm trying to make these circuits look like they are running downwards. I have the right movement, but I can't seem to start them at their actual size, causing them to appear skewed at the beginning of the animation and then scaling to full size ...

What could be causing my checkbox to become unresponsive and fail to reset?

I recently created a function to update my page title when the toggle button is clicked, but unfortunately, it seems to be getting stuck after the click. I can't seem to pinpoint the issue with this function, as I've tried coding it in both JQuer ...

JavaScript button not responding to click event

Here is the initial structure that I have: <section id="content"> <div class="container participant"> <div class="row"> <div class="input-group"> <div class="input-group-prepend"> ...

Ensure that all floating divs have uniform height

How can I ensure that two divs placed side by side will always have the same height, even when one of them changes based on content, using only CSS? I created a fiddle to demonstrate. I want both the red and blue divs to have equal heights... http://jsfi ...

What steps can I take to enhance my script and prevent the password field from being displayed alongside the username

I am currently working on a script that dynamically displays your username in the Login button as you type it. However, I have encountered an issue where the password is also being displayed, which is unacceptable. Additionally, I would like to enhance my ...

Avoiding Navbar Link Clicks due to Z-Index

Is it possible for the z-index or positioning of an element to hinder a user from clicking on a navbar link? When I visit my website , I find that I am unable to click on any of the links in the navigation bar. Could this be due to the z-index or position ...

What is the best way to add variables into HTML using a PHP array?

UPDATE: I have made some adjustments to the code below, but wanted to keep the original text intact for reference. There are additional aspects I am considering aside from the initial question. In my array, there is an HTML list that PHP shuffles to displ ...

highlight the selected option in the ng-repeat list of items

Looking for some assistance with a coding problem I'm having. I keep running into an error when trying to make a selected item in an ng-repeat list highlight. The CSS style is only applied on the second click, not the first. Additionally, I need to en ...

Displaying the HTML code for a dynamically generated table

Can the generated HTML code be retrieved when dynamically creating a table using v-for loops in Vue? <table> <tr> <th colspan="99">row 1</th> </tr> <tr> <th rowspan="2">row 2< ...

The bootstrap fails to load when accessing on a different computer within the same network (although it functions properly on my personal laptop)

Why does Bootstrap fail to load when accessing the website from another computer on the same network, but works perfectly on my own laptop? Here are the comparison images: https://i.sstatic.net/oBu5j.jpg https://i.sstatic.net/XDRqj.jpg On my laptop: htt ...

Double trigger caused by selection and modification events

I'm struggling to find a solution for this particular issue. I have implemented a textbox-like control that utilizes .select and .change events. As the user types, the textbox provides options to select from. The issue arises when selecting a value t ...

``In JavaScript, the ternary conditional operator is a useful

I am looking to implement the following logic using a JavaScript ternary operation. Do you think it's feasible? condition1 ? console.log("condition1 pass") : condition2 ? console.log("condition2 pass") : console.log("It is different"); ...

The overflow:hidden property seems to be completely ineffective

Recently, I encountered an issue with my small HTML and CSS script. Everything was functioning properly until, out of nowhere, a scroll bar appeared on my page even though I had set overflow to hidden. I'm puzzled as to why this scroll bar is showing ...

Creating a CSS image grid without relying on object-fit: cover can be achieved by

My goal is to create a horizontal masonry grid similar to Adobe Stock and Shutterstock without altering image aspect ratios by using object fit or any other stretching techniques. All the solutions I've found so far involve object-fit: cover, which c ...

Tips for making a central CSS gradient

I am looking to create a background gradient similar to the one shown in the image (Load more news) and also add a border style. Any suggestions on how to achieve this would be greatly appreciated. ...

Switching FontAwesome Stacks on Hover

I'm facing a challenge with creating a quick animation that replaces an entire span on hover. How can I successfully approach replacing a span on hover? <h1>I am looking to have this element replaced...</h1> <span class="fa-stack fa-lg ...

Prevent tooltips from disappearing when hovering over them

I need assistance in keeping my tooltip visible when the mouse is on it, but hiding it when the mouse moves out. Can you please help me achieve this? $(document).ready(function(){ $('[rel=tooltip]').bind('mouseover', function() { ...