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;
}
}