CSS居中方案介绍
来源:互联网 发布:php书籍 编辑:程序博客网 时间:2024/05/18 23:15
1. 水平居中
1.1. transform居中
通过transform
居中的核心思想是让居中元素先通过margin-left
属性向右移动50%,然后再利用transform
属性左移元素宽度的一半,从而达到居中的效果:
.parent { position:relative; width: 300px; height: 400px; background-color: red;}.child { position: absolute; /*第一个position不为static的父元素为其父元素*/ width: 200px; height: 200px; background-color: yellow; left: 50%; transform: translateX(-50%);}<div class="parent"> <div class="child"></div></div>
1.2. flex居中
利用flex居中是一种更为简单的方式,通过设置父元素的justify-content
为center
即可。
.parent2 { margin-top: 20px; width: 300px; height: 100px; display: flex; justify-content: center; background-color: red;}.child2 { background-color: yellow;}<div class="parent2"> <div class="child2">fsdjfklajsdklfklasjfkljksdlf</div></div>
1.3. inline-block
通过将块级子元素
设置inline-block
属性,然后再将父元素的text-align
属性设置为center
即可:
.parent1 { text-align: center; width: 300px; height: 30px; background-color: red; margin-top: 30px;}.child1 { display: inline-block; background-color: yellow;}<div class="parent1"> <div class="child1">fsdjfklajsdklfklasjfkljksdlf</div></div>
1.4. margin居中
通过设置子元素的margin
来使其居中:
.parent3 { position:relative; width: 300px; height: 100px; background-color: red; margin-top: 20px;}.child3 { position: absolute; /*第一个position不为static的父元素为其父元素*/ width: 200px; background-color: yellow; left: 0; right: 0; margin: 0 auto;}<div class="parent3"> <div class="child3">fsdjfklajsdklfklasjfkljksdlf</div></div>
2. 垂直居中
2.1 translate居中
.parent4 { position:relative; width: 300px; height: 400px; background-color: red; margin-top: 20px;}.child4 { position: absolute; /*第一个position不为static的父元素为其父元素*/ width: 200px; height: 200px; background-color: yellow; top: 50%; transform: translateY(-50%);}<div class="parent4"> <div class="child4">fsdjfklajsdklfklasjfkljksdlf</div></div>
同样的,结合1.1
就可以实现水平、垂直居中:
.parent5 { position:relative; width: 300px; height: 300px; background-color: red; margin-top: 20px;}.child5 { position: absolute; /*第一个position不为static的父元素为其父元素*/ width: 200px; height: 200px; background-color: yellow; top: 50%; left: 50%; transform: translate3D(-50%, -50%, 0);}<div class="parent5"> <div class="child5">fsdjfklajsdklfklasjfkljksdlf</div></div>
2.2 flex居中
利用flex的align-items
属性可以解决flex的垂直居中:
.parent6 { margin-top: 20px; width: 300px; height: 100px; display: flex; align-items: center; background-color: red;}.child6 { background-color: yellow;}<div class="parent6"> <div class="child6">fsdjfklajsdklfklasjfkljksdlf</div></div>
同样的,结合1.2
可以做到水平、垂直均居中:
.parent7 { margin-top: 20px; width: 300px; height: 100px; display: flex; align-items: center; justify-content: center; background-color: red;}.child7 { background-color: yellow;}<div class="parent7"> <div class="child7">fsdjfklajsdklfklasjfkljksdlf</div></div>
2.3 绝对定位
利用绝对定位同样可以实现垂直居中:
.parent8 { position:relative; width: 300px; height: 200px; background-color: red; margin-top: 20px;}.child8 { position: absolute; /*第一个position不为static的父元素为其父元素*/ width: 200px; height: 100px; background-color: yellow; top: 0; bottom: 0; margin: auto 0;}<div class="parent8"> <div class="child8">fsdjfklajsdklfklasjfkljksdlf</div></div>
再参考1.4
的方案,可以做到水平,垂直均居中:
.parent9 { position:relative; width: 300px; height: 200px; background-color: red; margin-top: 20px;}.child9 { position: absolute; /*第一个position不为static的父元素为其父元素*/ width: 200px; height: 100px; background-color: yellow; top: 0; bottom: 0; left: 0; right: 0px; margin: auto auto;}<div class="parent9"> <div class="child9">fsdjfklajsdklfklasjfkljksdlf</div></div>
阅读全文
0 0
- CSS居中方案介绍
- CSS居中方法介绍
- [CSS]各种居中对齐方案
- css 图片文字居中对齐方案
- CSS元素水平垂直居中方案总结
- CSS 幽灵元素方案垂直居中注意事项
- DIV框垂直水平居中的CSS方案
- CSS 最全面的水平垂直居中方案
- DIV+CSS中图片和文字居中问题解决方案
- css垂直水平居中的几种常用方案
- Web前端CSS居中的二种处理方案
- css+div垂直居中布局总结(3种方案)
- CSS居中
- CSS居中
- css居中
- CSS居中
- css居中
- css 居中
- VB文件系统编程中的常用函数
- 【已解决】[Err] MySQL server has gone away
- Ubuntu、linux基础概念及常用操作命令
- WebSocket发送图片或文件的思路(1)
- Matplotlib之3D骨骼关节的动画显示
- CSS居中方案介绍
- Sqlite 命令行导出、导入数据
- 机器学习关键点整理
- MATLAB数字图像处理(一)图像直方图
- Android Studio 新建项目通过git上传到码云
- android studio之jni(上)生成so文件
- Django-模板
- myeclipse10+ 注册机破解步骤
- Linux下查看用户列表