A Pen by Jeremy Frank on CodePen.
Created
November 26, 2019 11:34
-
-
Save wmucheru/9ab6f2bccfd1b5259405b423630c44fe to your computer and use it in GitHub Desktop.
Angled Edge Pseudo Element SASS Mixin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="block -berry edge--bottom"> | |
<h1>Bottom Angled Edge</h1> | |
</div> | |
<div class="block -blue edge--bottom--reverse"> | |
<h1>Bottom Angled Edge</h1> | |
<p>Reversed</p> | |
</div> | |
<div class="block -berry edge--top"> | |
<h1>Top Angled Edge</h1> | |
</div> | |
<div class="block -blue edge--top--reverse"> | |
<h1>Top Angled Edge</h1> | |
<p>Reversed</p> | |
</div> | |
<div class="block -orange edge--both"> | |
<h1>Top & Bottom Angled Edges</h1> | |
</div> | |
<div class="block -green edge--both--reverse"> | |
<h1>Top & Bottom Angled Edges</h1> | |
<p>Reversed</p> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// mixin | |
@mixin angle($pseudo, $flip: false, $angle: 1.5deg) { | |
// Possible values for $pseudo are: before, after, both | |
@if $pseudo == 'before' or $pseudo == 'after' or $pseudo == 'both' { | |
position: relative; | |
z-index: 1; | |
$selector: if($pseudo == 'both', '&:before,&:after', '&:#{$pseudo}'); | |
#{$selector} { | |
background: inherit; | |
content: ''; | |
display: block; | |
height: 50%; | |
left: 0; | |
position: absolute; | |
right: 0; | |
z-index: -1; | |
-webkit-backface-visibility: hidden; // for Chrome Windows | |
} | |
@if $pseudo == 'before' { | |
#{$selector} { | |
top: 0; | |
@if $flip { | |
transform: skewY($angle * -1); | |
transform-origin: 0 0; | |
} @else { | |
transform: skewY($angle); | |
transform-origin: 100% 0; | |
} | |
} | |
} | |
@if $pseudo == 'after' { | |
#{$selector} { | |
bottom: 0; | |
@if $flip { | |
transform: skewY($angle); | |
transform-origin: 0 100%; | |
} @else { | |
transform: skewY($angle * -1); | |
transform-origin: 100%; | |
} | |
} | |
} | |
@if $pseudo == 'both' { | |
&:before { | |
top: 0; | |
@if $flip { | |
transform: skewY($angle * -1); | |
transform-origin: 0 0; | |
} @else { | |
transform: skewY($angle); | |
transform-origin: 100% 0; | |
} | |
} | |
&:after { | |
bottom: 0; | |
@if $flip { | |
transform: skewY($angle); | |
transform-origin: 0 0; | |
} @else { | |
transform: skewY($angle * -1); | |
transform-origin: 100%; | |
} | |
} | |
} | |
} | |
} | |
// slanted edges here | |
.edge--bottom { | |
@include angle(after); | |
} | |
.edge--bottom--reverse { | |
@include angle(after, true); | |
} | |
.edge--top { | |
@include angle(before); | |
} | |
.edge--top--reverse { | |
@include angle(before, true); | |
} | |
.edge--both { | |
@include angle(both); | |
} | |
.edge--both--reverse { | |
@include angle(both, true); | |
} | |
.-berry { | |
background: #b52b4a; | |
} | |
.-blue { | |
background: #41ade5 | |
} | |
.-orange { | |
background: #de6628; | |
} | |
.-green { | |
background: #5e9b42; | |
} | |
// demo code below | |
.block { | |
color: #fff; | |
font-family: 'Fira Sans', sans-serif; | |
margin: 0 0 200px; | |
padding: 20% 20px; | |
text-align: center; | |
} | |
h1 { | |
font-size: 32px; | |
font-weight: 500; | |
} | |
p { | |
font-size: 14px; | |
font-weight: 300; | |
margin-top: 0.5em; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link href="https://fonts.googleapis.com/css?family=Fira+Sans:300,500" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment