【发布时间】:2020-08-08 01:28:27
【问题描述】:
问题
我需要在 CSS 中编写如下图所示的设计。除了纯 CSS,我还可以使用 Sass。
请注意,解决方案需要具有响应性。角度是固定的(14度),而距离x不是,因为它取决于容器的高度(不同的屏幕宽度会有所不同)。
固定高度
定高版本没问题:
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;
}
响应能力
问题是我的实现需要响应。当屏幕高度发生变化时,容器的高度也会发生变化,trapezium-shift 的值也会随之变化(在图片上标记为 x)。 Sass 在构建时运行,因此 Sass 无法知道容器的高度。
问题
您知道任何可能的解决方案吗?
【问题讨论】:
-
需要固定角度还是距离?
-
@TemaniAfif 角度需要固定,距离x是动态的。我会把它添加到问题中。
标签: css layout sass css-transforms