cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="outer-container vertical-center">
  <p><strong>@include center(vertical);</strong><br>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros.</p>
</div><!-- /ui-block -->

<div class="no-flexbox">
  <div class="outer-container vertical-center">
    <div class="inner-container">
      <p><strong>@include center(vertical);</strong><br>NO FLEXBOX. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</p><!--Content with an unknown height -->
    </div><!-- /ui-block-inner -->
  </div><!-- /ui-block -->
</div>

<div class="outer-container center">
  <div class="inner-container">
    <p><strong>@include center(both);</strong><br>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</p><!--Content with an unknown height -->
  </div><!-- /ui-block-inner -->
</div><!-- /ui-block -->

<div class="no-flexbox">
  <div class="outer-container center">
    <div class="inner-container">
      <p><strong>@include center(both);</strong><br>NO FLEXBOX. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</p><!--Content with an unknown height -->
    </div><!-- /ui-block-inner -->
  </div><!-- /ui-block -->
</div>

<div class="outer-container absolute-center">
  <div class="inner-container">
    <p><strong>@include center(absolute, both);</strong><br>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</p><!--Content with an unknown height -->
  </div><!-- /ui-block-inner -->
</div><!-- /ui-block -->

<div class="outer-container absolute-center-vertical">
  <div class="inner-container">
    <p><strong>@include center(absolute, vertical);</strong><br>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</p><!--Content with an unknown height -->
  </div><!-- /ui-block-inner -->
</div><!-- /ui-block -->

<div class="outer-container absolute-center-horizontal">
  <div class="inner-container">
    <p><strong>@include center(absolute, horizontal);</strong><br>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</p><!--Content with an unknown height -->
  </div><!-- /ui-block-inner -->
</div><!-- /ui-block -->

<div class="no-flexbox no-csstransforms">
  <div class="outer-container absolute-center-legacy">
    <div class="inner-container">
      <p><strong>@include center(both);</strong><br>NO FLEXBOX OR CSS TRANSFORMS. If you go into inspector and shut off all flexbox and transforms, you'll see this work.</p><!--Content with an unknown height -->
    </div><!-- /ui-block-inner -->
  </div><!-- /ui-block -->
</div>

<div class="no-flexbox no-csstransforms">
  <div class="outer-container center">
    <div class="inner-container">
      <p><strong>@include center(both);</strong><br>NO FLEXBOX OR CSS TRANSFORMS. If you go into inspector and shut off all flexbox and transforms, you'll see this work.</p><!--Content with an unknown height -->
    </div><!-- /ui-block-inner -->
  </div><!-- /ui-block -->
</div>
            
          
!
            
              $support-ie7: false;
$support-ie8: true;
$ie-conditional-classes: true;

// Absolute Center
//-------------------------------
@mixin center($axis: both, $method: flex, $dimensions: null) {
  
  @if $method == flex {
    @include center-flex($axis);
  }
  
  @else if $method == absolute {
    @include center-absolute($axis);
  }
  
  @if $dimensions != null {
    @include center-absolute-legacy($dimensions); 
  }
}

@mixin center-absolute($axis) {
  position: relative;
  > * {
    position: absolute;
    @if ($axis == "both") {
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      
        .no-csstransforms & {
        > * {
          top: auto;
          left: auto;
        }
      }
    } @else if ($axis == "horizontal") {
      left: 50%;
      transform: translate(-50%, 0);
      
        .no-csstransforms & {
        > * {
          display: block;
          margin-left: auto;
          margin-right: auto;
        }
      }
    } @else if ($axis == "vertical") {
      top: 50%;
      transform: translate(0, -50%);
      
      .no-csstransforms & {
        display: table;
        > * {
          display: table-cell;
          top: auto;
          vertical-align: middle;
        }
      }
    }
  }
}

@mixin center-flex($axis) {
  display: flex;
  @if $axis == horizontal {
    justify-content: center;
    //TODO: flexboxlegacy support
    .no-flexbox &, .no-flexbox-legacy & {
      > * {
        display: block;
        margin-left: auto;
        margin-right: auto;
      }
    }
  } @else if $axis == vertical {
    align-items: center;
    .no-flexbox & {
      display: table;
      > * {
        display: table-cell;
        top: auto;
        vertical-align: middle;
      }
    }
  } @else if $axis == both {
    justify-content: center;
    align-items: center;
    .no-flexbox & {
      position: relative;
      > * {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
    }
    .no-flexbox.no-csstransforms & {
      > * {
        position: relative;
        top: auto;
        left: auto;
        margin-left: auto;
        margin-right: auto;
      }
    }
  }
} // center-flex

@mixin center-absolute-legacy($dimensions) {
  // If only one value is supplied,
  // default to center the width
  @if length($dimensions) == 1 {
    $dimensions: $dimensions n;
  }

  $width: nth($dimensions, 1);
  $height: nth($dimensions, 2);
  
  .no-flexbox.no-csstransforms & {
    position: relative;
    > * {
      position: absolute;

      @if $width != n {
        @if not(unitless($width)) {
          width: $width;
          margin-left: (-$width / 2);
        }
        @if unitless($width) {
          width: $width + px;
          margin-left: (-$width / 2) + px;
        }
        left: 50%;
      }

      @if $height != n {
        @if not(unitless($height)) {
          height: $height;
          margin-top: (-$height / 2);
        }
        @if unitless($height) {
          height: $height + px;
          margin-top: (-$height / 2) + px;
        }
        top: 50%;
      }
    }
  }
} // absolute-center-legacy





// apply the mixin
.vertical-center {
  @include center(vertical);
}

// apply the mixin
.center {
  @include center();
  .inner-container {
    background-color: lighten(#bada55, 10%);
    width: 75%;
  }
}

.absolute-center {
  @include center(both, absolute);
}

.absolute-center-vertical {
  @include center(vertical, absolute);
}

.absolute-center-horizontal {
  @include center(horizontal, absolute);
}

.absolute-center-legacy {
  @include center(both, flex, 200px 200px);
  background-color: lighten(#bada55, 10%);
}





// Demo it
//-------------------------------

// just some style for the demo
.outer-container {
  float: left;
  width: 24%;
  min-height: 300px;
  padding: 10px;
  border: 10px solid #ffffff;
  background-color: #bada55;
}

// a little resetting
html,
body {
  height: 100%;
  margin: 20px;
}
*,
*:before,
*:after {
	box-sizing: border-box;
}
p {
  margin: 0;
}

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console