Skip to content

Instantly share code, notes, and snippets.

@irunatbullets
Created March 1, 2015 05:05
Show Gist options
  • Select an option

  • Save irunatbullets/95766d0013a0d50aa176 to your computer and use it in GitHub Desktop.

Select an option

Save irunatbullets/95766d0013a0d50aa176 to your computer and use it in GitHub Desktop.
A mixin for adding borders that don't effect the box model, meaning that if a designer asks for 10px padding around a box and there is also a 1px border, you don't have to make your padding 9px to compensate. It helps you to avoid magic numbers.
@mixin magic-border($widths, $color, $extra-box-shadow: false) {
$shadow: null;
@if length($widths) == 1 {
$shadow: 'inset 0 0 0 #{$widths} #{$color}';
}
@if length($widths) == 2 {
$shadow: 'inset 0 #{nth($widths,1)} 0 0 #{$color}, inset -#{nth($widths,2)} 0 0 0 #{$color}, inset 0 -#{nth($widths,1)} 0 0 #{$color}, inset #{nth($widths,2)} 0 0 0 #{$color}';
}
@if length($widths) == 3 {
$shadow: 'inset 0 #{nth($widths,1)} 0 0 #{$color}, inset -#{nth($widths,2)} 0 0 0 #{$color}, inset 0 -#{nth($widths,3)} 0 0 #{$color}, inset #{nth($widths,2)} 0 0 0 #{$color}';
}
@if length($widths) == 4 {
$shadow: 'inset 0 #{nth($widths,1)} 0 0 #{$color}, inset -#{nth($widths,2)} 0 0 0 #{$color}, inset 0 -#{nth($widths,3)} 0 0 #{$color}, inset #{nth($widths,4)} 0 0 0 #{$color}';
}
@if $extra-box-shadow != false {
@if length($extra-box-shadow) != 1 {
@error "Extra box shadows must be wrapped in quotes to allow for more than one.";
}
$shadow: append(unquote($shadow), unquote($extra-box-shadow), comma);
} @else {
$shadow: unquote($shadow);
}
box-shadow: $shadow;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment