Skip to content

Instantly share code, notes, and snippets.

@jbrz0
Last active August 20, 2020 08:50
Show Gist options
  • Save jbrz0/108439ce8d21c6d41ab2f33b03d55d48 to your computer and use it in GitHub Desktop.
Save jbrz0/108439ce8d21c6d41ab2f33b03d55d48 to your computer and use it in GitHub Desktop.
Sass mixin for positioning an element absolute middle / centered vertical / centered horizontal
// Define vertical, horizontal, or both position
@mixin center($position) {
position: absolute;
@if $position == 'vertical' {
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
@else if $position == 'horizontal' {
left: 50%;
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translate(-50%);
}
@else if $position == 'both' {
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
}
// Using the mixin
.foo {
@include center(both);
}
.foo-parent {
position: relative;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment