Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

再看垂直居中 #39

Open
naseeihity opened this issue Dec 19, 2017 · 0 comments
Open

再看垂直居中 #39

naseeihity opened this issue Dec 19, 2017 · 0 comments
Assignees
Labels

Comments

@naseeihity
Copy link
Owner

naseeihity commented Dec 19, 2017

之前总结过水平垂直居中的方法,
以及CSS-tricks中也有清晰的总结。

最近发现项目中其他人写的样式中大量使用了display:table-cell去实现水平垂直居中.

.father{
  display: table-cell;
  width: 500px;
  height: 300px;
  background-color: #de3;
  vertical-align: middle;
  text-align: center;
}

.child{
  display: inline-block;
  max-width:400px;
  max-height:200px;
  background-color: #fd2;
}

优势在于不会有position:absolute的负面影响

以及实现多行元素垂直居中:

.father{
  display: table-cell;
  width: 100px;
  height: 200px;//高度设置无效
  background-color: #de3;
  vertical-align: middle;
  text-align: center;
  word-break: normal;
}

这一方法的问题在于:

  • 高度设置无效
  • overflow设置无效
  • margin不感知
  • 与 position/float等存在冲突

多行文字垂直居中:

.flex-center-vertically {
  display: flex;
  justify-content: center;
  flex-direction: column;
  height: 400px;
}

.ghost-center {
  position: relative;
}
.ghost-center::before {
  content: " ";
  display: inline-block;
  height: 100%;
  width: 1%;
  vertical-align: middle;
}
.ghost-center p {
  display: inline-block;
  vertical-align: middle;
}
@naseeihity naseeihity added the CSS label Dec 19, 2017
@naseeihity naseeihity assigned naseeihity and unassigned naseeihity Mar 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant