Skip to content

Instantly share code, notes, and snippets.

@Camwyn
Created November 2, 2016 14:57
Show Gist options
  • Select an option

  • Save Camwyn/a98d7b0a130304c5cc7bc387781be1ca to your computer and use it in GitHub Desktop.

Select an option

Save Camwyn/a98d7b0a130304c5cc7bc387781be1ca to your computer and use it in GitHub Desktop.
Centering via position and transform
// Position an element horizontally (requires a 'position: relative' parent)
@mixin horizontal-center {
position: absolute;
left: 50%;
transform: translateX(-50%);
}
// Position an element vertically (requires a 'position: relative' parent)
@mixin vertical-center {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
// Position an element both vertically AND horizontally (requires a 'position: relative' parent)
@mixin center {
left: 50%;
position: absolute;
top: 50%;
transform: translateX(-50%) translateY(-50%);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment