Skip to content

Instantly share code, notes, and snippets.

@deardorffdev
Created May 28, 2017 11:11
Show Gist options
  • Save deardorffdev/bed0bd30a42b9ec43052947625dbc28f to your computer and use it in GitHub Desktop.
Save deardorffdev/bed0bd30a42b9ec43052947625dbc28f to your computer and use it in GitHub Desktop.
Useful SASS Mixins 1
/* * */
/*----------------[IMPORT VARIABLES]---------------*/
/* * */
/*
Push auto
To quickly centre a block element without having to worry about if there is any top or bottom margin already applied.
*/
@mixin push--auto {
margin: {
left: auto;
right: auto;
}
}
/*
Pseudo
When using ::before and ::after you'll always need these three, so we're saving two lines of code every time you use this.
*/
@mixin pseudo($display: block, $pos: absolute, $content: ''){
content: $content;
display: $display;
position: $pos;
}
/*
Pseudo
No Argument
*/
div::after {
@include pseudo;
top: -1rem; left: -1rem;
width: 1rem; height: 1rem;
}
/*
Responsive ratio
I use this for creating scalable elements (usually images / background images) that maintain a ratio.
*/
@mixin responsive-ratio($x,$y, $pseudo: false) {
$padding: unquote( ( $y / $x ) * 100 + '%' );
@if $pseudo {
&:before {
@include pseudo($pos: relative);
width: 100%;
padding-top: $padding;
}
} @else {
padding-top: $padding;
}
}
/*
CSS triangles
This mixin takes all the hassle out of creating that triangle you'll see coming out of most traditional tooltips, all without images, you just specify it's colour, how big you want it, the direction it's going to come out of your element and you're done!
*/
@mixin css-triangle($color, $direction, $size: 6px, $position: absolute, $round: false){
@include pseudo($pos: $position);
width: 0;
height: 0;
@if $round {
border-radius: 3px;
}
@if $direction == down {
border-left: $size solid transparent;
border-right: $size solid transparent;
border-top: $size solid $color;
margin-top: 0 - round( $size / 2.5 );
} @else if $direction == up {
border-left: $size solid transparent;
border-right: $size solid transparent;
border-bottom: $size solid $color;
margin-bottom: 0 - round( $size / 2.5 );
} @else if $direction == right {
border-top: $size solid transparent;
border-bottom: $size solid transparent;
border-left: $size solid $color;
margin-right: -$size;
} @else if $direction == left {
border-top: $size solid transparent;
border-bottom: $size solid transparent;
border-right: $size solid $color;
margin-left: -$size;
}
}
/*
Font styles
Take the pain out of setting styles for a font. (This assumes you have already included the font), set your fallback font once and you never have to worry again.
*/
@mixin font-source-sans($size: false, $colour: false, $weight: false, $lh: false) {
font-family: 'Work Sans Pro', Helvetica, Arial, sans-serif;
@if $size { font-size: $size; }
@if $colour { color: $colour; }
@if $weight { font-weight: $weight; }
@if $lh { line-height: $lh; }
}
/*
Placeholders
They're a pain as you have to set the style in all the separate formats, this sorts it for you.
*/
@mixin input-placeholder {
&.placeholder { @content; }
&:-moz-placeholder { @content; }
&::-moz-placeholder { @content; }
&:-ms-input-placeholder { @content; }
&::-webkit-input-placeholder { @content; }
}
/*
Placeholders Content Usage Example [Do Not Uncomment]
input,
textarea {
@include input-placeholder {
color: $grey;
}
}
*/
/*
Media queries
A massive time saver! You can just use pixel values but here are some default breakpoints setup that work too, another time saver while keeping your breakpoints consistent.
*/
$breakpoints: (
"phone": 400px,
"phone-wide": 480px,
"phablet": 560px,
"tablet-small": 640px,
"tablet": 768px,
"tablet-wide": 1024px,
"desktop": 1248px,
"desktop-wide": 1440px
);
@mixin mq($width, $type: min) {
@if map_has_key($breakpoints, $width) {
$width: map_get($breakpoints, $width);
@if $type == max {
$width: $width - 1px;
}
@media only screen and (#{$type}-width: $width) {
@content;
}
}
}
/*
Usage example below showing the padding and font-size increasing once the viewport gets over 1024px.
*/
.site-header {
padding: 2rem;
font-size: 1.8rem;
@include mq('tablet-wide') {
padding-top: 4rem;
font-size: 2.4rem;
}
}
/*
Z-index
Technically this is just a pure function. It's so easy to loose track of your z-index values when working in several different files, so I created this so that I could store them in one place for easy editing/recording.
Just place this snippet in your main variables/settings.scss file (needs to be imported before any of your other files that reference a x-index of course).
*/
@function z($name) {
@if index($z-indexes, $name) {
@return (length($z-indexes) - index($z-indexes, $name)) + 1;
} @else {
@warn 'There is no item "#{$name}" in this list; choose one of: #{$z-indexes}';
@return null;
}
}
$z-indexes: (
"outdated-browser",
"modal",
"site-header",
"page-wrapper",
"site-footer"
);
/*
Hardware
Simple and effective for when you need to trigger hardware acceleration for some animation, keeping everything fast, slick and flicker-free.
*/
@mixin hardware($backface: true, $perspective: 1000) {
@if $backface {
backface-visibility: hidden;
}
perspective: $perspective;
}
/*
Truncate
Adding Truncation to an element isn't as simple as it should be, although it is with this handy time saver, meaning when you include the mixin, you only have to specify your boundry with a max width value, happy days!
*/
@mixin truncate($truncation-boundary) {
max-width: $truncation-boundary;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment