Skip to content

Instantly share code, notes, and snippets.

@ifeiwu
Last active November 19, 2018 08:59
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 ifeiwu/5cc7de1882d73a374bdbe4cb4b6cd42a to your computer and use it in GitHub Desktop.
Save ifeiwu/5cc7de1882d73a374bdbe4cb4b6cd42a to your computer and use it in GitHub Desktop.
CSS3元素垂直居中

HTML

<div class="vertical-container">
  <div class="vertical-center">Centered</div>
</div>

CSS

.vertical-container {
  width: 200px;
  height: 200px;
  position: relative;
  margin: 0 auto;
  background: #fff;
}

.vertical-center {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment