方法:定位,外边距,内边距,层级,边框;
一个元素;
两个元素;
三个元素.
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="index.css">
</head>
<body>
  <div class="father1">
    <div class="son1"></div>
  </div>

方法1

  <div class="father2">
    <div class="son2"></div>
  </div>

方法2

  <div class="father3">
    <div class="son3"></div>
  </div>

方法3

  <div class="father4">
    <div class="son4"></div>
  </div>

方法4

  <div class="father5">
    <div class="son5"></div>
  </div>
  <!-- 前五种为两个元素的居中方式 -->

方法5

  <div class="six"></div>

方法6

  <div class="seven"></div>
  <!-- 后两种为1个元素的居中方式 -->

方法7

  <div class="father8">
    <div class="box1"></div>
    <div class="box2"></div>
  </div>
</div>
  </div>
  <!-- 最后一种为3个元素的居中方式 -->
方法8
</body>
</html>
 
* {
  margin: 0;
  padding: 0;
  list-style: none;
}
清除浏览器默认样式
.father1 {
  width: 400px;
  height: 400px;
  background-color: #fc4;
  position: relative;
}
.son1 {
  width: 200px;
  height: 200px;
  background-color: #f44;
  position: absolute;
  top: 100px;
  left: 100px;
}
/* 第一种居中方式 */
定位
.father2 {
  width: 200px;
  height: 200px;
  background-color: #f77;
  position: relative;
}
.son2 {
  width: 100px;
  height: 100px;
  background-color: #ff7;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -50px;
  margin-top: -50px;
}
/* 第二种居中方式 */
定位+外边距+百分比
.father3 {
  width: 200px;
  height: 200px;
  background-color: #0f0;
  position: relative;
  /* overflow: hidden; */
  /* 解决margin塌陷问题 */
}
.son3 {
  width: 100px;
  height: 100px;
  background-color: #77f;
  position: absolute;
  margin-left: 50px;
  margin-top: 50px;
}
/* 第三种居中方式 */
定位+外边距
.father4 {
  width: 100px;
  height: 100px;
  padding: 50px;
  background-color: #f44;
}
.son4 {
  width: 100px;
  height: 100px;
  background-color: #0f7;
}
/* 第四种居中方式 */
内边距
.father5 {
  width: 100px;
  height: 100px;
  border: 50px solid #f444;
  background-color: #f444;
}
.son5 {
  width: 100px;
  height: 100px;
  background-color: #4fff;
}
/* 第五种居中方式 */
边框
.six {
  width: 200px;
  height: 200px;
  background-color: #f9f67f;
  border: 100px solid #08c;
}
/* 第六种居中方式 */
一个元素 边框
.seven {
  width: 0px;
  height: 0px;
  padding: 100px;
  background-color: #f78;
  border: 50px solid #99f4;
}
/* 第七种居中方式 */
一个元素 边框+内边距
.box1 {
  width: 200px;
  height: 200px;
  background-color: #f075;
  position: absolute;
  z-index: 1;
}
.box2 {
  width: 100px;
  height: 100px;
  background-color: #ff99;
  position: absolute;
  z-index: 2;
  top: 50px;
  left: 50px;
}
.father8 {
  position: relative;
}
/* 第八种居中方式 */
3个元素 定位+层级

最新文章

  1. [翻译]开发文档:android Bitmap的高效使用
  2. JavaScript中,本周、上周、本月、上月日期判断
  3. MySQL如何查询两个日期之间的记录
  4. BAT批处理(一)
  5. UVa 10817 Headmaster&#39;s Headache (状压DP+记忆化搜索)
  6. C# Winform C/S系统快速开发平台(源码+原创)
  7. java_reflect_03
  8. 使用rsync+inotify-tools+ssh实现文件实时同步
  9. pygame 弹力球及其变速的实现
  10. JavaScript函数之作用域 / 作用链域 / 预解析
  11. 微信开发获取media_id错误码汇总
  12. Python逻辑运算符
  13. hdu 5398 动态树LCT
  14. ViewPager实现滑屏切换页面及动画效果(仿优酷客户端)
  15. 自己动手写web框架----1
  16. C语言之回文数算法
  17. logstash配置多入多出并互相隔离
  18. 关于 java.toString() ,(String),String.valueOf的区别
  19. 简单备份mysql数据库
  20. JAVA中的责任链模式(CH01)

热门文章

  1. Solution -「原创」Destiny
  2. C语言课堂--现代编译环境搭建[2020年7月]
  3. 使用centos linux vps搭建jupyter notebook踩坑日记
  4. YII类的映射表机制
  5. 使用 Liquibase 管理数据库版本 - SpringBoot 2.7 .2 实战基础
  6. 重构、插件化、性能提升 20 倍,Apache DolphinScheduler 2.0 alpha 发布亮点太多!
  7. Spring(一)- 初始 + DI+scope
  8. CF600E Lomsat gelral (dfs序+莫队)
  9. 定时器控制单只LED灯
  10. React报错之Rendered more hooks than during the previous render