1. 已知父级宽高并且文本比较简短

 .outline0{width:200px;line-height:200px;text-align:center;background:#ccc;
}
<div class="outline0"><span>test</span>
</div>    
2.转换元素的类型 vertical-align: middle;

.outline2{display:table;width:200px;height: 200px;background:#ccc;
}
.center{display:table-cell;text-align:center;vertical-align: middle;
}
<div class="outline2"><span class="center">test test test,test,test test test test test<span>
</div>
3.适合知道文本比较固定字段时,还有想定哪定哪

.outline3{width: 200px;height: 200px;background: #ccc;position: relative;
}
.text3{position: absolute;left: 50%;margin-left: -90px;display: block;width: 180px;top: 50%;margin-top: -21px;text-align: center;
}
<div class="outline3"><span class="text3">test test test,test,test test test test test<span>
</div>
4.最好用的

原因:使用top: 50%;left: 50%;, 是以左上角为原点,故不处于中心位置
translate(-50%,-50%) 作用是,往上(x轴),左(y轴)移动自身长宽的 50%,以使其居于中心位置

.outline4{width: 200px;height: 200px;background: #ccc;margin-bottom: 10px;text-align: center;position: relative;
}
.text4{position: absolute;left: 50%;top: 50%;transform: translate(-50%, -50%);
}
<div class="outline4"><span class="text4">test test test,test,test test test test test<span>
</div>
5.flex
.body {display: flex;justify-content: center;align-items: center;height:500px;width:100%;
}
.box {width: 50px;height: 50px;background:#000;
}<div class="body "><div class="box"></div></div>

文本在盒子里垂直居中 css相关推荐

  1. CSS自动生成排行榜或者序号(不论高度,文字都能垂直居中CSS样式);

    1. 在子集的伪类上加上content : counter(sectioncounter); counter-increment :sectioncounter; 在父级counter-reset:s ...

  2. css图片在盒子里居中,让图片在div盒子中水平垂直居中

    //调整多张图片,让图片水平垂直居中 function adjustImg(){ let imgDiv = document.getElementsByClassName("img" ...

  3. CSS学习笔记(内边距~文字在盒子里的垂直居中)

    内边距 1.内边距就是内容和边框之间的距离. 2.格式 2.1.非连写 .san1{width: 100px;height:100px;border: 1px solid black;padding- ...

  4. html盒子嵌套居中,css在盒子中垂直居中和固定居中

    顶部固定居中 我是固定的 .w960{ width: 960px; margin:0 auto; } .fixed{ position: absolute; top:0; left: 0; right ...

  5. CSS02_设置盒子水平+垂直居中 设置文本水平+垂直居中

    写在前面:此贴为CSS高频题,针对盒子模型的水平+垂直居中,还提到了其内部文字的居中,如有补充,请留下评论 HTML代码结构: <body><div class="box- ...

  6. CSS 字体 文本 过渡 盒子初步

    12.17 学习笔记 主要内容: 1.CSS字体样式 2.CSS文本样式 3.CSS使用过渡 4.盒子初步 一.CSS字体 1 font-size :字体大小 2.font-family:样式(后加字 ...

  7. 文本和盒子的水平 垂直居中 (18种方法)

    目录 一.水平居中: 1.(子元素居中)父:text-align:center:(行内元素) 2.(自己居中)自己:margin:0 auto:(块级元素) 3.(子元素居中)(子)display:i ...

  8. css数字固定居中怎么使用,css在盒子中垂直居中和固定居中

    顶部固定居中 我是固定的 .w960{ width: 960px; margin:0 auto; } .fixed{ position: absolute; top:0; left: 0; right ...

  9. css一个盒子里可以装3个图片并排吗_John: CSS浮动与清除浮动属性详解(CSS float clear)...

    CSS里的浮动,可以让元素脱离标准流,从左上角或右上角依次贴边排列. 下面这个案例将会帮我们了解浮动的基本情况.下面这段代码块,外面是一个大div,里面包含着3个div,第一个左浮动,后两个无浮动. ...

最新文章

  1. 配置MUX VLAN示例(汇聚层设备)
  2. Java中Dom解析XML
  3. SpringBoot基础入门
  4. 在stm32上使用Arduino IDE(神舟I号为例)
  5. find linux 目录深度_浪里淘沙,详解Linux系统中Find命令的实用技巧
  6. Linux进阶之VMware Linux虚拟机运行提示“锁定文件失败 虚拟机开启模块snapshot失败”的解决办法...
  7. DBCC CHECKDB 数据库或表修复
  8. android有道翻译api,有道智云自然翻译服务API
  9. 群赛 round#5 解题报告(superoxide,choice,rpwt)
  10. VsCode建立工程
  11. 计算机word虚线在哪里,电脑虚线怎么打出来
  12. searchview怎么改hint大小_老司机偷偷告诉你:改水电一般多少钱?水电改造价格怎么算?...
  13. Plustoken重要操盘手已被遣返回国,警方正全力侦查
  14. 查询用户活跃度表登录间隔30天的用户
  15. PHP程序员战地日记
  16. LCD1602和12864简单的介绍
  17. 【BZOJ4416】阶乘字符串(SHOI2013)-状压DP
  18. 与开发者共建移动 App 生态——记华为开发者联盟开放日
  19. MySQL 索引失效详解
  20. 一、 SAP MM模块概览

热门文章

  1. Unity调用外部摄像头/网络摄像头/手机摄像头
  2. linux控制工业设备,恩智浦推用于工业领域的Linux发行版 助力工厂自动化
  3. python中关于数据取整和保留小数点
  4. 图解MESI(缓存一致性协议)
  5. linux排列去重复的命令,实例详细说明linux下去除重复行命令uniq
  6. margin-left与left、margin-right与right区别
  7. python负载均衡
  8. centos6.9中glibc升级失败救援+救援模式挂载硬盘
  9. shiro权限认证HelleWorld示例(最简单的模拟调用方式)
  10. 海豚社区阶段性开发总结