Home  >  Article  >  Web Front-end  >  CSS Layout Tips: Best Practices for Implementing Floating Card Flip Effect

CSS Layout Tips: Best Practices for Implementing Floating Card Flip Effect

WBOY
WBOYOriginal
2023-10-16 09:07:411141browse

CSS Layout Tips: Best Practices for Implementing Floating Card Flip Effect

CSS Layout Tips: Best Practices for Implementing the Floating Card Flip Effect

The floating card flipping effect is a very common effect in web design, which can make the page Looks more dynamic and vivid. This article will introduce how to use CSS to achieve the floating card flip effect and give specific code examples.

Before we begin, we need to clarify the basic principles of floating card flipping. In fact, this effect can be achieved by using the CSS transform attribute. The flip effect is achieved by dividing the card into two parts, front and back, and rotating them separately. The following is a specific code example:

<html>
  <head>
    <style>
      .card {
        width: 300px;
        height: 200px;
        perspective: 1000px;
      }

      .card-inner {
        width: 100%;
        height: 100%;
        transition: transform 0.5s;
        transform-style: preserve-3d;
      }

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

      .card-front,
      .card-back {
        position: absolute;
        width: 100%;
        height: 100%;
        backface-visibility: hidden;
      }

      .card-front {
        transform: rotateY(0deg);
        background-color: #e74c3c;
      }

      .card-back {
        transform: rotateY(180deg);
        background-color: #3498db;
        color: #fff;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    </style>
  </head>
  <body>
    <div class="card">
      <div class="card-inner">
        <div class="card-front">
          <h2>正面</h2>
        </div>
        <div class="card-back">
          <h2>背面</h2>
        </div>
      </div>
    </div>
  </body>
</html>

In the above code, we create a div named "card" with a width of 300px and a height of 200px. By setting the perspective property, we can create a perspective effect to make the flip look more realistic.

Next, we create a div called "card-inner" that contains the front and back of the card. By setting the transform-style property to preserve-3d, we can maintain the perspective relationship between the front and back faces. At the same time, by setting the transition attribute, we can achieve a smooth transition of the flip effect.

When the mouse hovers over the card, we can achieve the flip effect of the card by setting the :hover pseudo-class selector and setting the value of the transform attribute of .card-inner to rotateY(180deg).

Next, we created two divs, .card-front and .card-back, which represent the front and back of the card respectively. By setting the backface-visibility property to hidden, we can remove the flickering effect when the card is flipped.

In .card-front, we can add any content to show the front of the card. In .card-back, we used a background color and centered text to show the back of the card.

The code example given above is just a simple implementation of the floating card flip effect. You can modify and expand it according to your own needs. I hope that the introduction in this article can help you better master CSS layout skills and achieve more cool effects.

The above is the detailed content of CSS Layout Tips: Best Practices for Implementing Floating Card Flip Effect. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn