当前位置: 首页 > news >正文

CSS 垂直水平居中总结(全)

目录

  • 1,不需要知道元素的宽高
    • 1.1,flex(2种)
    • 1.2,grid(2种)
    • 1.3,verticle-align:middle
    • 1.4,绝对定位
    • 1.5,table-cell
  • 2,需要知道元素的宽高
    • 2.1,绝对定位

以下面的代码为例

<div class="container"><div class="item"></div>
</div>

1,不需要知道元素的宽高

1.1,flex(2种)

最常用的,没有任何限制。

.container{display: flex;justify-content: center;align-items: center;
}

.container{display: flex;
}
.item {margin: auto;
}

1.2,grid(2种)

.container {display: grid;align-content: center;justify-content: center;
}
.container{display: grid;
}
.item {margin: auto;
}

1.3,verticle-align:middle

限制:

  1. 元素 display: inline-block
  2. 需要有一个兄弟元素作为参照物,并且会以最高的兄弟元素为作为参考,垂直于兄弟元素的中心点。所以可以利用伪元素。
.container {text-align: center;
}
.container::before {content : '';display: inline-block;vertical-align: middle;height: 100%;
}
.item {display: inline-block;width: 100px;height: 100px;vertical-align: middle;
}

1.4,绝对定位

.container {position: relative;
}
.item {width: 100px;height: 100px;position: absolute;left: 0;top: 0;bottom: 0;right: 0;margin: auto;
}

上下左右为 0 可以替换为 inset: 0,不过注意可能会有兼容性问题,参考。

1.5,table-cell

限制:元素 display: inline-block

.container {display: table-cell;text-align: center;vertical-align: middle;
}
.item {display: inline-block;width: 100px;height: 100px;
}

2,需要知道元素的宽高

2.1,绝对定位

.container {position: relative;
}
.item {width: 100px;height: 100px;position: absolute;left: 50%;top: 50%;margin: -50px 0 0 -50px;/* 或 */transform: translateX(-50px) translateY(-50px);
}

阮一峰-网格布局

以上。

相关文章:

  • 香港商标注册申请所需资料及办理流程
  • netty使用
  • 【Flink on k8s】- 4 - 在 Kubernetes 上运行容器
  • C#winform上下班打卡系统Demo
  • 鸿蒙系统开发手册 - HarmonyOS内核驱动层源码分析
  • 实现跨VLAN通信、以及RIP路由协议的配置
  • JAVA实现敏感词高亮或打码过滤:sensitive-word
  • TCP通讯
  • Linux UUCP命令教程:如何在Linux系统中进行文件复制(附实例详解和注意事项)
  • hive 命令行中使用 replace 和nvl2 函数报错
  • CopyOnWriteArraySet怎么用
  • log4j2日志框架使用
  • Redisson的基本使用
  • ERP数据仓库模型
  • C#winform点击按钮下载数据库中表的字段到Excel上
  • hexo+github搭建个人博客
  • “Material Design”设计规范在 ComponentOne For WinForm 的全新尝试!
  • 【Linux系统编程】快速查找errno错误码信息
  • Codepen 每日精选(2018-3-25)
  • CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里...
  • css系列之关于字体的事
  • JavaScript中的对象个人分享
  • Java程序员幽默爆笑锦集
  • js作用域和this的理解
  • mockjs让前端开发独立于后端
  • Vue2 SSR 的优化之旅
  • 安卓应用性能调试和优化经验分享
  • 分享一份非常强势的Android面试题
  • 解析 Webpack中import、require、按需加载的执行过程
  • 紧急通知:《观止-微软》请在经管柜购买!
  • 开发了一款写作软件(OSX,Windows),附带Electron开发指南
  • 力扣(LeetCode)56
  • 数据科学 第 3 章 11 字符串处理
  • 想使用 MongoDB ,你应该了解这8个方面!
  • 掌握面试——弹出框的实现(一道题中包含布局/js设计模式)
  • ​​​​​​​​​​​​​​Γ函数
  • ​2021半年盘点,不想你错过的重磅新书
  • ​软考-高级-信息系统项目管理师教程 第四版【第23章-组织通用管理-思维导图】​
  • #我与Java虚拟机的故事#连载02:“小蓝”陪伴的日日夜夜
  • $$$$GB2312-80区位编码表$$$$
  • (6)设计一个TimeMap
  • (LeetCode 49)Anagrams
  • (附源码)springboot家庭装修管理系统 毕业设计 613205
  • (附源码)springboot课程在线考试系统 毕业设计 655127
  • (论文阅读31/100)Stacked hourglass networks for human pose estimation
  • (三分钟了解debug)SLAM研究方向-Debug总结
  • (十三)Java springcloud B2B2C o2o多用户商城 springcloud架构 - SSO单点登录之OAuth2.0 根据token获取用户信息(4)...
  • (四)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (五)大数据实战——使用模板虚拟机实现hadoop集群虚拟机克隆及网络相关配置
  • .Family_物联网
  • .gitignore文件_Git:.gitignore
  • .NET CORE 3.1 集成JWT鉴权和授权2
  • .NET Standard 的管理策略
  • .NET 使用 XPath 来读写 XML 文件
  • .NET 应用启用与禁用自动生成绑定重定向 (bindingRedirect),解决不同版本 dll 的依赖问题