Using transform translate to create a 3d affect - html

I am not able to use transform: translateZ to change the position of back div to create a 3d affect . There are no changes at all in the image. I have used perspective and perspective origin to try and create a 3d affect. The front and back div are respectively the front and back sides of a cube. Here is the below code written in css and html respectively
:root {
--boxColor: #0ff7
}
body {
align-items: center;
justify-content: center;
display: flex;
background-color: black;
min-height: 100vh;
font-size: 50px;
}
.scene {
position: relative;
transform-style: preserve-3d;
}
.cube-container {
perspective: 100px;
perspective-origin: 50% 0%;
}
.cube {
height: 100px;
width: 100px;
}
.front {
height: 100px;
width: 100px;
background: rgb(62, 86, 226);
opacity: 0.5;
}
.back {
height: 100px;
width: 100px;
background: rgb(62, 86, 226);
opacity: 0.5;
top: -100vh;
position: absolute;
transform: translateZ(100px)
}
<div class="scene">
<div class="cube-container">
<div class="cube">
<div class="front">
</div>
<div class="back">
</div>
<div class="top">
</div>
<div class="bottom">
</div>
<div class="left">
</div>
<div class="right">
</div>
</div>
</div>
</div>

You missed transform-style: preserve-3d; on .cube and I would also use position: absolute; for .front. I dont understand the perspective perfectly, but its common to use higher numbers like: perspective: 500px;.
Hope it helped! :)
:root {
--boxColor: #0ff7
}
body {
align-items: center;
justify-content: center;
display: flex;
background-color: black;
min-height: 100vh;
font-size: 50px;
}
.scene {
position: relative;
transform-style: preserve-3d;
}
.cube-container {
perspective: 500px;
perspective-origin: 50% 0%;
}
.cube {
height: 100px;
width: 100px;
transform-style: preserve-3d;
transform: rotateY(20deg);
}
.front {
position:absolute;
height: 100px;
width: 100px;
background: red;
opacity: 0.5;
}
.back {
position: absolute;
height: 100px;
width: 100px;
background: blue;
opacity: 0.9;
transform: translateZ(-100px);
}
<div class="scene">
<div class="cube-container">
<div class="cube">
<div class="front">
</div>
<div class="back">
</div>
<div class="top">
</div>
<div class="bottom">
</div>
<div class="left">
</div>
<div class="right">
</div>
</div>
</div>
</div>

Related

Why is the Card-Flip is not working in CSS

When I am applying hover on the card container it is not working or it is not flipping but I when I remove the hover it flips to the back side, I have tried everything that I know but still no progress on that.
`
import React from 'react'
import './Summary.css'
function Summary() {
return (
<div className='summary'>
<img className="body__image" src={require("./Images/background3Color.jpg")}></img>
<div className='box__container'>
<div className='boxes'>
<div className='box__inner'>
<div className='box__front'>
<img className='boxes__pics' src={require('./Images/AboutMe.jpg')}></img>
</div>
<div className='box__back'>
<div className='headline'></div>
<div className='education'>
<h2>Bachelor Of Technology in Computer Science and Tech.</h2>
</div>
<div className='work-ex'>
<p>Worked as an Application Dvelopment Associate in Accenture for 1 year</p>
</div>
</div>
</div>
</div>
<div className='boxes'>
<div className='box__inner'>
<div className='box__front'>
<img className='boxes__pics' src={require('./Images/Contacts.jpg')}></img>
</div>
<div className='box__back'>
<div className='headline'></div>
<div className='education'>
<h2>Bachelor Of Technology in Computer Science and Tech.</h2>
</div>
<div className='work-ex'>
<p>Worked as an Application Dvelopment Associate in Accenture for 1 year</p>
</div>
</div>
</div>
</div>
</div>
</div>
)
}
export default Summary
`
I was expecting the flip will work but to no avail.
I have tried to add hover to both box__inner and boxes container but still it would not flip.
Here is what I tried:
`
.summary{
position: relative;
height: 100vh;
z-index: -1;
}
.body__image{
height: 100vh;
}
.box__container{
position: absolute;
/* background-color: aqua; */
top: 0px;
width: 100%;
height: 100%;
/* opacity: 0.4; */
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
align-items: center;
}
.boxes{
height: 320px;
width: 300px;
margin: 0px 10px 0px 10px;
position: relative;
background-color: rgba(107,186,167,0.7);
perspective: 1000px;
overflow: hidden;
}
.box__inner{
position: absolute;
width: 100%;
height: 100%;
text-align: center;
transition: all 0.8s ease;
transform-style: preserve-3d;
}
.boxes__pics{
height: 100%;
width: 100%;
opacity: 0.7;
}
.box__inner:hover{
cursor: pointer;
transform: rotateY(180deg);
}
.box__front, .box__back{
position: absolute;
width: 100%;
height: 100%;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
}
.box__front{
}
.box__back{
transform: rotateY(180deg);
}
`
The z-index was causing the main problem after removing it from the parent div the hover effect worked fine.

How to vertically center a horizontal scrolling div

