Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS 3 Divs rotated 45 degrees

I'm trying to create this effect:

enter image description here

I need the three colors to be 3 different divs:

<div>
  <div id="red"></div>
  <div id="blue"></div>
  <div id="green"></div>
</div>

The 3 divs need to fully fill its parent. Its parent could be another div or even the full window size. Here is the rotation I tried but it doesn't full fill the space.

#green {
  height: 100%;
  background-color: green;
  width: 37.5%;
  transform: rotate(45deg)
}

#red {
  height: 100%;
  background-color: red;
  width: 25%;
  transform: rotate(45deg)
}

#blue {
  height: 100vh;
  background-color: blue;
  width: 37.5%;
  transform: rotate(45deg)
}
<div>
  <div id="red"></div>
  <div id="blue"></div>
  <div id="green"></div>
</div>
like image 930
Tom Avatar asked Apr 09 '19 19:04

Tom


People also ask

How do I rotate a div 90 degrees?

An element can be rotated 90 degrees by using the transform property. This property is used to move, rotate, scale and others to perform various kinds of transformation to elements. The rotate() transformation function can be used as the value to rotate the element.

Which CSS property supports methods that will rotate horizontally vertically or 3 dimensionally?

The rotate3d() CSS function defines a transformation that rotates an element around a fixed axis in 3D space, without deforming it.

How do you change rotation in CSS?

Rotating an image using CSS Once the CSS code is applied to your . css file, stylesheet, or <style> tags, you can use the CSS class name in any of your image tags. To rotate an image by another measure of degrees, change the "180" in the CSS code and <img> tag to the degree you desire.


2 Answers

You can use a simple gradient to achieve this:

.box {
  width:150px;
  height:150px;
  background:
    linear-gradient(to bottom right,red 35%,blue 35%,blue 65%,yellow 65%);
}
<div class="box">

</div>

With 3 divs you can try like below with transform:

.box {
  width:150px;
  height:150px;
  position:relative;
  overflow:hidden;
}

.box :first-child {
  width:100%;
  height:100%;
  background:blue;
}
.box :nth-child(2),
.box :last-child {
  position:absolute;
  width:141%; /*sqrt(2)x100% */
  height:141%;
}

.box :nth-child(2) {
  top:0;
  left:0;
  background:red;
  transform:rotate(45deg) translate(-90%);
}
.box :last-child {
  bottom:0;
  right:0;
  background:yellow;
  transform:rotate(45deg) translate(90%)
}
<div class="box">
  <div></div>
  <div></div>
  <div></div>
</div>

You can easily add animation using the first method by applying multiple background:

.box {
  width:150px;
  height:150px;
  background:
    linear-gradient(to bottom right,red   50%,transparent 0),
    linear-gradient(to top left    ,yellow 50%,transparent 0),
    blue;
  background-size:200% 200%;
  background-position:100% 100%,0 0;
  transition:1s all;
}
.box:hover {
  background-position:50% 50%;
}
<div class="box">

</div>

also using the second method by adjusting the translate value:

.box {
  width:150px;
  height:150px;
  position:relative;
  overflow:hidden;
}

.box :first-child {
  width:100%;
  height:100%;
  background:blue;
}
.box :nth-child(2),
.box :last-child {
  position:absolute;
  width:141%; /*sqrt(2)x100% */
  height:141%;
  transition:1s all;
}

.box :nth-child(2) {
  top:0;
  left:0;
  background:red;
  transform:rotate(45deg) translate(calc(-1 * var(--p,120%)));
}
.box :last-child {
  bottom:0;
  right:0;
  background:yellow;
  transform:rotate(45deg) translate(var(--p,120%))
}

.box:hover{
  --p:70%;
}
<div class="box">
  <div></div>
  <div></div>
  <div></div>
</div>

It can also work responsively:

.box {
  height:100vh;
  background:
    linear-gradient(to bottom right,red    50%,transparent 50.1%),
    linear-gradient(to top left    ,yellow 50%,transparent 50.1%),
    blue;
  background-size:200% 200%;
  background-position:100% 100%,0 0;
  transition:1s all;
}
.box:hover {
  background-position:50% 50%;
}

body {
 margin:0;
}
<div class="box">

</div>

For more details about the values used with background: Using percentage values with background-position on a linear gradient

like image 51
Temani Afif Avatar answered Nov 09 '22 22:11

Temani Afif


body {
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 100vh;
}
#wrapper {
  width: 20rem;
  height: 20rem;
  position: relative;
  overflow: hidden;
}

#red, #blue, #green {
  position: absolute;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
}

#red {
  background: red;
  transform: rotate(45deg) translatex(-75%);
}

#blue {
  background: blue;
}

#green {
  background: green;
  transform: rotate(45deg) translatex(75%);
}
<div id="wrapper">
  <div id="blue"></div>
  <div id="red"></div>
  <div id="green"></div>
</div>

Do you mean like this?

I used css rotate and transform to achieve this.

like image 33
Snapstromegon Avatar answered Nov 09 '22 22:11

Snapstromegon