css水平垂直居中有哪些,写出代码?

2018-05-2407:19:16网页制作Comments2,026 views字数 387阅读模式

1.已知固定宽高元素文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/3553.html

.parent {
    position: relative;
}
.child {
    width: 300px;
    height: 100px;
    padding: 20px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -70px 0 0 -170px;
}

2. 未知宽高元素文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/3553.html

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

3.使用flex布局文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/3553.html

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

4.利用grid布局文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/3553.html

.parent {
  height: 140px;
  display: grid;
}
.child { 
  margin: auto;
}
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/zhizuo/3553.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/zhizuo/3553.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定