卖坚果的怪叔叔 卖坚果的怪叔叔
  • 首页
  • 技术
  • 生活
  • 记录
  • 朋友
  • 常用代码
  • 关于
首页 › 技术 › 常见的CSS文字居中显示

常见的CSS文字居中显示

坚果大叔
2020-02-21 10:31:31技术阅读数 287

1、利用line-height和vertical-align

html

<div class="box">
    <span>测试文字</span>
</div>

css

.box{
    width: 200px;
    height: 200px;
    overflow: hidden;
    background: #ccc;
    text-align: center;
}
.box span{
    vertical-align: middle;
    line-height: 200px;
}

2、利用display:table-cell实现水平垂直居中显示

html

<div class="table">
    <span class="cell">测试文字测试文字测试文字测试文字</span>
</div>

css

.table{
    display: table;
}
.cell{
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}

3、利用定位方式position+transform实现水平垂直居中显示

html

<div class="box">
    <span>测试文字测试文字测试文字测试文字</span>
</div>

css

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

4、利用display:flex

html

<div class="flex">
    <span>测试文字测试文字测试文字测试文字</span>
</div>

css

display:flex;
flex-direction: row;
justify-content: space-around; 
align-items: center;

5、利用display:box

html

<div class="box">
    <span>测试文字测试文字测试文字测试文字</span>
</div>

css

display: -webkit-box;
-webkit-box-orient: horizontal;
-webkit-box-pack: center;
-webkit-box-align: center;
CSS
赞赏 赞(1)
本文系作者 @坚果大叔 原创发布在 卖坚果的怪叔叔。未经许可,禁止转载。
Vue改变数组值,页面视图为何不刷新?
上一篇
什么是函数?
下一篇
今日天气
摸鱼日历
摸鱼人日历
近期文章
  • 🌚🌚🌚🌚
  • TS文档
  • 密码保护:暂存
  • deep 不生效!
  • 过去的,一点记录!
归档
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年5月
  • 2021年4月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年3月
  • 2018年2月
  • 2017年12月
  • 2017年11月
  • 2017年9月
🌚🌚🌚🌚
2022-05-17 22:12:24
75 3 0
TS文档
2022-05-11 21:09:30
79 0 1
密码保护:暂存
2022-05-11 10:21:14
57 0 0
deep 不生效!
2022-05-03 14:37:52
180 1 1
1
  • 1
博主

一枚佛系前端开发,会一丢丢摄影,喜欢折腾,爱好美食。分享点前端技巧、笔记以及各种有趣的APP和资源教程♥♥

友链
Lieme
公众号
西豆 崔欣 执行上下文 卖坚果的怪叔叔 集赞助手
Copyright © 2017-2022 卖坚果的怪叔叔

苏ICP备18048410号-2
  • 首页
  • 技术
  • 生活
  • 记录
  • 朋友
  • 常用代码
  • 关于
# WordPress # # CSS #
坚果大叔
192
文章
47
评论
111
喜欢