Last active
August 20, 2020 08:50
-
-
Save jbrz0/108439ce8d21c6d41ab2f33b03d55d48 to your computer and use it in GitHub Desktop.
Sass mixin for positioning an element absolute middle / centered vertical / centered horizontal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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