Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <main>
  <h1>Vertical &amp; horizontal centring*</h1>


  <section class=container-flexbox>
    <div class=centered-flexbox>

      <figure>
      <figcaption>IE10+ (preferred method)</figcaption>
        <pre><code class=language-css spellcheck=false contenteditable>.container-flexbox { 
  min-height: 100vh; /* Unit &amp; size irrelevant */
  display: flex; /* Collapses box */
  align-items: center;
  justify-content: center;
}
.centered-flexbox {
}</code></pre>
      </figure>

    </div>
  </section>


  <section class=container-grid>
    <div class=centered-grid>

      <figure>
      <figcaption>Grid method</figcaption>
        <pre><code class=language-css spellcheck=false contenteditable>.container-grid {
  min-height: 100vh; /* Unit &amp; size irrelevant */
  display: grid;
  place-content: center;
}
.centered-grid {
}</code></pre>
      </figure>

    </div>
  </section>



  <section class=container-noFixedHeight>
    <div class=centered-noFixedHeight>

      <figure>
      <figcaption>IE9+</figcaption>
        <pre><code class=language-css spellcheck=false contenteditable>.container-noFixedHeight {
  position: relative;
  min-height: 100vh; /* Unit &amp; size irrevelant */
}
.centered-noFixedHeight {
  position: absolute; /* Collapses box */
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}</code></pre>
      </figure>

    </div>
  </section>



  <section class=container-float>

    <div class=centered-float>
      <figcaption>IE9+</figcaption>
      <figure>
        <pre><code class=language-css spellcheck=false contenteditable>.container-float {
  height: 100vh; /* Unit irrelevant */
}
.container-float::after {
  content: "";
  display: table;
  clear: both;
}
.centered-float {
  position: relative;
  float: left; /* Collapses box */
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}</code></pre>
      </figure>
    </div>

  </section>



  <section class=container-ellipsis>
    <div class=centered-ellipsis>
      <span class=centered-ellipsis-span>
        Centre?
      </span>
    </div>
  </section>



  <section class=container-credits>
    <div class=centered-credits>

      <p>Further variants: <a class=lnk rel=noopener target=_blank title="[new window]" href="https://web.dev/centering-in-css/" target=_blank title="[new window]">Centering in CSS</a></p>

      <p>More options and variants: <a class=lnk rel=noopener target=_blank title="[new window]" href="https://www.smashingmagazine.com/2013/08/absolute-horizontal-vertical-centering-css/" target=_blank title="[new window]">Absolute horizontal vertical centering CSS</a></p>

      <p>Modern CSS: <a class=lnk rel=noopener target=_blank title="[new window]" href="https://moderncss.dev/resource-the-complete-guide-to-centering-in-css/">Complete Guide to Centering in CSS</a></p>

      <p>CSS Tricks: <a class=lnk rel=noopener target=_blank title="[new window]" href="https://css-tricks.com/centering-the-newest-coolest-way-vs-the-oldest-coolest-way/">Centering - newest vs oldest</a></p>

      <p>Photos: <a class=lnk rel=noopener target=_blank title="[new window]" href="https://unsplash.com/">Unsplash</a>

      <p>*Brit eh?</p>

    </div>
  </section>
</main>

