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

实现水平垂直居中的N种方式 #21

Open
DamomHd opened this issue Jan 19, 2021 · 5 comments
Open

实现水平垂直居中的N种方式 #21

DamomHd opened this issue Jan 19, 2021 · 5 comments
Labels

Comments

@DamomHd
Copy link
Owner

DamomHd commented Jan 19, 2021

<div class="container">
  <div class="content"></div>
</div>
.container{
      width:500px;
      height:500px;
      background-color:#999;
}
.content{
     width:200px;
     height:200px;
     background-color:#333;
}
@DamomHd DamomHd added the CSS label Jan 19, 2021
@DamomHd
Copy link
Owner Author

DamomHd commented Jan 19, 2021

absolute + margin

.container{
  position:relative;
}
.content{
  position: absolute;
  top: 0;
  bottom:0;
  right:0;
  left: 0;
  margin:auto;
}

@DamomHd
Copy link
Owner Author

DamomHd commented Jan 19, 2021

absolute + transform

.container {
    position: relative;
}
.content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

@DamomHd
Copy link
Owner Author

DamomHd commented Jan 19, 2021

flex

.container {
    display: flex;
    justify-content: center;
    align-items: center;
}
.content {
    text-align: center;
}

@DamomHd
Copy link
Owner Author

DamomHd commented Jan 19, 2021

grid

.container {
    display: grid;
    justify-items: center;
    align-items: center;
}
.content {
    text-align: center;
}

@DamomHd
Copy link
Owner Author

DamomHd commented Jan 19, 2021

css-table

.container {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
.content{
    display: inline-block;
}

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