Flip a card on mouse click

Viewed 3160

In a razor component of my Blazor application, I'm trying to implement a card-flip on mouse click. Every time the user clicks the tile the box will flip and display the content on the back and when the user clicks the back tile it will flip again to the front tile. I'm able to achieve it through hover but I wanted to achieve it through mouse click. Here's my code for more info:

.card-container {
    display: grid;
    perspective: 700px;
}

.card-flip {
    display: grid;
    grid-template: 1fr / 1fr;
    grid-template-areas: "frontAndBack";
    transform-style: preserve-3d;
    transition: all 0.7s ease;
}

.front {
    grid-area: frontAndBack;
}

.back {
    grid-area: frontAndBack;
    transform: rotateY(-180deg);
}

.card-container:hover .card-flip {
    transform: rotateY(180deg);
}
<!-- Card 2 -->
    <div class="col-md-4 card-container">
      <div class="card-flip">
        <!-- Card 2 Front -->
        <div class="card front">
          <img src="https://nikonrumors.com/wp-content/uploads/2014/03/Nikon-1-V3-sample-photo.jpg" class="card-img-top img-fluid">
          <div class="card-block">
            <h4 class="card-title">Front Card Title</h4>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
          </div>
        </div>
        <!-- End Card 2 Front -->

        <!-- Card 2 Back -->
        <div class="card back">
          <div class="card-header">
            Featured
          </div>
          <div class="card-block">
            <h4 class="card-title">Special title treatment</h4>
            <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
        <!-- End Card 2 Back -->
      </div>
    </div>
    <!-- End Card 2 -->

2 Answers

Easier than I thought, no javascript required. Nice effect! So here I am adding/toggling a class based on click.

<div class="col-md-4 card-container" @onclick="FlipMe">
    <div class="card-flip @flipCss">
...
@code {
    bool flipped;
    void FlipMe() => flipped = !flipped;    
    string flipCss => flipped ? "im-flipped" : "";
}
/*.card-container:hover .card-flip {
    transform: rotateY(180deg);
}*/

.im-flipped {
    transform: rotateY(180deg);
}

Here is a working BlazorFiddle

I recommend to do it with javascript or jquery, it's very easy, however, if you want to do it with CSS only, here is an example, but the <a> elements will no longer work.

.card-container {
    display: grid;
    perspective: 700px;
    position:relative;
}

.card-flip {
    display: grid;
    grid-template: 1fr / 1fr;
    grid-template-areas: "frontAndBack";
    transform-style: preserve-3d;
    transition: all 0.7s ease;
}

.front {
    grid-area: frontAndBack;
}

.back {
    grid-area: frontAndBack;
    transform: rotateY(-180deg);
}

#flipCard:checked + .card-flip {
    transform: rotateY(180deg);
}

label.flipCard{
  position:absolute;
  top:0;
  left:0;
  height:100%;
  width:100%;
  cursor:pointer;
  z-index:9999;
}
<!-- Card 2 -->

    <div class="col-md-4 card-container">
    <label class="flipCard" for="flipCard"></label>
    <input type="checkbox" id="flipCard" hidden />
      <div class="card-flip">
        <!-- Card 2 Front -->
        <div class="card front">
          <img src="https://nikonrumors.com/wp-content/uploads/2014/03/Nikon-1-V3-sample-photo.jpg" class="card-img-top img-fluid">
          <div class="card-block">
            <h4 class="card-title">Front Card Title</h4>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
          </div>
        </div>
        <!-- End Card 2 Front -->

        <!-- Card 2 Back -->
        <div class="card back">
          <div class="card-header">
            Featured
          </div>
          <div class="card-block">
            <h4 class="card-title">Special title treatment</h4>
            <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
        <!-- End Card 2 Back -->
      </div>
    </div>
    <!-- End Card 2 -->

Related