Skip to content

Instantly share code, notes, and snippets.

@robinverona
Last active April 21, 2023 08:10
Show Gist options
  • Save robinverona/8ddb9eb904e3a94a17b5e9f66a768c22 to your computer and use it in GitHub Desktop.
Save robinverona/8ddb9eb904e3a94a17b5e9f66a768c22 to your computer and use it in GitHub Desktop.
Sass function change text color based on background color
/* This snippet NEED HSL values to work correctly */
/* Sass variables with HSL values */
$orange: hsl(30, 91, 58, 1);
$blue: hsl(229, 100, 65, 1);
$purple: hsl(251, 54, 49, 1);
$black: hsl(0, 0, 0, 1);
$white: hsl(0, 0, 100, 1);
/* Utilities variables to pass to function */
$bg-color: $purple;
$text-color: dynamic-color(bg-color);
// dynamic color function declaration
@function dynamic-color($color) {
$font-color: $black;
@if (lightness($color) > 50%) {
$font-color: $black;
} @else {
$font-color: $white;
}
@return $font-color;
};
// Integration example
.button {
width: 200px;
padding: 18px 36px 16px;
color: black;
cursor: pointer;
background-color: $bg-color;
border: 3px solid black;
border-radius: 50px;
box-shadow: 6px 6px 0 black;
display: flex;
justify-content: center;
font-weight: bold;
font-family: 'arial';
color: dynamic-color($bg-color);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment