Example code here:
http://jsfiddle.net/gat5S/1/
I want to create a "card" that can be flipped via CSS rotateY. The rotation is based on David Walsh's CSS Flip Animation and in principle works well.
However, in my example, I have an :after pseudo-element for each side of the card that I use to create a paper-like shadow effect. This pseudo-element is put behind the card via negative z-index:-1;. This works for the "front" card but not after rotation and not for the "back" card.
The jsFiddle example code shows my problem (currently only containing webkit prefixes). In the initial state everthing looks as intended. The :after pseudo-element saying "INVISIBLE" is only partly visible. When you flip the card via the toggle button two things happen:
The front card :after element becomes visible. This is not intended.
The back card :after element is visible but also in the box where z-index:-1; should make it invisible. Also not intended.
I managed to solve 1. by using JavaScript to add a class to the .front which changes the visibility of :after. However, I cannot manage to solve 2. I tried different z-index values but these don't seem to have any effect at all.
I wonder if there is a clever way to solve both problems. Any help appreciated!
I've got it working
demo
CSS
.front, .back {
text-align: center;
background-color: #FFF9EC;
}
.front:after {
content: ' INVISIBLE ';
position: absolute;
width: 40%;
height: 10px;
left: 70px;
bottom: 12px;
z-index: -1;
-webkit-transform: translateZ(-1px);
-webkit-backface-visibility: hidden;
}
.back:after {
content: ' INVISIBLE ';
position: absolute;
width: 40%;
height: 10px;
left: 70px;
bottom: 12px;
z-index: -1;
-webkit-transform: translateZ(-1px);
-webkit-backface-visibility: hidden;
}
.flip-container {
-webkit-perspective: 1000;
}
/* flip the pane when hovered */
.flip-container.flip .flipper {
-webkit-transform: rotateY(180deg);
}
.flip-container, .front, .back {
width: 100px;
height: 100px;
}
/* flip speed goes here */
.flipper {
-webkit-transition: 0.6s;
-webkit-transform-style: preserve-3d;
position: relative;
left: 30px;
}
/* hide back of pane during swap */
.front, .back {
-webkit-backface-visibility: hidden;
position: absolute;
top: 0;
left: 0;
}
.back h1 {
-webkit-backface-visibility: hidden;
}
/* back, initially hidden pane */
.back {
-webkit-transform: rotateY(180deg) translateZ(1px);
-webkit-transform-style: preserve-3d;
-webkit-backface-visibility: hidden;
}
Mainly, I have changed the z-index to translateZ; that is the way you should work in 3D.
There was also some issue with the backface visibility : hidden not propagating to the child elements.
Related
In many tutorial, they use backface-visibility:hidden to make a flippable card. But it(card) works fine without backface-visibility:hidden. So why they use it? Any solution.
Css:
.scene {
width: 200px;
height: 260px;
perspective: 600px;
}
.card {
width: 100%;
height: 100%;
position: relative;
transition: transform 1s;
transform-style: preserve-3d;
}
.card:hover{
transform:rotateY(180deg);
}
.card__face {
position: absolute;
height: 100%;
width: 100%;
backface-visibility: ;
/*works fine without backface-visibility:hidden */
}
.card__face--front {
background: red;
}
.card__face--back {
background: blue;
transform: rotateY( 180deg );
}
Link: https://codepen.io/nur49/pen/QWKaQRv
What it does is determine whether the image will show when flipped. A use case would be if you only have content on one side and not the other as in this example. If you don't add the css rule, you will still see it through the other side, but obviously backwards. Typically you do see flipcards with info on both sides, so most cases it might not be entirely necessary. Below is the code example from here.
body {
font-family: sans-serif;
font-size: 0.8rem;
}
div {
position: relative;
height: 60px;
width: 60px;
background-color: red;
transform: rotateY(180deg);
}
#div1 {
-webkit-backface-visibility: hidden; /* Safari */
backface-visibility: hidden;
}
#div2 {
-webkit-backface-visibility: hidden; /* Safari */
backface-visibility: visible;
}
<h1>The backface-visibility Property</h1>
<p>This example shows the back face of two div elements.</p>
<p>This div element has "backface-visibility: hidden", and the back face of the div element is invisible:</p>
<div id="div1">DIV 1</div>
<p>This div element has "backface-visibility: visible", and the back face of the div element shows a mirror image of the front face:</p>
<div id="div2">DIV 2</div>
<p><strong>Note:</strong> The backface-visibility property is not supported in Internet Explorer 9 and earlier versions. Safari supports it with a webkit prefix.</p>
I was trying to implement the paper flip transition using css, so in order to achieve that the tasks that I needed were:
I had to move the div from the current position to the left. I was able to achieve it using translate property.
I also had to flip the div. I was able to achieve it using scaleX(-1)
Finally I had to flip it in curve.
How can I achieve the final part? Here is a link to CSSDesk. I have tried the flip transition using hover.
I don't know if it's what ou were looking for, but here is a quick example :
/* entire container, keeps perspective */
.flip-container {
perspective: 1500px;
margin: 0 auto;
margin-top:100px;
}
/* flip the pane when hovered */
.flip-container:hover .flipper, .flip-container.hover .flipper {
transform: rotateY(180deg);
}
.flip-container, .front, .back {
width: 320px;
height: 480px;
}
/* flip speed goes here */
.flipper {
transition: 0.6s;
transform-style: preserve-3d;
transform-origin: 100%;
position: relative;
}
/* hide back of pane during swap */
.front, .back {
backface-visibility: hidden;
position: absolute;
top: 0;
left: 0;
}
/* front pane, placed above back */
.front {
background-color:red;
z-index: 2;
/* for firefox 31 */
transform: rotateY(0deg);
}
/* back, initially hidden pane */
.back {
background-color:green;
transform: rotateY(180deg);
}
<div class="flip-container" ontouchstart="this.classList.toggle('hover');">
<div class="flipper">
<div class="front">
<p>FRONT</p>
</div>
<div class="back">
<p>BACK</p>
</div>
</div>
</div>
The key to acheive 3d effect is perspective: 1500px;
This example is based on this amazing article which explain the property
So ive been struggling with this set of code for a while now and I am so close to finishing it.
First problem was getting my images to be responsive to height and width, but after some discussions I was able to figure out how to make it responsive by simplifying the code and using colors as placeholders but now I cant figure out how to re-add the background images.
Here is the OG codepen, http://codepen.io/anon/pen/YypXjw
and here it is now, http://codepen.io/anon/pen/rOWXzW
* { color: #fff; }
.flip-container {
width: 33.333%;
padding-bottom: 33.333%;
float: left;
}
.flip-container:hover .flipper {
-webkit-transform: rotateY(180deg);
transform: rotateY(180deg);
}
.flipper {
-webkit-transition: 0.6s;
-webkit-transform-style: preserve-3d;
transition: 0.6s;
transform-style: preserve-3d;
position: relative;
}
.front {
position: absolute;
width: 100%;
padding-bottom: 100%;
}
.front-a {
background: red;
}
.front-b {
background: blue;
}
.front-c {
background: green;
}
I have no idea why I cant get the html to show up as well.
So the code was cleaned up, and now all I need to figure out is how to add a front and back image to the flipping div.
Ive attempted to add front and back divs like the og code but still cant get it to work.
Any help or suggestions is greatly appreciated.
Codepen
You need the front and back classed divs. You can target the front/back of each using pseudo selectors.
<div class="flip-container">
<div class="flipper">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
Background size cover and then each front/back's image.
.front, .back {
background-size: cover;
}
.flip-container:nth-of-type(1) .front {
background-image: url(image.jpg);
}
.flip-container:nth-of-type(1) .back {
background-image: url(image.jpg);
}
http://jsfiddle.net/RtTPU/
I am trying to swap the image "Adam" to "Adam-flipped" on hover, so once the image is hovered over it will swap as it's flipped yet it's not working. Is it possible to do this entirely in CSS?
<div class="flip-container" ontouchstart="this.classList.toggle('hover');">
<div class="flipper">
<div class="front">
<img src="http://adamginther.com/assets/img/adam.jpg" alt="Adam">
</div>
<div class="back">
<img src="http://adamginther.com/assets/img/adam-flipped.jpg" alt="Adam" >
</div>
</div>
</div>
.self-portrait {
border-radius: 400px;
width: 50%;
}
/* entire container, keeps perspective */
.flip-container {
perspective: 1000;
}
/* flip the pane when hovered */
.flip-container:hover .flipper, .flip-container.hover .flipper {
transform: rotateY(180deg);
}
.flip-container, .front, .back {
width: 320px;
height: 480px;
}
/* flip speed goes here */
.flipper {
transition: 0.6s;
transform-style: preserve-3d;
position: relative;
}
/* hide back of pane during swap */
.front, .back {
backface-visibility: hidden;
position: absolute;
top: 0;
left: 0;
}
/* front pane, placed above back */
.front {
z-index: 2;
}
/* back, initially hidden pane */
.back {
transform: rotateY(180deg);
}
Your code does not work because it lacks the prefixes for transform and backface-visibility. Plus you have to tell the children of .flipper to retain the 3d transforms applied to it and calculate the combined effects automatically by transform-style: preserve-3d (needs prefixes, too).
A second problem: You have to set an explicit width on .flip-container or .flipper to ensure the image flips around the center y axis. Leaving it on auto results in different levels of quirkyness on different screen sizes, though.
Note that my code samples contain only the webkit prefix for brevity. Hope this gets the point across.
Relevant changes:
.flip-container {
...
width: 400px;
height: 400px;
}
.flip-container:hover .flipper {
-webkit-transform: rotateY(180deg);
}
.flipper {
...
-webkit-transform-style: preserve-3d;
height: 100%;
width: 100%;
}
.front, .back {
...
-webkit-backface-visibility: hidden;
}
.back {
-webkit-transform: rotateY(180deg);
}
See all the changes on the fixed version of your fiddle: http://jsfiddle.net/marionebl/RtTPU/6/
Version with less verbose markup and sanitized css styles: http://jsfiddle.net/marionebl/6Je6r/1/
I think here is all you want. fiddle
.flip-container:hover .back{
z-index : 10;
}
.back{
z-index:0;
}
You need prefixes for the transform property.
Also .flip-container.hover is useless, and perspective needs units:
/* entire container, keeps perspective */
.flip-container {
-webkit-perspective: 1000px;
perspective: 1000px;
}
/* flip the pane when hovered */
.flip-container:hover .flipper {
transform: rotateY(180deg);
-webkit-transform: rotateY(180deg);
}
corrected fiddle
I want to create a CSS based popup (CSS3 Allowed) without any JavaScript, with a fade transition and scale effect. Something similar to IceCream Sandwitch and JellyBean popup messages.
I have tried the following:
http://jsfiddle.net/OMS_/7UaK4/5/
Main Parts of Code:
HTML
<span class="info"> Info </span>
<div class="message">
<p>
Content
</p>
</div>
CSS
.message {
position: absolute;
top: 100px;
left: 0;
width: 100%;
text-align: center;
opacity: 0;
-webkit-transform: scale(.9, .9);
-webkit-transition: all .18s ease-in-out;
}
.info:hover + .message {
opacity: 1;
-webkit-transform: scale(1, 1);
}
What I am doing is setting the opacity of the element to 0, and on hover of a sibling DOM element, transtion it to 1.
How do I position it in center, both vertically and horizontally?
Also, is this the proper way to make a CSS3 popup?
Can I transition from display: none to display: block ?
Thanks
How do I position it in center, both vertically and horizontally?
Essentially, you would push the popup 50% from the top and left. However, you must go backwards a bit, since you must take into account the width and height of the popup.
.center-of-screen {
position: absolute;
top: 50%; margin-top: /* -(height/2) */;
left: 50%; margin-left: /* -(width/2) */;
}
Source: How to center an object exactly in the center?
Note: -(height/2) and -(width/2) are negative values of half of element's width and height. E.g. if your element is 300px x 200px code is:
.center-of-screen {
position: absolute;
top: 50%; margin-top: -100px;
left: 50%; margin-left: -150px;
}
Also, is this the proper way to make a CSS3 popup?
Yes, if you are talking about a hover popup.
Can I transition from display: none to display: block ?
No. You would go from display: none to display: block with transition only on opacity.
This is possible now by using visibility:
.message {
visibility: hidden;
opacity: 0;
transform: scale(.9);
transition: transform .18s, opacity .18s, visibility 0s .18s; // delay visibility
}
.info:hover + .message {
visibility: visible;
opacity: 1;
transform: scale(1);
transition-delay: 0s;
}