文章目录

  • 水平垂直居中
    • 一、实现水平垂直居中的方式
    • 二、具体实现
      • 元素定宽高
        • 利用定位 + calc
        • 利用定位 + margin:负值
      • 元素不定宽高
        • 利用定位 + margin:auto
        • 利用定位 + transform
        • flex 布局
        • grid 布局
        • table 布局
    • 另:行内元素水平垂直居中
    • 参考资料

水平垂直居中

一、实现水平垂直居中的方式

元素定宽高(知道元素宽高)

  • 利用定位 + calc
  • 利用定位 + margin:负值

元素不定宽高(不知道元素宽高)

  • 利用定位 + margin:auto
  • 利用定位 + transform
  • flex 布局
  • grid 布局
  • table 布局

二、具体实现

元素定宽高

利用定位 + calc

<style>.outer {position: relative;width: 200px;height: 200px;background-color: red;}.inner {position: absolute;top: calc(50% - 50px);left: calc(50% - 50px);width: 100px;height: 100px;background-color: yellow;}
</style>
<div class="outer"><div class="inner"></div>
</div>

利用定位 + margin:负值

<style>.outer {position: relative;width: 200px;height: 200px;background-color: red;}.inner {position: absolute;top: 50%;left: 50%;margin-left: -50px;margin-top: -50px;width: 100px;height: 100px;background-color: yellow;}
</style>
<div class="outer"><div class="inner"></div>
</div>

元素不定宽高

利用定位 + margin:auto

<style>.outer {position: relative;width: 200px;height: 200px;background-color: red;}.inner {position: absolute;top: 0;bottom: 0;left: 0;right: 0;margin: auto;width: 100px;height: 100px;background-color: yellow;}
</style>
<div class="outer"><div class="inner"></div>
</div>

利用定位 + transform

<style>.outer {position: relative;width: 200px;height: 200px;background-color: red;}.inner {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);width: 100px;height: 100px;background-color: yellow;}
</style>
<div class="outer"><div class="inner"></div>
</div>

缺点:兼容性不佳

flex 布局

<style>.outer {display: flex;justify-content: center;align-items: center;width: 200px;height: 200px;background-color: red;}.inner {width: 100px;height: 100px;background-color: yellow;}
</style>
<div class="outer"><div class="inner"></div>
</div>

缺点:兼容性不佳

grid 布局

<style>.outer {display: grid;justify-content: center;align-items: center;width: 200px;height: 200px;background-color: red;}.inner {width: 100px;height: 100px;background-color: yellow;}
</style>
<div class="outer"><div class="inner"></div>
</div>

缺点:兼容性不佳

table 布局

<style>.outer {display: table-cell;text-align: center;vertical-align: middle;width: 200px;height: 200px;background-color: red;}.inner {display: inline-block;width: 100px;height: 100px;background-color: yellow;}
</style>
<div class="outer"><div class="inner"></div>
</div>

另:行内元素水平垂直居中

水平居中

  • 在行内元素的父元素设置:text-align: center;
  • flex布局:在行内元素的父元素设置:display: flex;justify-content: center;

垂直居中

  • 单行文本

    • line-height 设置为父元素的高度
    • flex布局:在行内元素的父元素设置:display: flex;align-items: center;
  • 多行文本
    • 在父元素设置: disaply: table-cell; vertical-align: middle; (多行文本垂直居中也适合单行文本的垂直居中)

参考资料

  • 面试官:你能实现多少种水平垂直居中的布局(定宽高和不定宽高)
  • 面试官:元素水平垂直居中的方法有哪些?如果元素不定宽高呢?

