Problem
I need to code a design like in image below in CSS. In addition to pure CSS I also have Sass at my disposal.
Note that the solution needs to be responsive. The angle is fixed (14 degrees), while the distance x is not, because it depends on the height of the container (which will be different on different screen widths).
Fixed height
The fixed height version is not a problem:
HTML
<section class="container">
<p class="left">
Some text spanning multiple lines
</p>
<p class="right">
Some text spanning multiple lines
</p>
</section>
CSS
@use "sass:math";
$trapezium-skew-angle: -14deg;
@mixin orange-background {
position: relative;
// Needs to use pseudo-element to be able to render it
// below the trapezium's layer
&::before {
z-index: -1;
content: "";
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-color: orange;
}
}
@function trapezium-shift($trapezium-height) {
@return (-1 * math.tan($trapezium-skew-angle) * $trapezium-height) / 2;
}
@mixin trapezium($height, $width) {
overflow: hidden;
position: relative;
&::after {
z-index: -1;
content: "";
display: block;
position: absolute;
left: -1 * trapezium-shift($height);
top: 0;
bottom: 0;
width: $width;
background-color: blue;
transform: skew($trapezium-skew-angle);
}
}
@mixin column {
outline: 1px dashed black;
padding: 4rem;
width: 50%;
}
.container {
@include orange-background;
display: flex;
flex-flow: row nowrap;
height: 300px;
}
.left {
@include column;
@include trapezium($height: 300px, $width: 50%);
}
.right {
@include column;
}
Responsiveness
The problem is that my implementation needs to be responsive. When the screen height changes, the height of the container will change too and so will change the value of trapezium-shift
(which is marked as x on the image). Sass runs at build-time, so there is no way for Sass to know the height of the container.
Question
Do you know about any possible solution to this?