[[[https://codepen.io/2kool2/pen/mKeeGM]]]
              
            
!

CSS

              
                /* Generic styles */
body {
  font-family: sans-serif;
  line-height: 1.5;
  margin: 0;
  background-color: #1a1a1a;
  color: #fff;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
main {
  height: 100vh;
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
}
h1 {
  font-weight: 100;
  position: fixed;
  color: #000;
  background-color: rgba(255,255,255, .8);
  top: 1rem;
  left: 50%;
  transform: translate(-50%, 0);
  padding: .5rem 1rem;
  width: 50%;
  min-width: 20rem;
  margin: 0 auto;
  text-align: center;
  line-height: 1.2;
}
p {margin: 1rem; max-width: 34rem;}

figure {margin: 0;}
figcaption {
  background-color: #1a1a1a;
  text-align: center;
  padding: .25rem;
  font-weight: 700;
}
a {
  color: #fff !important;
  text-decoration: none;
  border-bottom: 1px solid rgba(255,255,255, .5);
}

[class^="container-"] {
  scroll-snap-align: start;
}

[class^="centered-"] {
  
  /* Optional: Keep inside viewport */
  max-width: calc(100% - 2rem);
  
  /* OPTIONAL: */
  max-height: calc(100vh - 4rem);

  /* Not required but plays nice */
  transition: all .3s ease-out;
}
[class^="centered-"]:hover,
[class^="centered-"]:focus-within {
  box-shadow: 0 0 .5rem .25rem rgba(0,0,0, .75);
}


/* Required centring styles */


 /* Preferred but doesn't work at all in IE9 */
.container-flexbox {
  min-height: 100vh; /* Unit & size irrevelant */
  display: flex;
  align-items: center;
  justify-content: center;
}
.centered-flexbox {
}


/* OR */


 /* Doesn't work at all in IE9 */
.container-grid {
  min-height: 100vh; /* Unit & size irrevelant */
  display: grid;
  place-content: center;
}
.centered-grid {
/*   margin: 0 auto; */
}


/* OR */


.container-noFixedHeight {
  position: relative;
  min-height: 100vh; /* Unit & size irrevelant */
}
.centered-noFixedHeight {
  position: absolute; /* Collapses box */
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}


/* OR */


.container-float {
  height: 100vh; /* Unit irrevelant */
  /* padding: 1rem; /* Optional */
}
.container-float::after {
  content: "";
  display: table;
  clear: both;
}
.centered-float {
  position: relative;
  float: left; /* Collapses box */
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}


/* OR */


.container-ellipsis {
  position: relative;
  height: 100vh; /* Unit irrevelant */
}
.centered-ellipsis,
.centered-ellipsis-span {
  position: absolute; /* Collapses box */
  max-width: 50vmin;
  max-height: 50vmin;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.centered-ellipsis {
  /* Sets this div to 50% sq */
  bottom: 0;
  right: 0;
}
.centered-ellipsis-span {
}


/* Credits section */
.container-credits {
  height: 100vh; /* Unit irrevelant */
  display: flex;
  align-items: center;
  justify-content: center;
}


/* Pretty-pretties - Not required but sweet */
/* Images unsplash.com */

.container-float {
  background-position: center bottom;
  background-size: cover;
  /* Dmitri Popov */
  background-image: url(https://images.unsplash.com/photo-1497514935393-24b153421536?w=1920);
}
.container-noFixedHeight {
  background-position: center;
  background-size: cover;
  /* Michał Grosicki */
  background-image: url(https://images.unsplash.com/photo-1489343511429-5482f78c15cf?w=1920);
}
.container-absolute {
  background-position: center;
  background-size: cover;
  /* Michał Grosicki */
  background-image: url(https://images.unsplash.com/photo-1494187042164-c3d951f93e5c?w=1920);
}
.container-flexbox {
  background-position: center bottom;
  background-size: cover;
  /* James Padolsey */
  background-image: url(https://images.unsplash.com/photo-1476958526483-36efcaa80b1b?w=1920);
}
.container-grid {
  background-position: center top;
  background-size: cover;
  /* Wolfgang Hasselmann */
  background-image: url(https://unsplash.com/photos/mZabJ9h8_jc/download?force=true&w=1920);
}


.container-ellipsis {
  background-position: center;
  background-size: cover;
  /* Jeremy Thomas */
  background-image: url(https://unsplash.com/photos/E0AHdsENmDg/download?force=true&w=1920);
}
.centered-ellipsis {
  border-radius: 50%;
  box-shadow: inset 0 2.5rem 5rem 1.5rem rgba(0,0,0, .8), 0 0 1rem .125rem #000;
  background: linear-gradient(to bottom, #342a29 0%,#b4300a 9%,#ffc102 30%,#edddd7 44%,#e8ebf2 50%,#edddd7 56%,#ffc102 70%,#b4300a 87%,#342a29 100%);
}
.centered-ellipsis:hover {
  box-shadow: inset 0 2.5rem 5rem 1.5rem rgba(0,0,0, .8), 0 0 1rem .125rem #000;
}
.centered-ellipsis-span {
  background-color: #000;
  padding: 1rem;
  font-weight: 700;
  border-radius: 50%;
  box-shadow: 0 0 1rem 2rem #b4300a;
  box-shadow: 0 0 3vmin 4vmin #b4300a;
}


.container-credits {
  padding: 1rem;
  height: calc(100vh - 2rem);
  background-position: center bottom;
  background-size: cover;
  background-image: url(https://images.unsplash.com/photo-1431576901776-e539bd916ba2?w=1920);
}
.centered-credits {
  background-color: rgba(0,0,0, .85);
}
.centered-credits:hover {
  background-color: rgba(0,0,0, 1);
}
              
            
!

JS

              
                // Prism used for code highlighting
// Force dark mode
document.body.setAttribute('data-lightmode', 'dark');
              
            
!
999px

Console