Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1819

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T11:41:09+00:00 2024-11-25T11:41:09+00:00

CSS: 3 Sided Card

  • 62k

The other day I was tasked with creating a “3 sided card” that flips. It proved more challenging than I thought, though the solution isn't that complex.

What we're building

Calling it a “3 sided card” is misleading, but when I first started trying to build this, that's all I could think to call it.

In reality, it is pentahedron — in particular, a triangular prism. Imagine a cube where you remove one side, and then fold the adjoining two together.

The final product can be found at the end.

The gist

The structure consists of three divs making up the sides, wrapped in a div for the prism.

  <div class="prism">   <div class="side one">1</div>   <div class="side two">2</div>   <div class="side three">3</div> </div>   
Enter fullscreen mode Exit fullscreen mode

We don't need to worry about the top and bottom.

The css is little more complicated. First, some non-critical styling to make it look nice.

  .prism {   margin: auto;   width: 100px;   aspect-ratio: 1 / 1; }  .side {   border: 1px solid black;   display: flex;   flex-direction: column;   align-items: center;   justify-content: center; }  .one {   background-color: #63cdda; }  .two {   background-color: #ea8685; }  .three {   background-color: #f5cd79; }   
Enter fullscreen mode Exit fullscreen mode

The good stuff

Making this relies heavily on the rotate3d function, which I'll talk about in the next section.

For now, let's define the css for the prism and it's sides.

css

  .prism {   transition: transform 0s linear;   transform-style: preserve-3d; }   
Enter fullscreen mode Exit fullscreen mode

On the prism div, we define a transition for when the transform property changes. This will define how fast the card changes.

It's currently set at 0s for accessibility.

Below it we can define a media query for if the user has no preference for reduced motion.

  @media (prefers-reduced-motion: no-preference) {   .prism {     transition: transform 0.5s linear;   } }   
Enter fullscreen mode Exit fullscreen mode

This will give the card a spin.

We will apply a transform in the next section.

Now the sides.

  .side {   position: absolute;   height: 100%;   width: 100%; }   
Enter fullscreen mode Exit fullscreen mode

This ensures each side takes up the whole face of the prism.

Lastly, we add a transform property to each side.

  .side.one {   transform: rotateY(0deg) translateZ(29px); }  .side.two {   transform: rotateY(120deg) translateZ(29px); }  .side.three {   transform: rotateY(240deg) translateZ(29px); }   
Enter fullscreen mode Exit fullscreen mode

Each side is rotated on it's Y-axis 120deg (i.e. 360/3).

The translateZ property was a bit of a guessing game. It's related to the width of the prism, but I had to figure out the right value by trial-and-error.

Html and JavaScript

Now, I haven't talk about rotate3d property yet. Before we add it, we're going to add a data attribute to the prism.

  <div class="prism" data-degrees="0">   <div class="side one">1</div>   <div class="side two">2</div>   <div class="side three">3</div> </div>   
Enter fullscreen mode Exit fullscreen mode

Now we can manipulate the data attribute with JS and apply a rotate3d value based off of the data attribute.

  function toggleDegrees(e) {   const currentDegrees = +e.currentTarget.dataset.degrees || 0;   const newDegrees = currentDegrees + 120;   e.currentTarget.style.transform = `rotate3d(0, -1, 0, ${newDegrees}deg)`;   e.currentTarget.dataset.degrees = newDegrees; }  document.querySelector(".prism").addEventListener("click", toggleDegrees);   
Enter fullscreen mode Exit fullscreen mode

By constantly increasing the degree, instead of setting it back to 0, the card will keep spinning in one direction.

Live

Conclusion

I hope someone else finds this useful. I wanted to get thoughts typed out before I forget how to do this.

Any suggestions are welcomed!

cssshowdevtutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.