Skip to content

Instantly share code, notes, and snippets.

@helloromero
Forked from kieranmv95/_center.mixin.scss
Created November 13, 2017 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helloromero/743a60c856ec469e98f313fe39f284de to your computer and use it in GitHub Desktop.
Save helloromero/743a60c856ec469e98f313fe39f284de to your computer and use it in GitHub Desktop.
Center anything horizontally vertically or on both axis in scss
// Center anything horizontally, vertically or both (assuming that the parents position is set to relative)
// Codepen Example Usage: https://codepen.io/anon/pen/yPbgYW
@mixin center($center: 'all') {
position: absolute;
@if $center == 'all' {
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
} @else if $center == 'horizontal' {
top: 0;
left: 50%;
transform: translateX(-50%);
} @else if $center == 'vertical' {
left: 0;
top: 50%;
transform: translateY(-50%);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment