div 居中方法汇总

简介: 居中问题无论在工作还是在面试中, 都是很常见的下面我来给大家总结几种常用的方法, 大家可根据实际情况选择好了, 废话不多说, 先看下效果法1定位+偏移 (margin)html css.

居中问题无论在工作还是在面试中, 都是很常见的
下面我来给大家总结几种常用的方法, 大家可根据实际情况选择
好了, 废话不多说, 先看下效果


法1

定位+偏移 (margin)

  • html
<div class="father">
    <div class="son"></div>
</div>
  • css
.father {
    position: relative;
    width: 1000px;
    height: 600px;
    background: lightblue;
}
.son {
    position: absolute;
    width: 100px;
    height: 100px;
    background: yellow;
    top: 50%;
    margin-top: -50px; /* 高度的一半 */
    left: 50%;
    margin-left: -50px; /* 宽度的一半 */
}

法2

定位+偏移 (transform)

  • html
<div class="father">
    <div class="son"></div>
</div>
  • css
.father {
    position: relative;
    width: 1000px;
    height: 600px;
    background: lightblue;
}
.son {
    position: absolute;
    width: 100px;
    height: 100px;
    background: yellow;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

法3

利用 margin: auto; 自动分配多余空间

  • html
<div class="father">
    <div class="son"></div>
</div>
  • css
.father {
    position: relative;
    width: 1000px;
    height: 600px;
    background: lightblue;
}
.son {
    position: absolute;
    width: 100px;
    height: 100px;
    background: yellow;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
}

top、left、right、bottom 的值相等即可,不一定要都是0

法4

Flex 布局

  • html
<div class="father">
    <div class="son"></div>
</div>
  • css
.father {
    width: 1000px;
    height: 600px;
    background: lightblue;
    display: flex;
    align-items: center; /* 垂直居中 */
    justify-content: center; /* 水平居中 */
}
.son {
    width: 100px;
    height: 100px;
    background: yellow;
}

法5

text-align: center; + vertical-align: middle;

  • html
<div class="father">
    <div class="son"></div>
    <div class="daughter"></div>
</div>
  • css
.father {
    width: 1000px;
    height: 600px;
    background: lightblue;
    text-align: center;
}
.son {
    display: inline-block;
    width: 100px;
    height: 100px;
    background: yellow;
    vertical-align: middle;
}
.daughter {
    display: inline-block;
    width: 0;
    height: 100%;
    vertical-align: middle;
}

暂时想到这么多, 还有其他方法的小伙伴们欢迎补充, 我会将你的名字写上去, 谢谢!

相关文章
|
网络协议 安全 网络安全
WireGuard 系列文章(六):Netmaker 安装
WireGuard 系列文章(六):Netmaker 安装
|
数据中心
Zerotier常用命令整理
Zerotier一款可以让您随时随地轻松连接云,移动,桌面和数据中心资源的工具。通过Zerotier可以轻松地将你的多台设备建立局域网,互联互通。本文主要整理Zerotier在日常使用中的命令,以备日常使用查询。
20013 1
Zerotier常用命令整理
|
Web App开发 域名解析 缓存
如何在 Ubuntu 20.04 上安装 Node.js 和 npm
本文我们主要为大家介绍在 Ubuntu 20.04 上安装 Node.js 和 npm 的三种不同的方式。
164013 7
如何在 Ubuntu 20.04 上安装 Node.js 和 npm
|
网络协议 物联网 Linux
WireGuard 系列文章(七):使用 WireGuard 和 Netmaker 创建 Full Mesh 网络
WireGuard 系列文章(七):使用 WireGuard 和 Netmaker 创建 Full Mesh 网络
Debian 系统 重启后iptables规则无法保存或者丢失
Debian 系统 重启后iptables规则无法保存或者丢失
1780 0
|
5天前
|
数据采集 人工智能 自然语言处理
3分钟采集134篇AI文章!深度解析如何通过云无影AgentBay实现25倍并发 + LlamaIndex智能推荐
结合阿里云无影 AgentBay 云端并发采集与 LlamaIndex 智能分析,3分钟高效抓取134篇 AI Agent 文章,实现 AI 推荐、智能问答与知识沉淀,打造从数据获取到价值提炼的完整闭环。
390 93
|
6天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~