水平垂直居中的几种方式相关推荐

  1. css设置子盒子水平垂直居中(四种方式)

    css设置子盒子水平垂直居中(四种方式) 1. 使用绝对定位 <!DOCTYPE html> <html><head><meta charset=" ...

  2. CSS水平垂直居中的五种方式

    CSS水平垂直居中的几种方式 absolute and -margin 需要设置盒子的宽度和高度 .father{border:1px solid red;width: 80vh;height: 90 ...

  3. css实现水平垂直居中的七种方式

    css实现水平垂直居中的七种方式 一.使用grid布局 二.使用flex布局 三.使用定位+外边距 四.使用定位+平移 五.使用外边距 + 平移 六.使用文本对齐 + 行高 七.使用表格单元 一.使用 ...

  4. HTML水平垂直居中的四种方式

    第一种方式是使用margin进行移动 水平居中 margin:0 auto; <!DOCTYPE html> <html lang="en"> <he ...

  5. CSS实现水平垂直居中的几种方式

    1.最简单 margin: 0 auto line-height等于height .outer {width: 200px;padding: 200px;background-color: pink; ...

  6. div盒子水平垂直居中的几种方式

    1. div水平居中 div{width:100px;height:100px;margin:0 auto; } 2. div水平垂直居中 这里要有两层或者给body设置高度100vh,然后给元素设置 ...

  7. 【前端】水平垂直居中的几种方式

    1. flex 布局 flex布局也叫弹性布局,我认为flex box布局是一种十分优雅的布局方式,兼容性一般,使用起来非常简单. display: flex;// flex-direction默认是 ...

  8. CSS实现水平垂直居中的6种方式

    大家好,我是小梅,公众号:「小梅的前端之路」 原创作者. 作为在前端领域不断探索的一员,在此记录开发中遇到的问题,如果你也遇到了相同的问题,希望本文对你有帮助. 在开发中经常需要实现的一个页面效果是: ...

  9. html 水平垂直居中,css水平垂直居中有几种实现方式?

    项目中经常碰到需要实现水平垂直居中的样式.下面就总结几种常用的方法 css水平垂直居中有几种实现方式? 1.水平对齐+行高 [思路一]text-align + line-height实现单行文本水平垂 ...

最新文章

  1. 他,16岁辍学创业,如今已身家过亿!今年将实现飞向太空的梦想
  2. histeq函数实现直方图的均衡化和规定化
  3. python len ljust_Python string.ljust方法代码示例
  4. 洛谷 P1598 垂直柱状图【字符串】
  5. c++thread里暂停线程_多线程技术
  6. Android异步处理:Handler+Looper+MessageQueue深入详解
  7. Moss/Sharepoint:自定义 Oracle Membership
  8. 网友疯买、雷军力撑,又一家国货站起来了!
  9. 基于8086CPU微处理器的汇编学习之内存空间的编辑
  10. 3-8 堆栈模拟队列 (25 分)
  11. paip.regf文件读取与编辑
  12. openCV 图像阀值
  13. jdk重复安装,Error:Registry key ‘Software\JavaSoft\Java Runtime Environment\CurrentVersion(已解决)
  14. ASAN Pass源码分析(六)——全局变量插桩
  15. linux脚本简介,Linux Shell脚本简介
  16. SecureCRT自动保存日志设置
  17. 金融借贷平台大数据风控解决方案
  18. outlook你的邮件服务器证书无效,outlook, webmail.xxx.com 证书无效过期,无法连接到服务器,outlook无法收发邮件...
  19. 面试汇总:这是一份全面详细的Android面试指南
  20. java程序员云计算学习路线

热门文章

  1. CH583/CH579蓝牙智能遥控器方案
  2. 全免费、保姆级Eclipse32位软件、安装、运行一条龙记录
  3. wer 流程图编程_孩子学完各个阶段的编程课能够参加哪些比赛?
  4. 笔记本电脑点开都是计算机,笔记本电脑所有程序都打不开怎么办
  5. Crontab中的除号(slash)到底怎么用?
  6. 将web网站转为App
  7. ffplay flv mp4 转_C#调用FFmpeg将flv视频格式转换成mp4格式
  8. [转]内存与进程管理器
  9. SQLserver中的内连接和左连接
  10. android手机安装carplay,安卓系统适用carplay经验分享