DEV Community

Tomas Stveracek
Tomas Stveracek

Posted on

Make Your CSS Better with SCSS Mixins and Functions

SCSS is an extension of CSS that makes your code easier to manage. With SCSS, you can use mixins and functions to help you avoid writing the same code again and again. In this article, I’ll show you some useful SCSS mixins and functions that can save you time and make your code cleaner.

Why Use Mixins and Functions? When writing CSS, you often repeat the same styles. SCSS mixins and functions help by:

  • Avoiding Repetition: Write common styles once and use them everywhere.
  • Adding Flexibility: Change styles easily with parameters.
  • Writing Dynamic Styles: Use calculations to create more flexible designs.

1. Responsive Breakpoints Mixin

When making websites responsive, you need to write a lot of media queries. This mixin makes it easy to handle breakpoints.

@mixin respond-to($breakpoint) {
  @if $breakpoint == mobile {
    @media (max-width: 600px) { @content; }
  } @else if $breakpoint == tablet {
    @media (max-width: 900px) { @content; }
  } @else if $breakpoint == desktop {
    @media (min-width: 1200px) { @content; }
  }
}

// Usage
.container {
  padding: 20px;

  @include respond-to(mobile) {
    padding: 10px;
  }

  @include respond-to(desktop) {
    padding: 40px;
  }
}
Enter fullscreen mode Exit fullscreen mode

This mixin lets you handle breakpoints by just using simple names like "mobile" or "desktop."

2. Button Styles Mixin

Creating buttons can be repetitive. This mixin lets you create buttons with different colors while keeping other styles the same.

@mixin button($bg-color, $text-color: #fff) {
  background-color: $bg-color;
  color: $text-color;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s ease;

  &:hover {
    background-color: darken($bg-color, 10%);
  }
}

// Usage
.button-primary {
  @include button(#007BFF);
}

.button-secondary {
  @include button(#6C757D);
}
Enter fullscreen mode Exit fullscreen mode

Now you can create buttons quickly with just one line of code, adjusting the colors as needed.

3. Typography Mixin

Typography is important for any website. This mixin lets you set up responsive typography with just a few lines of code.

@mixin typography($font-size, $line-height: 1.5, $weight: normal) {
  font-size: $font-size;
  line-height: $line-height;
  font-weight: $weight;

  @include respond-to(mobile) {
    font-size: $font-size * 0.9;
  }

  @include respond-to(desktop) {
    font-size: $font-size * 1.1;
  }
}

// Usage
h1 {
  @include typography(32px, 1.2, bold);
}

p {
  @include typography(16px);
}

Enter fullscreen mode Exit fullscreen mode

This mixin helps you make sure your font sizes look good on both small and large screens.

4. Function for Rem Units

This function converts px values to rem, making your code easier to scale for different devices.

@function px-to-rem($px, $base-font-size: 16px) {
  @return $px / $base-font-size * 1rem;
}

// Usage
.container {
  padding: px-to-rem(32);
}
Enter fullscreen mode Exit fullscreen mode

Instead of manually converting pixels to rem units, you can use this function to do it automatically.

5. Color Adjustment Function

Need to change a color quickly? This function darkens or lightens a color based on your input.

@function adjust-color-brightness($color, $amount) {
  @if $amount > 0 {
    @return lighten($color, $amount);
  } @else {
    @return darken($color, abs($amount));
  }
}

// Usage
.header {
  background-color: adjust-color-brightness(#007BFF, -10%); // Darker blue
}
Enter fullscreen mode Exit fullscreen mode

With this function, you can easily create lighter or darker shades of a color, perfect for hover effects or themes.

6. Grid Layout Mixin

Creating grid layouts is easy with this mixin. It lets you set the number of columns and the space between them.

@mixin grid-layout($columns: 3, $gap: 20px) {
  display: grid;
  grid-template-columns: repeat($columns, 1fr);
  grid-gap: $gap;
}

// Usage
.grid {
  @include grid-layout(4, 30px);
}
Enter fullscreen mode Exit fullscreen mode

This mixin simplifies the process of creating grid layouts by allowing you to customize the number of columns and gaps.

Conclusion:

Mixins and functions in SCSS help you write cleaner and more efficient CSS. With just a few lines of code, you can make your styles more flexible and easier to maintain. Whether you’re creating responsive designs, buttons, or dynamic layouts, SCSS mixins and functions can make your life as a developer easier. Give them a try in your next project!

Top comments (0)