I am trying to have a horizontal scrolling page and want the scroll to be 75% of my viewport and vertically aligned. But for some reason the div doesn't get aligned vertically. I tried doing margin left, right, top and bottom but resulted in my code acting unexpectedly. Even I tried using "display flex" but didn't work at all.
<div class="main_background">
<div class="wrapper">
<div class="slide one"></div>
<div class="slide two"></div>
<div class="slide three"></div>
<div class="slide four"></div>
</div>
</div>
and my CSS file...
.slide{
width: 100vw;
height: 80vh;
}
.wrapper{
display: flex;
flex-direction: row;
width: 300vw;
transform: rotate(90deg) translateY(-100vh);
transform-origin:top left;
margin-top: 90px;
}
.one{
background-color: orchid;
}
.two{
background-color: orangered;
}
.three{
background-color: pink;
}
.four{
background-color: green;
}
.main_background{
width: 100vh;
height: 100vw;
background-image: linear-gradient(green, greenyellow);
transform: rotate(-90deg) translateX(-100vh);
transform-origin:top left;
overflow-y: scroll;
overflow-x: hidden;
position: absolute;
}
It would be greatly helpful if someone can help me with this! Thanks
Try this out. I gave the .wrapper container a height of 100vh and aligned the flexbox along both axes.
.slide {
width: 100vw;
height: 80vh;
}
.wrapper {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: row;
width: 300vw;
transform: rotate(90deg) translateY(-100vh);
transform-origin:top left;
margin-top: 90px;
}
.one {
background-color: orchid;
}
.two {
background-color: orangered;
}
.three {
background-color: pink;
}
.four {
background-color: green;
}
.main_background {
width: 100vh;
height: 100vw;
background-image: linear-gradient(green, greenyellow);
transform: rotate(-90deg) translateX(-100vh);
transform-origin:top left;
overflow-y: scroll;
overflow-x: hidden;
position: absolute;
}
<div class="main_background">
<div class="wrapper">
<div class="slide one"></div>
<div class="slide two"></div>
<div class="slide three"></div>
<div class="slide four"></div>
</div>
</div>

CSS Card Effect not working on Mobile Safari

