In the scenario where x
represents the length of the outer square's side, the length of the inner square should be approximately sqrt(2)/2 * x
(≈ 0.707x). More insights on the mathematical reasoning behind this can be found here.
When working with sass, as there is no built-in sqrt
function, a suitable estimation approach can be implemented. Further information on how to do this mathematically can be accessed through this link: more math.
@function sqrt($square, $tolerance: .001, $estimate: $square/2) {
@if abs($square - $estimate*$estimate) < $tolerance {
@return $estimate;
}
@return sqrt($square, $tolerance, ($estimate + $square/$estimate)/2);
}
To incorporate this concept in your sass code:
$size: 200px;
$halfSqrt2: sqrt(2)/2;
.square {
height: $size;
width: $size;
background: pink;
display: flex;
justify-content: center;
align-items: center;
.square-inner {
transform: rotate(45deg);
background: red;
height: $halfSqrt2 * $size;
width: $halfSqrt2 * $size;
}
}
Important note:
width: 50%;
height: 50%;
The above styling won't generate a perfect square unless the container itself is a square.