Reputation: 928
I Have written a mixin to create for a background with 4 side-by-side lines at any size and any angle at any position by taking advantage of linear-gradient
however I am wondering if there would be a way to make it more dynamic?
@mixin corner-lines($start, $gap, $width, $angle, $colour) {
background:
linear-gradient($angle,
#0000 $start,
#0000 calc(#{$start} + #{$gap}),
$colour calc(#{$start} + #{$gap}),
$colour calc(#{$start} + (#{$gap} * 2) + #{$width}),
#0000 calc(#{$start} + (#{$gap} * 2) + #{$width}),
#0000 calc(#{$start} + (#{$gap} * 3) + #{$width})),
linear-gradient($angle,
#0000 calc(#{$start} + (#{$gap} * 4) + #{$width}),
#0000 calc(#{$start} + (#{$gap} * 5) + #{$width}),
$colour calc(#{$start} + (#{$gap} * 5) + #{$width}),
$colour calc(#{$start} + (#{$gap} * 6) + (#{$width} * 2)),
#0000 calc(#{$start} + (#{$gap} * 6) + (#{$width} * 2)),
#0000 calc(#{$start} + (#{$gap} * 7) + (#{$width} * 2))),
linear-gradient($angle,
#0000 calc(#{$start} + (#{$gap} * 8) + (#{$width} * 2)),
#0000 calc(#{$start} + (#{$gap} * 9) + (#{$width} * 2)),
$colour calc(#{$start} + (#{$gap} * 9) + (#{$width} * 2)),
$colour calc(#{$start} + (#{$gap} * 10) + (#{$width} * 3)),
#0000 calc(#{$start} + (#{$gap} * 10) + (#{$width} * 3)),
#0000 calc(#{$start} + (#{$gap} * 11) + (#{$width} * 3))),
linear-gradient($angle,
#0000 calc(#{$start} + (#{$gap} * 12) + (#{$width} * 3)),
#0000 calc(#{$start} + (#{$gap} * 13) + (#{$width} * 3)),
$colour calc(#{$start} + (#{$gap} * 13) + (#{$width} * 3)),
$colour calc(#{$start} + (#{$gap} * 14) + (#{$width} * 4)),
#0000 calc(#{$start} + (#{$gap} * 14) + (#{$width} * 4)),
#0000 calc(#{$start} + (#{$gap} * 15) + (#{$width} * 4)));
}
This creates something like this if given an angle of -45deg
with a gap of 5px
and a width of 10px
:
Currently however if I wanted to double the number of lines I'd have to copy-paste the linear-gradient
to make the background element larger
Is there a way I could loop n
times and generate this long linear gradient?
If I looped I'd use something like this, This example doesnt work but It'd be how I'd approach it:
$my-var: "someValue";
@while $i <= (#{$steps} * 4) {
$my-var: $my-var + linear-gradient($angle,
#0000 calc(#{$start} + (#{$gap} * #{$i}) + (#{$width} * $i)),
#0000 calc(#{$start} + (#{$gap} * (#{$i} + 1)) + (#{$width} * #{$i})),
$colour calc(#{$start} + (#{$gap} * (#{$i} + 1)) + (#{$width} * #{$i})),
$colour calc(#{$start} + (#{$gap} * (#{$i} + 2)) + (#{$width} * (#{$i} + 1))),
#0000 calc(#{$start} + (#{$gap} * (#{$i} + 2)) + (#{$width} * (#{$i} + 1))),
#0000 calc(#{$start} + (#{$gap} * (#{$i} + 3)) + (#{$width} * (#{$i} + 1))));
$i: $i + 4
}
I'm not even sure if this is possible since I don't think you can concat property values but I thought I'd ask anyway.
codesandbox: https://codesandbox.io/s/nostalgic-diffie-yxzgo?file=/src/styles.scss
Thanks!
Edit (12/08/20): Combining both Temani Afif's & Monzoor Tamal's answers got me to the desired outcome. Here is a mixin that works for me without hard coding any widths:
@mixin corner-lines($gap, $width, $n, $color) {
position: relative;
&::before {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: calc(#{$n} * (#{$width} + #{$gap}));
background:
repeating-linear-gradient(90deg,
transparent 0 #{$gap},
#{$color} 0 calc(#{$gap} + #{$width}));
content: '';
transform: skew(-45deg);
transform-origin: top;
}
}
Upvotes: 3
Views: 967
Reputation: 273448
I would do this using a different where it would easier to handle without SASS. You only need to adjust the CSS variables to control the gradient:
.box {
width:200px;
height:200px;
display:inline-block;
background:pink;
}
.gradient {
--n:3;
--gap:15px;
--width:20px;
--color:red;
position:relative;
z-index:0;
overflow:hidden;
}
.gradient::before {
content:"";
position:absolute;
top:0;
left:0;
bottom:0;
width:calc(var(--n)*(var(--width) + var(--gap)));
background:
repeating-linear-gradient(90deg,
transparent 0 var(--gap),
var(--color) 0 calc(var(--gap) + var(--width)));
transform:skew(-45deg);
transform-origin:top;
}
<div class="box gradient"></div>
<div class="box gradient" style="--n:5"></div>
<div class="box gradient" style="--n:2;--width:30px;--color:blue;"></div>
<div class="box gradient" style="--n:4;--gap:20px;--width:25px;--color:yellow;"></div>
Upvotes: 1
Reputation: 810
@mixin stripe($startColor, $endColor, $stripeAngel: -45, $stripeSize: 20px, $contentWidth: 100px, $contentAngel: 135,) {
background:
linear-gradient(
#{$contentAngel}deg,
rgba(255, 255, 255, 0.0) $contentWidth,
rgb(255, 255, 255) 0),
repeating-linear-gradient(
#{$stripeAngel}deg,
$startColor,
$startColor $stripeSize,
$endColor $stripeSize,
$endColor $stripeSize * 2);
}
.tt {
height: 100vh;
padding: 5em;
// @include corner-lines(90%, 5px, 10px, -45deg, red)
@include stripe(red, #ffffff, -45, 20px, 100px, 135)
}
all you need to change $contentWidth
size. let me know if it helps.
Upvotes: 1