I've built a card flip effect that seems to work on Safari Mobile regarding the "flip" aspect of the effect. However, the card is not displaying the correct image upon flipping. I'm using the effect as a "Before and After", using separate images on each side of the card. I'll post my code. Thank you.
.beforeafter {
margin: 10px auto;
text-align: center;
}
.card-container {
cursor: pointer;
height: 300px;
perspective: 600;
position: relative;
width: 300px;
display: inline-block;
}
.clientcard {
height: 100%;
position: absolute;
transform-style: preserve-3d;
transition: all .5s ease-in-out;
width: 100%;
}
.clientcard:hover {
transform: rotateY(180deg);
-webkit-transform: -webkit-translateY(180deg);
}
.clientcard .side {
backface-visibility: hidden;
border-radius: 2px;
height: 100%;
position: absolute;
overflow: hidden;
width: 100%;
}
.clientcard .back {
background: #eaeaed;
color: #0087cc;
line-height: 150px;
text-align: center;
transform: rotateY(180deg);
}
<div class="beforeafter">
<div class="card-container">
<div class="clientcard">
<div class="side"><img src="img/sean1.JPG"></div>
<div class="side back"><img src="img/sean1copy.JPG"></div>
</div>
</div>
<div class="card-container">
<div class="clientcard">
<div class="side"><img src="img/sean2.JPG"></div>
<div class="side back"><img src="img/sean2copy.JPG"></div>
</div>
</div>
<div class="card-container">
<div class="clientcard">
<div class="side"><img src="img/sean3copy.jpg"></div>
<div class="side back"><img src="img/sean3.jpg"></div>
</div>
</div>
<div class="card-container">
<div class="clientcard">
<div class="side"><img src="img/gwork.jpg"></div>
<div class="side back"><img src="img/alana2.jpeg"></div>
</div>
</div>
</div>
I believe you want to rotate on the Y axis in the negitive -180deg. I improved your code a bit and don't mind the shitty format it was translated from sass and if you want another card with flip copy and past that code
.card {
perspective: 150rem;
-moz-perspective: 150rem;
position: relative;
height: 52rem; }
.card_side {
height: 52rem;
transition: all 2s ease;
position: absolute;
top: 0;
left: 0;
width: 100%;
backface-visibility: hidden;
}
.card_side-front {
background-color: blue;
}
.card_side-back {
transform: rotateY(180deg);
}
.card_side-back-1 {
background-image: linear-gradient(to right bottom, #ffb900, #ff7730);
}
.card:hover .card_side-front {
transform: rotateY(-180deg);
}
.card:hover .card_side-back {
transform: rotateY(0);
}
.card_picture-1 {
background-color: blue;
}
<div class="row">
<div class="col-1-of-3">
<div class="card">
<div class="card_side card_side-front">
<h4 class="card_heading">
<span class="card_heading-span card_heading-span-1">card front text</span>
</h4>
</div>
<div class="card_side card_side-back card_side card_side-back-1">
<div class="card_cta">
<div class="card_price-box">
<p class="card_price-only">back text</p>
</div>
</div>
</div>
</div>
</div>

Responsive 3d cube - bottom moving on y axis with viewport resize

I'm trying to make a responsive rectangular prism with depth that will resize with the window. But, no matter what I do, I can't get the bottom side to work correctly. It only lines up on the Z axis when the viewport has a square aspect ratio.
I had the same issue with the top side moving when I had it set to translateY(-50vh), but it starting behaving how I wanted when I changed it to -50vw (for some reason I can't figure out).
How can I make the bottom responsive like the top?
https://codepen.io/anon/pen/jwBQGR
.scene {
width: 100vw;
height: 100vh;
margin: 0 auto 0;
perspective: 3000px;
}
.rectangle {
position: relative;
width: 100%;
height: 100%;
transform-style: preserve-3d;
transform: translateZ(-50vw);
}
.side {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: visible;
}
.back {
transform: translateZ(-50vw);
background-color: saddlebrown;
}
.top {
background-color: lightblue;
height: 100vw;
transform: translateY(-50vw) rotateX(-90deg);
}
.bottom {
background-color: green;
height: 100vw;
transform: translateY(50vh) rotateX(90deg);
}
.front {
opacity: 0;
pointer-events: none;
}
.left {
background-color: sandybrown;
transform: translateX(-50vw) rotateY(-90deg);
}
.right {
background-color: brown;
transform: translateX(50vw) rotateY(90deg);
}
<div class="scene">
<div class="rectangle" id="cubeID">
<div class="side back">
<h1>Back</h1>
</div>
<div class="side top">
<h1>Top </h1>
</div>
<div class="side bottom">
<h1>Bottom</h1>
</div>
<div class="side front">
<h1>Front</h1>
</div>
<div class="side left">
<h1>Left</h1>
</div>
<div class="side right">
<h1>Right</h1>
</div>
</div>
</div>
The point is to use the transform-origin property (see transform-origin on MDN). This way, you can control the rotation axis for each side rather than repositioning them with translation after they have been rotated as you did.
Here is an example :
body,html{margin:0;}
.scene {
width: 100vw;
height: 100vh;
margin: 0 auto;
perspective: 3000px;
}
.rectangle {
position: relative;
width: 100%;
height: 100%;
transform-style: preserve-3d;
}
.side {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: visible;
}
.back {
transform: translateZ(-100vw);
background-color: saddlebrown;
}
.top {
background-color: lightblue;
height:100vw;
transform-origin:50% 0;
transform: rotateX(-90deg);
}
.bottom {
background-color: green;
height:100vw;
bottom:0;
transform-origin: 50% 100%;
transform: rotateX(90deg);
}
.front {
opacity: 0;
pointer-events: none;
}
.left {
background-color: sandybrown;
transform-origin:0 50%;
transform: rotateY(90deg);
}
.right {
background-color: brown;
transform-origin:100% 50%;
transform: rotateY(-90deg);
}
<div class="scene">
<div class="rectangle" id="cubeID">
<div class="side back"><h1>Back</h1></div>
<div class="side top"><h1>Top </h1></div>
<div class="side bottom"><h1>Bottom</h1></div>
<div class="side front"><h1>Front</h1></div>
<div class="side left"><h1>Left</h1></div>
<div class="side right"><h1>Right</h1></div>
</div>
</div>

Group images that contains image overlay to the centre of the page

I am trying to center a group (a table with 3x3) of pictures to the center of the webpage, I manage to do it before adding image overlay to it. But since I added image overlay, the images are appearing on top left of the webpage. How do i group them and center them, also how am I supposed to get the image location so that when I set the image overlay, it goes to the specific picture as each picture will have different image overlay text.
CSS
.container {
position: relative;
width: 100px;
height: 100px;
}
.image {
display: block;
width: 100px;
height: 100px;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
}
.container:hover .overlay {
opacity: 1;
}
.text {
color: red;
font-size: 20px;
font-weight: bold;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
HTML
<div style="text-align:center">
<div class="container">
<img src="wheel1.jpg" class="image">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<div class="container">
<img src="wheels2.jpg" class="image">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<div class="container">
<img src="wheel3.jpg" class="image"">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
`
You could center it using flexbox. Change your main div
<div style="text-align-center;">
......
</div>
to
<div style="display: flex; flex-direction: column;align-items: center;">
.....
</div>
And it should work.
.wrapper{
display: flex;
flex-direction: row;
justify-content: center;
}
.container {
position: relative;
width: 100px;
height: 100px;
}
.image {
display: block;
width: 100px;
height: 100px;
border: 1px solid red;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
}
.container:hover .overlay {
opacity: 1;
}
.text {
color: red;
font-size: 20px;
font-weight: bold;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
<div class="wrapper">
<div class="container">
<div class="image"></div>
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<div class="container">
<div class="image"></div>
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<div class="container">
<div class="image"></div>
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
</div>
Here is the fiddle.