实现div中的div水平垂直居中

实现div中的div水平垂直居中方法一利用 transform 实现 div 居中 要先设置定位 css outer width 500px height 200px background color green position relative inner width 20px height 10px background colo

方法一

.outer { 
    width: 500px; height: 200px; background-color: green; position: relative; } .inner { 
    width: 20px; height: 10px; background-color: wheat; position: absolute; } .center { 
    left: 50 %; top: 50 %; transform: translate(- 50 %, - 50 %);/*自己的50% */ } 

html

<div class="outer"> <div class="inner center"></div> </div> 

方法二

利用 margin: auto;要先设置定位

css

.outer { 
    width: 500px; height: 200px; background-color: green; position: relative; } .inner { 
    width: 20px; height: 10px; background-color: wheat; position: absolute; } .center { 
    left: 0; top: 0; right: 0; bottom: 0; margin: auto; } 

html

<div class="outer"> <div class="inner center"></div> </div> 

方法三

.flex{ 
    display:flex; align-items: center; justify-content:center; width:500px; height:500px; border:10px solid; } .flex_son{ 
    width:100px; height:100px; border:10px solid; } 

html

<div class='flex'> <div class="flex_son"></div> </div> 

方法四

div绝对定位水平垂直居中【margin 负间距】 这或许是当前最流行的使用方法。

 .outer { 
    width: 500px; height: 500px; position: relative; background:red; } .center{ 
    width:200px; height: 200px; background:green; position: absolute; left:50%; top:50%; margin-left:-100px;//要宽度的一半 margin-top:-100px; } <div class="outer"> <div class="center"></div> </div> 

方法五

.Aa{ 
    display: table-cell; text-align:center; vertical-align: middle; width: 500px; height: 500px; background:green; } .aa{ 
    display: inline-block; background:red; width: 100px; height: 100px; } <div class="Aa"> <div class="aa"></div> </div> 

方法六

利用父基线跟伪元素:before搭配vertical-align:middle实现对齐

<style type="text/css"> .parent { 
    display: inline-block; width: 300px; height: 300px; font-size: 0; background: #; text-align: center; } .parent:before { 
    display: inline-block; width: 20px; height: 100%; content: ''; background: #; vertical-align: middle; } .child { 
    display: inline-block; width: 50px; height: 50px; background: #19be6b; vertical-align: middle; } </style> <div class="parent"> <div class="child">child</div> </div> 

方法(窗口居中)

#aa { 
    width: 50vw; height: 50vh; margin: 25vh auto; /*margin: 25vh 25vw; */ } <div id="aa"></div> 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/221950.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午4:54
下一篇 2026年3月17日 下午4:55


相关推荐

  • PDF 补丁丁 0.5 正式版发布

    PDF 补丁丁 0.5 正式版发布经过了两年的测试 新版本的 PDF 补丁丁已经比较稳定了 在农历新年前发布这个 0 5 版 作为正式稳定版吧 新的 PDF 补丁丁比旧的 0 3 版增加了许多功能 PDF 可视化编辑文档书签 可从带页码的目录文件 马健的 PDF 书签文件中复制文本粘贴到书签编辑器导入书签 界面的调整 增加了首页列出程序的所有功能 增加了工具栏和菜单栏 修改文档功能可嵌入字体和替换字体功能

    2026年3月18日
    2
  • Excel地址 C语言

    Excel地址 C语言Excel 单元格的地址表示很有趣 它使用字母来表示列号 比如 A 表示第 1 列 B 表示第 2 列 Z 表示第 26 列 AA 表示第 27 列 AB 表示第 28 列 BA 表示第 53 列 当然 Excel 的最大列号是有限度的 所以转换起来不难 如果我们想把这种表示法一般化 可以把很大的数字转换为很长的字母序列呢 本题目既是要求对输入的数字 输出其对应的 Excel 地址表示方式 例如 输入 26 则

    2026年3月26日
    2
  • Mysql慢查询_mysql服务启动慢

    Mysql慢查询_mysql服务启动慢一、开启慢查询日志1、查看是否开启:showvariableslike’slow_query_log’;2、查看是否开启记录未使用索引的查询:showvariableslike’log_queries_not_using_indexes’;3、开启未使用索引的查询日志:setgloballog_queries_not_using_indexes=on;4、查看超过多长时间的语句才会…

    2022年10月14日
    2
  • release和snapshots

    release和snapshots文章目录前言一 Snapshot 和 Release 版本的区别 二 什么时候用 Snapshot 版本 三 稳定版使用 Snapshot 版本行不行 前言 repository 标签里面的 release 和 snapshots 一 Snapshot 和 Release 版本的区别 Snapshot 版本代表不稳定 尚处于开发中的版本 快照版本 Release 版本则代表稳定的版本 发行版本 二 什么时候用 Snapshot 版本 依赖库中的 jar 正处于开发的阶段 会被经常被更新 这种情况下 如果使用 Release 会频繁增加

    2026年3月18日
    2
  • pyCharm打开已有的ipynb文件

    pyCharm打开已有的ipynb文件先安装 python 和 pyCharm 创建一个 jupyternoteb 项目将文件复制到该项目里面会显示安装 jyputer 点击安装 可在 cmd 中输入 jyputernoteb 查看 如果很久没有

    2026年3月27日
    2
  • k8s批量删除Evicted Pods「建议收藏」

    k8s批量删除Evicted Pods「建议收藏」1、查询pod状态[root@k8s_masterprom]#kubectl-nkube-systemgetpods|grepEvictedcoredns-67cb7b5f46-2crxg0/1Evicted04h56mcoredns-67cb7b5f46-2ds7n0/1…

    2022年5月17日
    644

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号