Skip to content

Instantly share code, notes, and snippets.

@ralphcrisostomo
Created August 17, 2012 10:46
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 ralphcrisostomo/3377895 to your computer and use it in GitHub Desktop.
Save ralphcrisostomo/3377895 to your computer and use it in GitHub Desktop.
Center an element absolutely and relatively
/**
author: Ralph Crisostomo
*/
@mixin center-element($width, $height) {
width:$width;
height:$height;
position: absolute;
top: 50%;
left: 50%;
margin-top: -($height / 2);
margin-left: -($width / 2);
}
@include center-element(<element width>,<element hieght>);
/** Sample */
.container{
width: 100px;
height:
// position: relative; // Uncomment to center .element inside .container
// Will be centered absolutely
.element{
width: 50px;
height: 50px;
@include center-element(50px,50px);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment