Files
30-seconds-of-code/snippets/ghost-trick.md
Angelos Chalaris 7e69307433 Update tags, expertise and descriptions across
Fix anything that was a bit out of place
2020-03-05 22:52:51 +02:00

673 B

title, tags
title tags
Ghost trick layout,intermediate

Vertically centers an element in another.

<div class="ghost-trick">
  <div class="ghosting"><p>Vertically centered without changing the position property.</p></div>
</div>
.ghosting {
  height: 300px;
  background: #0ff;
}

.ghosting:before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}

p {
  display: inline-block;
  vertical-align: middle;
}

Explanation

  • Use the style of a :before pseudo-element to vertically align inline elements without changing their position property.

Browser support