博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CSS布局--垂直水平居中
阅读量:4687 次
发布时间:2019-06-09

本文共 988 字,大约阅读时间需要 3 分钟。

···设置两个盒子

<div class="parent">

  <div class="child">
  </div>
</div>

 

方法一:absolute

<!-- //父元素相对定位,子元素绝对定位 -->

<!-- //绝对定位盒子模型有个特点:left + right + width + padding + margin = 包含块的宽度;此时可以将left、right设置为0;padding、margin未设置是默认为0;所以将margin设置为auto,使得元素自动居中; -->

<style type="text/css">

*{
  margin: 0;
  padding: 0;
}
.parent{
  position: relative;
  width: 200px;
  height: 200px;
  border: 1px solid black;
}
.child{
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
  width: 100px;
  height: 100px;
  background: pink;
}
</style>

 

方法二:absolute + 负margin

.parent{  
  position: relative;
  width: 200px;
  height: 200px;
  border: 1px solid black; 
}
.child{
  position: absolute;
  left: 50%; top: 50%;
  margin-left: -50px;
  margin-top: -50px;
  width: 100px;
  height: 100px;
  background: pink;
}
方法三:flex布局
  //只需设置父节点属性,无需设置子元素
  // 父元素设置
  display: flex;
  justify-content:center;
  align-items:Center;
 

 

转载于:https://www.cnblogs.com/veraNotes/p/10885636.html

你可能感兴趣的文章
(转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
查看>>
Java并发编程
查看>>
[转]MySQL数据库管理常用命令
查看>>
Git Stash用法
查看>>
Android 读取文件内容
查看>>
sql server 2008学习8 sql server存储和索引结构
查看>>
Jquery radio选中
查看>>
《Visual C++ 2010入门教程》系列二:安装、配置和首次使用VS2010
查看>>
Best Time to Buy and Sell Stock with Cooldown_LeetCode
查看>>
postgressql数据库中limit offset使用
查看>>
测试思想-集成测试 关于接口测试 Part 2
查看>>
windows下mysql密码忘了怎么办?【转】
查看>>
java文件上传和下载
查看>>
SQL联合查询(内联、左联、右联、全联)的语法(转)
查看>>
枚举和实用类
查看>>
python基础知识第二篇(字符串)
查看>>
php生成器使用总结
查看>>
T-SQL中的indexof函数
查看>>
javascript基础之数组(Array)对象
查看>>
mysql DML DDL DCL
查看>>