大家好,我是新来的程序猿小清,在我们前端开发中,经常会让元素水平垂直居中的,现在我来给大家带来以下几种水平垂直居中的方法,如有不如,请谅解

<body><div class = "parent"><div class="son"><div></div>
</body>
  <style>/* 第一种  使用定位家margin-top、margin-left*/.parent{position:relative;width:300px;height:300px;border:1px solid #f00;}.parent .son{/* 通过父相子绝,来实现元素水平居中 top/left: 百分之五十, margin-top/margin-left 各减去高宽一半*/position:absolute;top:50%;left:50%;margin-top:-50px;margin-left:-50px;width:100px;height:100px;background-color:#0f0;}/* 第二种定位加 转换属性transform */.parent{position:relative;width:300px;height:300px;border:1px solid #f00;}.parent .son{/* 通过父相子绝,来实现元素水平居中 top/left: 百分之五十transform:translate(-50%,-50%)  向左移动当前元素宽度的一半。向上移动当前元素高度的一半*/position:absolute;top:50%;left:50%;width:100px;height:100px;transform:translate(-50%,-50%)background-color:#0f0;}/* 第三种定位 加margin:auto */.parent{position:relative;width:300px;height:300px;border:1px solid #f00;}.parent .son{/* 通过父相子绝,来实现元素水平居中 top/right/bottom/left: 0margin:auto*/position:absolute;top:0;right:0;bottom:0;left:0;margin:auto;width:100px;height:100px;background-color:#0f0;}/* 第四种flex布局 + margin:auto */.parent{display:flex;width:300px;height:300px;border:1px solid #f00;}.parent .son{/* margin:auto*/margin:auto;width:100px;height:100px;background-color:#0f0;}/* 第五种flex布局 + justift-content:center/  align-items:center*/.parent{display:flex;justify-content:center;align-items:center;width:300px;height:300px;border:1px solid #f00;}.parent .son{width:100px;height:100px;background-color:#0f0;}/* 第六种 使用grid布局 */.parent{display:grid;width:300px;height:300px;border:1px solid #f00;}.parent .son{width:100px;height:100px;background-color:#0f0;justify-self:center;align-self:center}</style>

CSS如何让一个div水平垂直居中相关推荐

  1. 如何让一个div水平垂直居中

    在工作中 经常会碰到让一个div框针对某个模块水平垂直居中 针对这种情况 有多种方法 现在一一实现一下 一. div绝对定位水平垂直居中 margin 负间距 代码: .box {width: 200 ...

  2. 让一个 div 水平垂直居中的几种方法

    水平垂直居中有好多种实现方式,主要就分为两类:不定宽高和定宽高,以在body下插入一个div为例: 定宽高 使用定位+margin element.style {position: absolute; ...

  3. CSS实现子元素div水平垂直居中

    div基本布局 <div class="main"><div class="center"></div>

  4. 怎么让一个 div 水平垂直居中

    <div class="parent"><div class="child"></div> </div> 方法1 ...

  5. 前端面试系列-CSS基础-div水平垂直居中文本居中(单行文字、多行文字)

    文章目录 一.div水平垂直居中 1.flex 2.position (元素已知宽高) 3.position transform (元素未知宽高度) 4.position(元素已知宽度)maigin: ...

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

    如何让div水平垂直居中 @(css)[妙瞳] 引子 我们经常遇到需要把div中的内容进行水平和垂直居中.所以,这里介绍一种方法,可以使div水平居中和垂直居中. 代码: <!DOCTYPE h ...

  7. 关于DIV嵌套(二):div嵌套div水平垂直居中

    div嵌套div水平垂直居中可以使用position定位来实现,这是最常用的方法. <!DOCTYPE html> <html><head><meta cha ...

  8. 让div水平垂直居中的六种方法

    ** 方法一:绝对定位方法:不确定当前div的宽度和高度,采用 transform: translate(-50%,-50%); 当前div的父级添加相对定位(position: relative;) ...

  9. div水平垂直居中的七种方法

    学习笔记(一) div水平垂直居中的七种方法 文章目录 学习笔记(一) 前言 一.绝对定位法 1.方法一 2.方法二 3.方法三 4.方法四 二.flex布局法 1.方法五 三.将小div转成行内块 ...

最新文章

  1. 计算机导论第一章试题及答案,计算机导论第一章测试题
  2. PHP与base64
  3. win32按钮按下不弹起_为什么厨房插座都不装五孔插座了?听电工一说,懊悔没早知道...
  4. animate动画案例_animate动画案例——小小购物狂
  5. 使用Predicate操作Collection集合
  6. JS 基础知识点及常考面试题(一)
  7. (转)RabbitMQ学习之安装
  8. 安卓逆向系列教程 4.6 去广告
  9. 以前的(山寨机)手机是怎么逐渐消失的?
  10. fastjson 版本_以快标榜的fastjson,快没快多少,倒是漏洞一大堆
  11. ssms 缺少索引信息_SQLServer索引重建
  12. 掌财社:一边亏钱一边被骂,爱奇艺需要出路
  13. 飞冰 - ICE Design Pro 使用指南
  14. vb发送到文件服务器,VB实现文件上传到FTP服务器
  15. win11系统用户账户控制总是弹出来?
  16. 【爆炸总结】NOIp2017赛(Bao)后(Zha)总结
  17. 数据库SQL:数据更新
  18. KingabseES例程-函数和过程的 INVOKER 与 DEFINER
  19. 建设智慧政务 浪潮网络助力昆明打造政务云平台
  20. 深受java开发者喜爱的顶级Java IDE

热门文章

  1. 2万5千字大厂面经 | 掘金技术征文
  2. deeplabv3+——训练自己的数据集 torch1.12.0 cuda11.3
  3. Python从小白到新手
  4. 海尔燃气热水器经常不能点火的简单维修
  5. 2019爪哇部落第十届新生选拔赛 题解
  6. 忙里偷闲( ˇˍˇ )闲里偷学【C语言篇】——(5)有趣的指针
  7. YouTube 十岁了,这十年里视频网站改变了什么?
  8. vert.x中future的简单使用
  9. 基于abp vnext手动编写一个功能的完整步骤
  10. carbon php 天数相差,使用 Carbon 计算时间差