Rotate background image height 100% - html

I'm trying to make my images responsive with some rotation angle (10deg) but my height is increasing too much, and it's not at center and full width.
This is the design (colors are background-image), as you can see, with the rotation, I can't fill all the top side and same with bottom, and another problem, is my full height increase.
*CSS: CODE
.rotate > div {
transform: scale(1) rotate(-10deg) translateX(0px) translateY(0px) skewX(0deg) skewY(0deg);
}
.rotate > div > div > div {
transform: scale(1) rotate(10deg) translateX(0px) translateY(0px) skewX(0deg) skewY(0deg);
}
.full {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.list {
position: relative;
width: 100%;
height: 100%;
}
.item {
height: 50%;
}
.wrapper {
width: 100%;
height: 100%;
}
Thank you so much guys.

Use the images within a pseudo element and allow some overflow to cover the white space:
body {
margin:0;
min-height:500px;
height:100vh;
overflow:hidden;
}
.box {
height:50%;
box-sizing:border-box;
position:relative;
}
.box:before {
content:"";
position:absolute;
top:-40%;
left:-10%;
right:-10%;
bottom:-40%;
transform:rotate(-10deg);
background:var(--i) center/cover no-repeat;
}
.box.first::before {
bottom:0;
}
.box.last::before {
top:0;
}
<div class="box first" style="--i:url(https://picsum.photos/1000/800?image=0)">
</div>
<div class="box last" style="--i:url(https://picsum.photos/1000/800?image=1069)">
</div>

Related

CSS Animation: `backface-visibility` causing cross-browser problems?

I'm developing a flip animation to show new numbers; it's much like an analog clock or calendar with the hinge in the middle.
The approach is straight forward: have a div with:
The bottom half of the first number on one side
The top half of the second number rotated 180 degrees so it's on the back
In order to show the new number, I rotate that whole div around the center of the container, revealing the back of the rotating div:
Number flip animation in latest Firefox
However, in Chrome, the animation doesn't always work. Sometimes half disappears completely until the transition animation is complete and sometimes the old number doesn't render: Number flip animation in latest Chrome with the bottom of the number not appearing till after animation is complete
In Safari 12, it's worse, it doesn't seem to respect backface-visibility, even with the -webkit- prefix:
Safari 12 Number animation, the bottom half of the first number is inverted after animation is complete
Pre-Chromium Edge handles this fine, but new (checked in v83) Edge has the same issue as Chrome.
I've tried messing around with the properties and have looked through other backface-visibility questions here.
Here's the code, hover over the numbers to see the flip:
body {
background: #2e517d;
}
.container {
width: 175px;
height: 192px;
background: #4e9bfa;
position: relative;
left: 50%;
transform: translate(-50%, 50%);
perspective: 1000px;
}
.cover {
width: 175px;
height: 50%;
position: absolute;
top: 96px;
background-color: #34b58c;
transform: rotateX(0deg);
transform-style: preserve-3d;
transform-origin: top;
transition: all 0.5s ease-out;
}
.container:hover .cover {
transform: rotateX(180deg);
}
.flip {
margin: 0;
display: block;
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}
.container p {
font-size: 1000%;
margin: 0;
}
.container>p {
height: 96px;
overflow: hidden;
}
.front-number-bottom {
position: relative;
height: 96px;
overflow: hidden;
background-color: red;
}
.front-number-bottom p {
margin: 0;
position: relative;
top: -96px;
}
.back-number-top {
position: relative;
height: 96px;
overflow: hidden;
}
.back-number-bottom {
height: 96px;
overflow: hidden;
position: relative;
z-index: -1;
}
.back-number-bottom p {
margin: 0;
position: relative;
top: -96px;
}
div.front {
background: red;
}
div.back {
background: green;
transform: rotateX(180deg);
}
<body>
<div class="container">
<p>76</p>
<div id="cover" class="cover">
<div class="flip front">
<div class="front-number-bottom">
<p>76</p>
</div>
</div>
<div class="flip back">
<div class="back-number-top">
<p>77</p>
</div>
</div>
</div>
<div class="back-number-bottom">
<p>77</p>
</div>
</div>
</div>
</body>
Is this a sound approach that can be easily fixed in Chromium browsers and Safari?
Would a different approach be better?
I guess your code is a bit complex. I would simplify your logic like below where you no more need backface-visibility: hidden;
Note the usage of two important things:
the mask that allow me to cut the element and show only 50% of the height (top or bottom). This will make the animation more realistic since each number will have both top and bottom part seperated.
the z-index trick where I apply a transtion that change the z-index exactly at the middle of the animation (when the rotations are at 90deg)1.
.card {
width: 175px;
height: 192px;
position: relative;
z-index:0;
left: 50%;
transform: translate(-50%, 50%);
font-size: 160px;
}
.card span,
.card span::before,
.card span::after {
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
}
.card span {
position:absolute;
z-index:2;
perspective: 1000px;
}
.card span:first-child {
z-index:3;
transition:0s 0.25s all linear;
}
.card span::before,
.card span::after{
content:attr(data-number);
-webkit-mask:linear-gradient(#fff,#fff) top/100% 50% no-repeat;
mask:linear-gradient(#fff,#fff) top/100% 50% no-repeat;
background:red;
transition:0.5s all linear;
transform-style: preserve-3d;
}
.card span::after {
-webkit-mask-position:bottom;
mask-position:bottom;
background:green;
}
.card span:first-child::after {
transform: rotateX(0deg);
}
.card span:last-child::before {
transform: rotateX(-180deg);
}
/* Hover */
.card:hover span:first-child {
z-index:1;
}
.card:hover span:first-child::after {
transform: rotateX(180deg);
}
.card:hover span:last-child::before {
transform: rotateX(0deg);
}
<div class="card">
<span data-number="76"></span>
<span data-number="77"></span>
</div>
The mask can be replaced with clip-path too:
.card {
width: 175px;
height: 192px;
position: relative;
z-index:0;
left: 50%;
transform: translate(-50%, 50%);
font-size: 160px;
}
.card span,
.card span::before,
.card span::after {
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
}
.card span {
z-index:2;
perspective: 1000px;
}
.card span:first-child {
z-index:3;
transition:0s 0.25s all linear;
}
.card span::before,
.card span::after{
content:attr(data-number);
clip-path:polygon(0 0,100% 0,100% 50%,0 50%);
background:red;
transition:0.5s all linear;
transform-style: preserve-3d;
}
.card span::after {
clip-path:polygon(0 50%,100% 50%,100% 100%,0 100%);
background:green;
}
.card span:first-child::after {
transform: rotateX(0deg);
}
.card span:last-child::before {
transform: rotateX(-180deg);
}
/* Hover */
.card:hover span:first-child {
z-index:1;
}
.card:hover span:first-child::after {
transform: rotateX(180deg);
}
.card:hover span:last-child::before {
transform: rotateX(0deg);
}
<div class="card">
<span data-number="76"></span>
<span data-number="77"></span>
</div>
Another optimization using counter and without setting an explicit width/height
.card {
margin:0 5px;
font-family:monospace;
display:inline-block;
text-align:center;
position: relative;
z-index:0;
font-size: 150px;
counter-reset:num calc(var(--n,1) - 1);
}
/* this will defined the height/width*/
.card::after {
content:counter(num);
visibility:hidden;
}
/**/
.card span,
.card span::before,
.card span::after {
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
}
.card span {
z-index:2;
perspective: 1000px;
counter-increment:num;
}
.card span:first-child {
z-index:3;
transition:0s 0.25s all linear;
}
.card span::before,
.card span::after{
content:counter(num);
clip-path:polygon(0 0,100% 0,100% 50%,0 50%);
background:red;
transition:0.5s all linear;
transform-style: preserve-3d;
}
.card span::after {
clip-path:polygon(0 50%,100% 50%,100% 100%,0 100%);
background:green;
}
.card span:first-child::after,
.card:hover span:last-child::before{
transform: rotateX(0deg);
}
.card span:last-child::before {
transform: rotateX(-180deg);
}
.card:hover span:first-child::after {
transform: rotateX(180deg);
}
.card:hover span:first-child {
z-index:1;
}
<div class="card" style="--n:75">
<span></span><span></span>
</div>
<div class="card" style="--n:5">
<span></span><span></span>
</div>
<div class="card" style="--n:100">
<span></span><span></span>
</div>
1 When using linear it's pretty easy but it's more trick with other ease functions. Here is a related question that can help you identify the middfle of ease functions: When exactly does an ease animation reach its midpoint?

Why are 3D cube faces distorted?

I'm designing a rotating cube logo for my portfolio site. After trying all night, for some reason my 3D cube logo is no longer a cube. Two problems:
The shape of the cube is distorted. The .front div is larger than all the other divs for the cube. I can't see why this is happening.
When .container div's animation is commented out, you'll notice the viewer position is head on. I need the view position to be more 'isometric', like the viewer is looking at the edge of the cube from above. I've tried to rotate the Z- and Y-axis of the .container div to achieve this but no luck so far. Un-comment the background-color: pink; on the .container div to see this.
I have a feeling the above problems are to do with the perspective property. I'm not sure how to calculate the correct amount of perspective here, and this could be my problem.
Here's my CodePen link.
HTML:
<div class="container">
<div class="cube">
<div class="front"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="back"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="left"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="right"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="top"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="bottom"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
</div>
</div>
CSS:
html {
background: #666;
}
body {
margin: 0;
padding: 0;
height: 100vh;
}
.container {
position: relative;
top: 50%;
left: 50%;
margin-left: -200px;
margin-top: -200px;
width: 400px;
height: 400px;
/* background-color: pink; */
transform-style: preserve-3d;
perspective: 1000px;
animation: rotate 2000ms linear infinite;
}
.cube {
/* background-color: blue; */
width: 200px;
height: 200px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -100px;
margin-top: -100px;
transform-style: preserve-3d;
}
.cube div {
position: absolute;
width: 200px;
height: 200px;
border: 5px solid #ccc;
box-sizing: border-box;
background-size: cover;
}
.cube img {
width: 100%;
opacity: 1;
}
.front {
transform: translateZ(100px);
}
.back {
transform: rotateY(180deg) translateZ(100px);
}
.left {
transform: rotateY(-90deg) translateZ(100px);
}
.right {
transform: rotateY(90deg) translateZ(100px);
}
.top {
transform: rotateX(90deg) translateZ(100px);
}
.bottom {
transform: rotateX(-90deg) translateZ(100px);
}
#keyframes rotate {
0% {
transform: rotateY(0deg);
}
100% {
transform: rotateY(360deg);
}
}
There are an issue because you rotate the container and its perspective as well.
you want a perspective on the cube, you have to set the perspective property on body element (or any kind of container of your animation that is not animated itself) and avoid to set it on the animated element. Actually, moving the element that is set by a perspective value will move this 3D element inside a 2D view – its own parent element. That causes the weird cube rendering on your exemple.
Also, if you want to control the perpective origin, you can use perspective-origin that lets you determine the position at which the viewer is looking. Associated with perspective property, you will be able to control the whole rendered scene.
So, the result will change with following code:
html { background: #666; }
body {
margin: 0;
padding: 0;
height: 100vh;
perspective: 900px;
perspective-origin: bottom;
}
.container {
position: relative;
top: 50%;
left: 50%;
margin-left: -200px;
margin-top: -200px;
width: 400px;
height: 400px;
transform-style: preserve-3d;
animation: rotate 2000ms linear infinite;
}
.cube {
width: 200px;
height: 200px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -100px;
margin-top: -100px;
transform-style: preserve-3d;
}
.cube div {
position: absolute;
width: 200px;
height: 200px;
border: 5px solid #ccc;
box-sizing: border-box;
background-size: cover;
}
.cube img {
width: 100%;
}
.front { transform: translateZ(100px); }
.back { transform: rotateY(180deg) translateZ(100px); }
.left { transform: rotateY(-90deg) translateZ(100px); }
.right { transform: rotateY(90deg) translateZ(100px); }
.top { transform: rotateX(90deg) translateZ(100px); }
.bottom { transform: rotateX(-90deg) translateZ(100px); }
#keyframes rotate {
to { transform: rotateY(360deg); }
}
<body>
<div class="container">
<div class="cube">
<div class="front"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="back"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="left"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="right"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="top"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="bottom"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
</div>
</div>
</body>

Positioning elements with position:fixed side-by-side each other

For a web application meant for mobile browsers, I want to integrate this simple yet elegant preloader. It's going to reside on top of a translucent overlay. It's also going to have some words on top of it (e.g. "Please wait...", or "Hold on a minute..." etc).
I tried integrating all that. The result is below (run the snippet). It has 3 distinct problems (can you help me solve them all):
1) I haven't been able to position the preloader + it's text on top of the translucent overlay (although I've used a high z-index).
2) Moreover, the preloader graphic is getting skewed in accordance with the length of the sentence on top of it. I want it to be independent of that.
3) Lastly, they're not positioned in the exact middle (vertically speaking).
body{background:#ECF0F1}
.parent{
position:fixed;
z-index:1;
top:50%;
left:50%;
transform:translate(-50%, -50%);
}
.caption{
margin-bottom:1em;
color:gray;
position:relative;
z-index:10;
}
.load{
width:50px;
height:50px;
position:relative;
z-index:10;
}
.load hr{border:0;margin:0;width:40%;height:40%;position:fixed;border-radius:50%;animation:spin 2s ease infinite}
.load :first-child{background:#19A68C;animation-delay:-1.5s}
.load :nth-child(2){background:#F63D3A;animation-delay:-1s}
.load :nth-child(3){background:#FDA543;animation-delay:-0.5s}
.load :last-child{background:#193B48}
#keyframes spin{
0%,100%{transform:translate(0)}
25%{transform:translate(160%)}
50%{transform:translate(160%, 160%)}
75%{transform:translate(0, 160%)}
}
.overlay{
position:fixed;
width:100%;
height:100%;
top:0;
left:0;
opacity:0.6;
z-index:100;
background-color:#FFFFFF;
}
body {
background: #E8F5E9;
background: repeating-linear-gradient(
to right,
#FAFAFA,
#FAFAFA 50px,
#E8F5E9 50px,
#E8F5E9 100px
);
}
<body>
<div class="overlay">
<div class="parent">
<div class="caption">Lorem ipsum dolor sit amet ...</div>
<div class="load">
<hr><hr><hr><hr>
</div>
</div>
</div>
<body>
Note: I'm hoping for pure CSS solutions, no JS involvement. I'd also want to avoid flex-box, and go for something universal (in a backward compatibility sense). E.g. flex-box support dwindles in older versions of Android browser (according to caniuse.com). I want to respect those versions too, hence it's best to stick to well-supported CSS 2.1 properties.
The issue is with the hr element they are placed fixed and they should be absolute to keep their relation with parent container. And then simply add margin:auto to load to center them.
And since parent and overlay elements are both fixed position you can separate them to be able to correctly use z-index and avoid the opacity being applied of overlay:
body {
background: #ECF0F1
}
.parent {
position: fixed;
z-index: 1000;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.caption {
margin-bottom: 1em;
color: gray;
position: relative;
z-index: 10;
}
.load {
width: 50px;
height: 50px;
position: relative;
z-index: 10;
margin: auto;
}
.load hr {
border: 0;
margin: 0;
width: 40%;
height: 40%;
position: absolute;
border-radius: 50%;
animation: spin 2s ease infinite
}
.load :first-child {
background: #19A68C;
animation-delay: -1.5s
}
.load :nth-child(2) {
background: #F63D3A;
animation-delay: -1s
}
.load :nth-child(3) {
background: #FDA543;
animation-delay: -0.5s
}
.load :last-child {
background: #193B48
}
#keyframes spin {
0%,
100% {
transform: translate(0)
}
25% {
transform: translate(160%)
}
50% {
transform: translate(160%, 160%)
}
75% {
transform: translate(0, 160%)
}
}
.overlay {
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
opacity: 0.6;
z-index: 100;
background-color: #FFFFFF;
}
body {
background: #E8F5E9;
background: repeating-linear-gradient( to right, #FAFAFA, #FAFAFA 50px, #E8F5E9 50px, #E8F5E9 100px);
}
<div class="overlay">
</div>
<div class="parent">
<div class="caption">Lorem ipsum dolor sit amet ...</div>
<div class="load">
<hr>
<hr>
<hr>
<hr>
</div>
</div>
The preloader is a child of the overlay, therefore it will have the parent opacity, no way to avoid that but setting it outside the overlay, maybe as a sibling, and adjust the overlay with a negative z-index
As for the sizing, the he are ignoring the parent size as they are set to position:fixed, should be absolute
body {
background: #ECF0F1
}
.parent {
position: fixed;
z-index: 1;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.caption {
margin-bottom: 1em;
color: gray;
position: relative;
z-index: 10;
}
.load {
width: 50px;
height: 50px;
position: relative;
z-index: 10;
margin: auto;
}
.load hr {
border: 0;
margin: 0;
width: 40%;
height: 40%;
position: absolute;
border-radius: 50%;
animation: spin 2s ease infinite
}
.load :first-child {
background: #19A68C;
animation-delay: -1.5s
}
.load :nth-child(2) {
background: #F63D3A;
animation-delay: -1s
}
.load :nth-child(3) {
background: #FDA543;
animation-delay: -0.5s
}
.load :last-child {
background: #193B48
}
#keyframes spin {
0%,
100% {
transform: translate(0)
}
25% {
transform: translate(160%)
}
50% {
transform: translate(160%, 160%)
}
75% {
transform: translate(0, 160%)
}
}
.overlay {
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
opacity: 0.6;
z-index: -10;
background-color: #FFFFFF;
}
body {
background: #E8F5E9;
background: repeating-linear-gradient( to right, #FAFAFA, #FAFAFA 50px, #E8F5E9 50px, #E8F5E9 100px);
}
<div class="overlay">
</div>
<div class="parent">
<div class="caption">Lorem ipsum dolor sit amet ...</div>
<div class="load">
<hr>
<hr>
<hr>
<hr>
</div>
</div>

How to create cube with only HTML and CSS?

I have this and I want to make a cube with HTML & CSS only like in the above image. My best try:
.mainDiv{
position: relative;
width: 206px;
height: 190px;
margin: 0px auto;
margin-top:100px;
}
.square{
width:100px;
height:100px;
background:#c52329;
border:solid 2px #FFF;
float:left;
transform: skew(180deg,210deg);
position: absolute;
top: 43px;
}
.square2{
width:100px;
height:100px;
background:#c52329;
border:solid 2px #FFF;
float:left;
transform: skew(180deg,150deg);
position: absolute;
left:102px;
top: 43px;
}
.square3{
width:100px;
height:100px;
background:#c52329;
border:solid 2px #FFF;
float:left;
transform: skew(180deg,180deg);
position: absolute;
left: 51px;
top: -61px;
}
<div class="mainDiv">
<div class="square"></div>
<div class="square2"></div>
<div class="square3"></div>
</div>
According to your HTML, I get this JSFiddle. I just played with transform.
.mainDiv{
position: relative;
width: 206px;
height: 190px;
margin: 0px auto;
margin-top:100px;
}
.square{
width:100px;
height:100px;
background:#c52329;
border:solid 2px #FFF;
transform: skew(180deg,210deg);
position: absolute;
top: 43px;
}
.square2{
width:100px;
height:100px;
background:#c52329;
border:solid 2px #FFF;
transform: skew(180deg,150deg);
position: absolute;
left:102px;
top: 43px;
}
.square3{
width:114px;
height:100px;
background:#c52329;
border:solid 2px #FFF;
transform: rotate(150deg) translate(-40px, -16px) skew(30deg, 0deg);
position: absolute;
left: 0px;
top: -32px;
}
<div class="mainDiv">
<div class="square"></div>
<div class="square2"></div>
<div class="square3"></div>
</div>
Updated CSS
.square3{
width:114px;
height:100px;
background:#c52329;
border:solid 2px #FFF;
transform: rotate(150deg) translate(-40px, -16px) skew(30deg, 0deg);
position: absolute;
left: 0px;
top: -32px;
}
I changed transform CSS with this.
Extra: David Walsh has a cool animated version on an cube. Apart from the fact that it looks kinda cool, by fiddling with the settings you can learn quite a lot about it.
You can also achieve a cube with 3d transforms. This will give your cube a more realistic perspective. As if the cube was a real 3d shape like this:
In the following I used one div with 2 pseudo elements :
body {
perspective: 900px;
padding-bottom:50%;
}
div {
position: relative;
width: 20%;
padding-bottom: 20%;
margin: 0 auto;
transform-style: preserve-3d;
background: #C52329;
transform: rotateX(60deg) rotatez(45deg);
}
div:before, div:after {
content: '';
position: absolute;
width: 100%;
height: 100%;
transform-origin: -2% -2%;
background: inherit;
}
div:before {
top: 104%; left: 0;
transform: rotateX(-90deg);
}
div:after {
top: 0; left: 104%;
transform: rotateY(90deg);
}
<div></div>
CSS 3d cube with 6 faces:
This technique allows you to make a "real cube" with 6 faces:
body{
perspective-origin:50% -100%;
perspective: 900px;
overflow:hidden;
}
h1{position:absolute;font-family:sans-serif;}
.cube {
position:relative;
padding-bottom:20%;
transform-style: preserve-3d;
transform-origin: 50% 100%;
transform:rotateY(45deg) rotateX(0);
transition:transform 3s;
}
.cubeFace {
position: absolute;
left:40%;top:0;
width: 20%;height:100%;
margin: 0 auto;
transform-style: inherit;
background: #C52329;
box-shadow:inset 0 0 0 5px #fff;
transform-origin:50% 50%;
transform: rotateX(90deg);
backface-visibility:hidden;
}
.face2{
transform-origin:50% 50%;
transform: rotatez(90deg) translateX(100%) rotateY(90deg);
}
.cubeFace:before, .cubeFace:after {
content: '';
position: absolute;
width: 100%;
height: 100%;
transform-origin:0 0;
background: inherit;
box-shadow:inherit;
backface-visibility:inherit;
}
.cubeFace:before {
top: 100%; left: 0;
transform: rotateX(-90deg);
}
.cubeFace:after {
top: 0; left: 100%;
transform: rotateY(90deg);
}
body:hover .cube{
transform:rotateY(405deg) rotateX(360deg);
}
<h1>Hover me:</h1>
<div class="cube">
<div class="cubeFace"></div>
<div class="cubeFace face2"></div>
</div>
Note that I didn't add the vendor prefixes in the examples. For more info about browser support and what vendor prefixes are needed according to your target audience, see canIuse for 3d transforms.
Basically, you want to do 2 transformations:
rotate the rectangle
squeeze it (skew it)
so basically, you need to do a transform: rotate(x) skew(y, y) and play a bit with size and placing.
here's a little demo I created, based on your own demo:
(I did remove the borders since they felt unneeded to me)
.mainDiv{
position: relative;
width: 206px;
height: 190px;
margin: 0px auto;
margin-top:100px;
}
.square{
width:100px;
height:100px;
background:#c52329;
float:left;
transform: skew(180deg,210deg);
position: absolute;
top: 43px;
}
.square2{
width:100px;
height:100px;
background:#c52329;
float:left;
transform: skew(180deg,150deg);
position: absolute;
left:102px;
top: 43px;
}
.square3{
width:110px;
height:110px;
background:#c52329;
float:left;
transform: rotate(45deg) skew(-15deg, -15deg);
position: absolute;
left: 46px;
top: -42px;
}
<div class="mainDiv">
<div class="square"></div>
<div class="square2"></div>
<div class="square3"></div>
</div>
First let me point out that a skew angle should be between -90deg and 90deg, non-inclusive. All of your skews fall way outside this range.
Limiting myself to sensible skew numbers, it turned out to be quite simple:
.mainDiv{
position: relative;
width: 206px;
height: 190px;
margin: 0px auto;
margin-top:100px;
}
.tile {
width:100px;
height:100px;
background:#c52329;
border:solid 2px #FFF;
position: absolute;
}
.square{
transform: skewY(30deg);
top: 43px;
}
.square2{
transform: skewY(-30deg);
left:102px;
top: 43px;
}
.square3{
height: 58px;
left: 50px;
top: -18px;
transform: skew(60deg, -30deg);
}
<div class="mainDiv">
<div class="tile square"></div>
<div class="tile square2"></div>
<div class="tile square3"></div>
</div>
Job done. I've also tidied up the huge repetition of styles into a common class for you.
A single box and 2 pseudos can do this as well.
http://codepen.io/gc-nomade/pen/vGeajp
#square {
display: flex;
align-items: center;
justify-content: center;
border-radius: 5px;
background: #C52329;
/*box-shadow: 0 0 5px;*/
width: 90px;
height: 150px;
margin: 5em;
position: relative;
transform: skew(30deg) rotate(30deg);
}
#square:before,
#square:after {
display: inherit;
align-items: center;
justify-content: center;
content: 'before';
position: absolute;
top: 0;
left: 2px;
right: -2px;
bottom: 0;
background: inherit;
border-radius: inherit;
box-shadow: inherit;
transform: translate(100%, -31%) skew(0, -45deg) rotate(0deg);
}
#square:after {
content: 'after';
top: -2px;
left: 0%;
height: 60%;
right: 0;
bottom: 2px;
transform: translate(50%, -100%) rotate(0deg)skew(-45deg)
}
<div id="square">
boxe
</div>
Use the following css for .square3:
.square3{
width:110px;
height:110px;
background:#c52329;
float:left;
transform: rotate(45deg) skew(-15deg, -15deg);
position: absolute;
left: 46px;
top: -42px;
}
Changing the CSS for .square3 should do it:
height: 58px;
left: 50px;
position: absolute;
top: -18px;
transform: skew(240deg, 150deg);
width: 100px;
https://jsfiddle.net/8vuj7peb/26/
I seen this and thought I would add something I came up with while trying to make some old fashioned abc blocks. Making them into 3d I only had to label the main container with another class to change positions and saved on the code. I commented the tutorial in the code. Hope this helps someone. :)
/*-------------------------------------------------------------
First we need to create our container for later reference
-I put this to show in the center of the screen if you wanted to
copy and paste the code into a document for play.
-The width is just to give the margin auto something to center on.
-You really on need the element itself to reference later, but this is prettier
-------------------------------------------------------------*/
.box{
width: 100px;
margin: 200px auto;
text-align: center;
line-height: 5;
}
/*---------------------------------------------------------------------------
The box-wrapper is our real hero container here. This is where we nail our box together.
-set this to relative position for child elements to reference to.
-transform-style is set to preserve-3d because I wanted to keep the look as the text turns with the box. You can also set this to flat, but its not near as cool.
---------------------------------------------------------------------------*/
.box-wrapper{
position: relative;
transform-style: preserve-3d;
-webkit-transform-style: preserve-3d;
}
/*-------------------------------------------------------------------------
Here I am just giving the box its needed dimesions and setting them to absolute so nothing gets any ideas of wandering off.
-PLEASE NOTE: the border has 2px and our w:98 h:98 making it a total of 100px. (this is important when we translate later)
-------------------------------------------------------------------------*/
.box-wrapper div{
width: 98px;
height: 98px;
position: absolute;
border: 2px solid black;
border-radius: 5px;
}
/*----------------------------------------------------------------------
Since our sides are 100px we only need to move our box sides 50px to get the edges to match up without gaps.
-Meaning "translate" moves to the position relative to your .box-wrapper. (You can play with this code to see it in action, try to take a visible section of the box and take it down 10).
-Also I use "rotate" y and x to turn our box sheets (.box-wrapper div's)
-----------------------------------------------------------------------*/
.front{
transform: translateZ(50px) rotateY(0deg);
}
.back{
transform: translateZ(-50px) rotateY(180deg);
}
.top{
transform: translateY(-50px) rotateX(90deg);
}
.bottom{
transform: translateY(50px) rotateX(-90deg);
}
.right{
transform: translateX(50px) rotateY(90deg);
}
.left{
transform: translateX(-50px) rotateY(270deg);
}
/*-----------------------------------------------------------------------
Then after all of this we can use our cool box-wrapper to turn this baby
Hope this is helpful! :) Enjoy!
-------------------------------------------------------------------------*/
.box .box-wrapper{
transform: rotateX(-30deg) rotateY(-40deg);
}
.box .box-wrapper div{
background-color: yellow;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bob the box builder</title>
<link rel="stylesheet" type="text/css" href="boxstyle.css">
<style>
</style>
</head>
<body>
<!--Create our box that holds our stuff -->
<div class="box">
<!--Create our real container that keeps our box sides nailed together-->
<div class="box-wrapper">
<!--Make our box sheets that we will nail together with css-->
<div class="front">Front</div>
<div class="back">Back</div>
<div class="left">Left</div>
<div class="right">Right</div>
<div class="top">Top</div>
<div class="bottom">Bottom</div>
</div>
</div>
</body>
</html>
y
|
|____ x
╱
z
Imagine a cube from the front side. What you can see? A square that comes out over the screen. So, for the front side, we have:
.front {
transform : translateZ(50px);
}
for the right side, we have a square that is rotated 90 degrees on Y-Axis and moved on own new Z-Axis:
.right {
transform : rotateY(90deg) translateZ(50px);
}
for the left side, we have a square that is rotated -90 degrees on Y-Axis and moved on own new Z-Axis:
.right {
transform : rotateY(-90deg) translateZ(50px);
}
for the top side, we have a square that is rotated 90 degrees on X-Axis and moved on own new Z-Axis:
.right {
transform : rotateX(90deg) translateZ(50px);
}
for the back side, we have a square that is rotated -180 degrees on Y-Axis and moved on own new Z-Axis:
.right {
transform : rotateY(-180deg) translateZ(50px);
}
Then, Just package them in a shape container class with transform-style: preserve-3d property:
.cube {
transform-style: preserve-3d;
}
finally, you can rotate your cube and see the CSS-3D magic.
.cube {
transform-style: preserve-3d;
transform: rotateX(-40deg) rotateY(45deg);
}
.cube {
transform-style: preserve-3d;
transform: rotateX(-40deg) rotateY(45deg);
}
.side {
position: absolute;
width: 100px;
height: 100px;
background: #c52329;
border: solid 3px white;
}
.front {
transform: translateZ(53px);
}
.top {
transform: rotateX(90deg) translateZ(53px);
}
.right {
transform: rotateY(90deg) translateZ(53px);
}
.left {
transform: rotateY(-90deg) translateZ(53px);
}
.bottom {
transform: rotateX(-90deg) translateZ(53px);
}
.back {
transform: rotateY(-180deg) translateZ(53px);
}
<div class="cube">
<div class="side front"></div>
<div class="side back"></div>
<div class="side right"></div>
<div class="side left"></div>
<div class="side top"></div>
<div class="side bottom"></div>
</div>
It is a full cube. For your approach, you can ignore the back, right, and bottom sides.
Thanks to css-tricks.com

How to create a responsive 4 div diamond?

I am working on a project where I have a div diamond of pictures that needs to be responsive.
The picture below shows the diamond in div I have created, but it doesn't work in all sizes. I want the diamond to react responsively to the browser size, so it always fits.
I have a jsFiddle, but it is not responsive. Just to show what I want, and I have been trying to create.
<div id="page">
<div id="main">
<div class="box blue"></div>
<div class="box green"></div>
<div class="box red"></div>
<div class="box yellow"></div>
</div>
</div>
#page {
width:100%;
height:100%;
min-height:500px;
min-width:500px;
}
#main {
height:80px;
width:80px;
position:relative;
display:block;
transform: rotate(45deg);
-ms-transform: rotate(45deg); /* IE 9 */
-webkit-transform: rotate(45deg); /* Safari and Chrome */
}
.box {
display: inline-block;
height:35%;
width:35%;
margin-right:5%;
margin-top:5%;
}
.blue {
background-color:blue;
}
.green {
background-color:green;
}
.red {
background-color:red;
}
.yellow {
background-color:#ffd54f;
}
Any help is very much appreciated :-)
Start with a responsive base:
#main {
width: 35%;
height: 0;
position: relative;
padding-bottom: 35%;
border: solid 1px black;
margin: auto;
}
The trick is to set the vertical dimension as padding percentage, that is calculated on the width of the parent. (So it is always a square)
Now set the diamonds, translated as percentages.
.box {
height:100%;
width:100%;
position: absolute;
}
.blue {
background-color:blue;
-webkit-transform: translate(-75%, 100%) rotate(45deg);
}
.green {
background-color:green;
-webkit-transform: translate(0, 25%) rotate(45deg);
}
.red {
background-color:red;
-webkit-transform: translate(75%, 100%) rotate(45deg);
}
.yellow {
background-color:#ffd54f;
-webkit-transform: translate(0, 175%) rotate(45deg);
}
fiddle
EDIT: OK it can be done with CSS alone. Updated fiddle here:
http://jsfiddle.net/5CfNb/5/
Not sure if this can be done with CSS alone, but here's my solution using a few lines of jQuery. Depending on the aspect ratio though, it won't be a perfect 'diamond', so still needs some tweaking. But I hope this helps.
$('#main').height($(window).height());
$('#main').width($(window).width());
$(window).resize(function() {
$('#main').height($(window).height());
$('#main').width($(window).width());
});
http://jsfiddle.net/5CfNb/4/
This could also help you a bit: http://jsfiddle.net/maximgladkov/bJLYn/1/
#main {
position: absolute;
top: 50%;
left: 50%;
margin: -15% 0 0 -15%;
height: 0px;
width: 30%;
padding-top: 30%;
overflow: visible;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
}
.box {
display: block;
width: 45%;
height: 45%;
position: absolute;
}
.blue {
background: blue;
top: 0;
left: 0;
}
.green {
background: green;
top: 0;
right: 0;
}
.red {
background: red;
bottom: 0;
right: 0;
}
.yellow {
background: #ffd54f;
bottom: 0;
left: 0;
}