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

css如何使一个盒子水平垂直居中

方法一:利用定位(常用方法,推荐)

<style>
.parent{width: 500px;height: 500px;border: 1px solid #000;position:relative;
}.child {width: 100px;height: 100px;border: 1px solid #999;position:absolute;top: 50%;left: 50%;margin-top: -50px;margin-left: -50px;
}
</style>

方法二:利用 margin:auto;

<style>.parent{width: 500px;height: 500px;border: 1px solid #000;position:relative;}.child {width: 100px;height: 100px;border: 1px solid #999;position:absolute;margin: auto;top: 0;left: 0;right: 0;bottom: 0;}
</style>

方法三:利用 display:flex;设置垂直水平都居中

<style>.parent{width: 500px;height: 500px;border: 1px solid #000;display: flex;justify-content: center;align-items: center;}.child {width: 100px;height: 100px;border: 1px solid #999;}
</style>

方法四:利用 transform

<style>.parent{width: 500px;height: 500px;border: 1px solid #000;position:relative;}.child {width: 100px;height: 100px;border: 1px solid #999;position:absolute;top: 50%;left: 50%;transform:translate(-50%, -50%);}
</style>


http://www.mrgr.cn/news/775.html

相关文章:

  • 鸿蒙关于可以实现滚动效果的容器组件的相关知识
  • 《广东省消费品召回管理办法》所称的消费品缺陷,是指因设计、制造、警示等原因,致使同一批次、型号或者类别的消费品中___的危及人身、财产安全的不合理危险。()
  • 和等于 k 的最长子数组长度(LeetCode)
  • SpringMVC
  • 二、开发环境和第一个OpenTK程序
  • Mininet应用实践
  • RedisTempate序列化的json字符串,如何反序列化
  • 【SpringBoot】SpringBoot中的异常处理和异常跳转页面
  • 【Python爬虫】技术深度探索与实践
  • C#学习总结
  • SQL基础——MySQL的索引
  • 这是啥设计模式-组合模式
  • Mysql-约束
  • Unity的UI设计
  • 使用旧笔记本电脑搭建私人NAS
  • JVM -垃圾回收器
  • vue 后台管理 指定项目别名
  • [LitCTF 2024]浏览器也能套娃?
  • CentOS7安装Docker教程(含最新镜像地址)
  • 国内无法更新linux,无法使用sudo yum update