博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现水平垂直居中的几种方法
阅读量:4617 次
发布时间:2019-06-09

本文共 1174 字,大约阅读时间需要 3 分钟。

水平居中

  1. 如果是行内元素,要实现水平居中只需要将父元素设置为text-align=center
  2. 如果是固定宽度的块状元素,设置该元素本身为margin: 0 auto
  3. css3的新属性font-content,自动将元素宽度缩小到内容的宽度,然后使用margin:0 auto可以轻松的实现水平居中(目前只支持chrome和FireFox)

.son{ width: -moz-fit-content; width: -webkit-fit-content; width:fit-content; margin:0 auto; }

   4.绝对定位以及margin-left的负值实现水平居中

    

.son {            position: absolute;            width: 50px;            left: 50%;            margin-left: -25px;(宽度的一半)            background-color: blue;            text-align: center;        }

   5.绝对定位left right同时设置为0 同时设置margin:0 auto

.son{            position: absolute;            width: 50px;            left: 0;            right: 0;            background-color: blue;            margin: 0 auto;            height: 100%;        }

垂直居中

  1. 若元素为单行文本,直接设置其text-align为父元素的高度
  2. 利用position以及top bottom属性

 

.son{    position:absolute;    height:固定;    top:0;    bottom:0;    margin:auto 0;}

 

  3.利用margin的负值

.son{    position:absolute;    top:50%;    height:固定;    margin-top:-0.5高度;}

  4.利用vertical-align

.parent::after, .son{    display:inline-block;    vertical-align:middle;}.parent::after{    content:'';    height:100%;}

 

转载于:https://www.cnblogs.com/yinping/p/11262620.html

你可能感兴趣的文章
SPOJ #11 Factorial
查看>>
City Upgrades
查看>>
“人少也能办大事”---K2 BPM老客户交流会
查看>>
关于七牛进行图片添加文字水印操作小计
查看>>
DataSource数据库的使用
查看>>
CentOS开启samba实现文件共享
查看>>
MSSQL使用sqlbulkcopy批量插入数据
查看>>
证明一个数能被3整除,当且仅当它的各位数的和能被3整除
查看>>
2018秋寒假作业4—PTA编程总结1
查看>>
android自适应屏幕
查看>>
2019-北航面向对象-电梯作业总结
查看>>
SqlHelper
查看>>
初识算法、数据结构
查看>>
Luogu4069 SDOI2016 游戏 树链剖分、李超线段树
查看>>
Java的内部类真的那么难以理解?
查看>>
一文搞懂Java环境,轻松实现Hello World!
查看>>
hash实现锚点平滑滚动定位
查看>>
也谈智能手机游戏开发中的分辨率自适应问题
查看>>
【转】MYSQL数据库设计规范与原则
查看>>
《中国大历史》—— 读后总结
查看>>