-
-
Save ladislavsulc/37f9425e2e0cd8782085 to your computer and use it in GitHub Desktop.
Use Sass to dynamically change text color based on background color
This file contains hidden or 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
@function set-notification-text-color($color) { | |
@if (lightness( $color ) > 50) { | |
@return #000; // lighter color, return black | |
} | |
@else { | |
@return #fff; // darker color, return white | |
} | |
} | |
$confirm: hsla(101, 72%, 37%, 1); // green | |
$warning: #ffc53a; // yellow | |
$alert: rgb(172, 34, 34); // red | |
%notification { | |
border-radius: 10px; | |
display: block; | |
font-family: sans-serif; | |
font-size: 1.5em; | |
margin: 1em auto; | |
padding: 1em 2em; | |
text-align: center; | |
width: 30%; | |
} | |
.notification { | |
@extend %notification; | |
} | |
.confirm { | |
background: $confirm; | |
color: set-notification-text-color($confirm); | |
} | |
.warning { | |
background: $warning; | |
color: set-notification-text-color($warning); | |
} | |
.alert { | |
background: $alert; | |
color: set-notification-text-color($alert); | |
} |
This file contains hidden or 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
<p class="notification confirm">Confirmation</p> | |
<p class="notification warning">Warning</p> | |
<p class="notification alert">Alert</p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment