最全的CSS盒子(div)水平垂直居中布局,对CSS 布局掌握程度决定你在 Web 开发中的开发页面速度。

相对于屏幕

方法一:利用定位

<div class="box"></div>
<style>
body {
background: green;
}
.box {
position: fixed;
top: 50%;
left: 50%;
margin: -150px 0 0 -150px;
width: 300px;
height: 300px;
background: orange;
}
</style>

设置 Position 为 fixed 定位,top 和 left 各设置 50%,margin 设置负的容器宽高的一半。

方法二:利用 transform

<div class="box"></div>
<style>
body {
background: green;
}
.box {
position: fixed;
top: 50%;
left: 50%;
width: 300px;
height: 300px;
transform: translate(-50%, -50%);
background: orange;
}
</style>

设置 Position 为 fixed 定位,top 和 left 各设置 50%,transform 的 translate 设置上、左 -50%。

方法三:利用 margin auto

<div class="box"></div>
<style>
body {
background: green;
}
.box {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
width: 300px;
height: 300px;
background: orange;
}
</style>

设置 Position 为 fixed 定位,上、右、下、左设置为 0,margin 设置为 auto。

相对于父容器

方法一:利用定位

<div class="parent">
<div class="child"></div>
</div>
<style>
.parent {
position: relative;
margin: 100px auto 0;
width: 500px;
height: 500px;
background: green;
}
.child {
position: absolute;
top: 50%;
left: 50%;
margin: -100px 0 0 -100px;
width: 200px;
height: 200px;
background: orange;
}
</style>

父容器设置为相对定位,子容器设置为绝对定位,top 和 left 各设置 50%,margin 设置负的子容器宽高的一半。

方法二:利用 transform

<div class="parent">
<div class="child"></div>
</div>
<style>
.parent {
position: relative;
margin: 100px auto 0;
width: 500px;
height: 500px;
background: green;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 200px;
height: 200px;
background: orange;
}
</style>

父容器设置为相对定位,子容器设置为绝对定位,top 和 left 各设置 50%,transform 的 translate 设置上、左 -50%。

方法三:利用 margin auto

<div class="parent">
<div class="child"></div>
</div>
<style>
.parent {
position: relative;
margin: 100px auto 0;
width: 500px;
height: 500px;
background: green;
}
.child {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
width: 200px;
height: 200px;
background: orange;
}
</style>

父容器设置为相对定位,子容器设置为绝对定位,上、右、下、左设置为 0,margin 设置为 auto。

方法四:利用 flex

<div class="parent">
<div class="child"></div>
</div>
<style>
.parent {
position: relative;
margin: 100px auto 0;
width: 500px;
height: 500px;
display: flex;
justify-content: center;
align-items: center;
background: green;
}
.child {
width: 200px;
height: 200px;
background: orange;
}
</style>

父容器 display 设置为 flex,水平垂直设置为居中。

方法五:计算父盒子与子盒子的空间距离

<div class="parent">
<div class="child"></div>
</div>
<style>
.parent {
margin: 100px auto 0;
width: 500px;
height: 500px;
overflow: hidden;
background: green;
}
.child {
margin: 150px auto;
width: 200px;
height: 200px;
background: orange;
}
</style>

计算父盒子与子盒子的空间距离。

微信交流群

前端面试剑指 Offer (3群)

