Skip to content

Instantly share code, notes, and snippets.

@tankbar
Created February 20, 2018 22:22

Revisions

  1. tankbar created this gist Feb 20, 2018.
    14 changes: 14 additions & 0 deletions _truly-centered.scss
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    @mixin centerer($horizontal: true, $vertical: true) {
    position: absolute;
    @if ($horizontal and $vertical) {
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    } @else if ($horizontal) {
    left: 50%;
    transform: translate(-50%, 0);
    } @else if ($vertical) {
    top: 50%;
    transform: translate(0, -50%);
    }
    }
    11 changes: 11 additions & 0 deletions example.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <div class=“parent”>
    <div class=“child both”>I’m centered on both axis!</div>
    </div>

    <div class=“parent”>
    <div class=“child horizontal”>I’m centered horizontally!</div>
    </div>

    <div class=“parent”>
    <div class=“child vertical”>I’m centered vertically!</div>
    </div>
    17 changes: 17 additions & 0 deletions example.scss
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    .parent {
    position: relative;
    }

    .child {
    &.both {
    @include centerer;
    }

    &.horizontal {
    @include centerer(true, false);
    }

    &.vertical {
    @include centerer(false, true);
    }
    }