Advanced mixins help you write reusable and flexible styles that can handle many situations without repeating code.
Why advanced mixins solve complex problems in SASS
@mixin mixin-name($param1, $param2: default) { // styles using parameters @if $param1 == value { // conditional styles } @content; // optional block content }
@mixin button($color) { background-color: $color; border: none; padding: 1rem; color: white; border-radius: 0.5rem; }
@mixin responsive-text($size) { font-size: $size; @media (max-width: 600px) { font-size: $size * 0.8; } }
@mixin complex-box($shadow: true) { padding: 1rem; background: white; @if $shadow { box-shadow: 0 0 10px rgba(0,0,0,0.2); } }
This example shows an advanced mixin called card that takes a background color and an optional shadow flag. It creates cards with different styles based on parameters. The primary card has a shadow and blue background, while the secondary card has no shadow and a light background. The cards also scale up slightly on hover for a nice effect.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Advanced Mixins Example</title> <style type="text/scss"> @mixin card($bg-color, $shadow: true) { background-color: $bg-color; padding: 2rem; border-radius: 1rem; @if $shadow { box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15); } transition: transform 0.3s ease; &:hover { transform: scale(1.05); } } .card-primary { @include card(#3498db); color: white; } .card-secondary { @include card(#ecf0f1, false); color: #333; } </style> </head> <body> <section> <article class="card-primary"> <h2>Primary Card</h2> <p>This card has a blue background and shadow.</p> </article> <article class="card-secondary"> <h2>Secondary Card</h2> <p>This card has a light background and no shadow.</p> </article> </section> </body> </html>
Advanced mixins let you add logic like conditions and loops inside your styles.
Using parameters makes your mixins flexible for many uses.
Mixins help keep your CSS DRY (Don't Repeat Yourself) and easier to update.
Advanced mixins let you write reusable, flexible styles with logic and parameters.
They help solve complex styling needs without repeating code.
Using them makes your CSS cleaner and easier to maintain.