【最全】CSS盒子(div)水平垂直居中居然还有这种方式的更多相关文章

  1. css实现div水平垂直居中

    中秋快到了,祝大家中秋快乐. 平时大家写bug过程中肯定会遇到让div框水平或者垂直居中,然而有时候能居中,有时候不能居中.我把平时遇到的一些方法写出来,如果对你有用,那便是晴天. 1.text-al ...

  2. css让div水平垂直居中

    示例1: .div1{ width:200px; height:300px; border:1px solid #000; position: relative; } .div2{ width: 40 ...

  3. CSS实现DIV水平 垂直居中-1

    水平大家都知道,一般固定宽度给个margin:0 auto:就可以了.下面实现水平垂直都居中 HTML <div class="parent"> </div> ...

  4. Css控制div水平垂直居中显示

    <style>#info{height:0px; width:0px;top:50%; left:50%;position:absolute;}#center{background:#FF ...

  5. CSS控制DIV水平垂直居中

    <div style="position:absolute; width: 600px; height: 200px; left: 50%; top: 50%; margin-left ...

  6. DIV水平垂直居中的CSS兼容写法

    DIV水平垂直居中,非IE浏览器可以用CSS3来处理,IE浏览器中分别处理IE6和/IE7.IE8.IE9. 在IE低版本中,虽然大致上没有问题,但还是有一些细微的显示问题. 示例如下: <!D ...

  7. 如何让div水平垂直居中

    引子 我们经常遇到需要把div中的内容进行水平和垂直居中.所以,这里介绍一种方法,可以使div水平居中和垂直居中. 代码: <!DOCTYPE html> <html lang=&q ...

  8. Flexbox制作CSS布局实现水平垂直居中

    Flexbox实现一个div元素在body页面中水平垂直居中: <!DOCTYPE html><html lang="en"><head>  & ...

  9. 文字以及div水平垂直居中

    文字以及div水平垂直居中.md <div class=”content”> <div class=”mydiv”> huangyingnin! </div>< ...

随机推荐

  1. SQL安装

    安装教程 点击传送 遇到的问题 解决方案1:

  2. Django学习——Django测试环境搭建、单表查询关键字、神奇的双下划线查询(范围查询)、图书管理系统表设计、外键字段操作、跨表查询理论、基于对象的跨表查询、基于双下划线的跨表查询

    Django测试环境搭建 ps: 1.pycharm连接数据库都需要提前下载对应的驱动 2.自带的sqlite3对日期格式数据不敏感 如果后续业务需要使用日期辅助筛选数据那么不推荐使用sqlite3 ...

  3. Blazor和Vue对比学习(基础1.2):模板语法和Razor语法

    Vue使用模板语法,Blazor使用祖传的Razor语法,从逻辑和方向上看,两者极为相似,比如: 都基于HTML 都通过声明式地将组件实例的状态(数据/方法)绑定到呈现的DOM上 都通过指令实现更加丰 ...

  4. 低代码 —— 初步认识 Appsmith

    初步认识 Appsmith appsmith 是什么 appsmith 是 github 上的一个开源项目,截至此刻(20220512)有 17.7k Star. Appsmith 是一个低代码.开源 ...

  5. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

  6. Linux内网渗透

    Linux虽然没有域环境,但是当我们拿到一台Linux 系统权限,难道只进行一下提权,捕获一下敏感信息就结束了吗?显然不只是这样的.本片文章将从拿到一个Linux shell开始,介绍Linux内网渗 ...

  7. 【单片机】CH32V103串口IDLE空闲中断

    CH32V103c8t6 在寻找解决接收完数据后,怎么即时判断数据已经完成了接收.发现串口有一个IDLE空闲中断.如下图描述: 意思是在串口接收完一帧数据 会产生一个中断,此时程序可判断为数据已接收完 ...

  8. Es图形化软件使用之ElasticSearch-head、Kibana,Elasticsearch之-倒排索引操作、映射管理、文档增删改查

    今日内容概要 ElasticSearch之-ElasticSearch-head ElasticSearch之-安装Kibana Elasticsearch之-倒排索引 Elasticsearch之- ...

  9. 碎碎念软件研发02:敏捷之Scrum

    一.什么是 Scrum 1.1 Scrum 定义 Scrum 是敏捷开发方法之一,它使用比较广泛. 敏捷的其它开发方法还有 XP(极限编程).FDD(特性驱动开发).Crystal(水晶方法).TDD ...

  10. UNION 与 UNION ALL 的区别

    UNION:合并查询结果,并去掉重复的行. UNION ALL:合并查询结果,保留重复的行. 举例验证说明: 创建两个表:user_info 和 user_info_b,设置联合主键约束,联合主键的列 ...