块级元素水平垂直居中方法

目录

一.脱离文档流

1.有没有宽高都可以

2.必须要有宽高

二.没有脱离文档流

1.有无宽高都可

2.有宽高


一.脱离文档流

只要是脱离文档流,都是使用的是父相子绝(父块相对定位,子块绝对定位),

有三种方式:


 
 
  1. 有无宽高都可
  2. 1.父相子绝 可以使用 margin :auto
  3. 2.父相子绝 transform :translatex
  4. 有宽高
  5. 3.父相子绝 margin-top 一半

1.有没有宽高都可以

下面两种方法都是需要父相子绝(父块相对定位,子块绝对定位),在子块中设置

方法一 margin:auto


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title> </title>
  6. <style type="text/css">
  7. .box1{
  8. width: 100%;
  9. height: 100%;
  10. border: 1px solid red;
  11. }
  12. .box2{
  13. width: 400px;
  14. height: 400px;
  15. background-color:skyblue;
  16. position: relative;
  17. }
  18. .box3{
  19. width: 100px; /*1.width:90%也可以 2.不设置宽也可以*/
  20. height: 100px; /*1.height:90%也可以 2.不设置高也可以*/
  21. background-color: gray;
  22. position: absolute;
  23. top: 0;
  24. right: 0;
  25. bottom: 0;
  26. left: 0;
  27. margin:auto;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div class="box1">
  33. <div class="box2">div2 <div class="box3">div3 </div> </div>
  34. <div>
  35. </body>
  36. </html>

方法二 transform:translate:


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title> </title>
  6. <style type="text/css">
  7. .box1{
  8. width: 100%;
  9. height: 100%;
  10. border: 1px solid red;
  11. }
  12. .box2{
  13. width: 400px;
  14. height: 400px;
  15. background-color:skyblue;
  16. position: relative;
  17. }
  18. .box3{
  19. width: 100px; /*width:90%也可以 但 margin-top margin-left 也需要改成百分形式*/
  20. height: 100px; /*height:90%也可以 但 margin-top margin-left 也需要改成百分形式*/
  21. background-color: gray;
  22. position: absolute;
  23. top: 50%;
  24. left: 50%;
  25. transform: translate(-50%,-50%);
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div class="box1">
  31. <div class="box2">div2 <div class="box3">div3 </div> </div>
  32. <div>
  33. </body>
  34. </html>

2.必须要有宽高

方法一 margin-top margin-left


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title> </title>
  6. <style type="text/css">
  7. .box1{
  8. width: 100%;
  9. height: 100%;
  10. border: 1px solid red;
  11. }
  12. .box2{
  13. width: 400px;
  14. height: 400px;
  15. background-color:skyblue;
  16. position: relative;
  17. }
  18. .box3{
  19. width: 100px; /*width:90%也可以 但 margin-top margin-left 也需要改成百分形式*/
  20. height: 100px; /*height:90%也可以 但 margin-top margin-left 也需要改成百分形式*/
  21. background-color: gray;
  22. position: absolute;
  23. top: 50%;
  24. left: 50%;
  25. margin-top: - 50px;
  26. margin-left: - 50px;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div class="box1">
  32. <div class="box2">div2 <div class="box3">div3 </div> </div>
  33. <div>
  34. </body>
  35. </html>

二.没有脱离文档流

有3种方法


 
 
  1. 有无宽高都可:
  2. 1.(父)
  3. display :flex; /*将其定义为弹性容器*/
  4. align-items: center; /*垂直居中对齐*/
  5. justify-content: center; /*水平居中对齐*/
  6. 2.
  7. (父) display :flex
  8. (子) margin :auto
  9. 有宽高
  10. 1.
  11. (父) display :table-cell;
  12. vertical-align: middle;(垂直居中)
  13. (子) margin :0 auto(水平居中)

1.有无宽高都可

方法一:


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title> </title>
  6. <style type="text/css">
  7. .box1{
  8. width: 100%;
  9. height: 100%;
  10. border: 1px solid red;
  11. }
  12. .box2{
  13. width: 400px;
  14. height: 400px;
  15. background-color:skyblue;
  16. display:flex; /*将其定义为弹性容器*/
  17. align-items: center; /*垂直居中对齐*/
  18. justify-content: center; /*水平居中对齐*/
  19. }
  20. .box3{
  21. /* 宽高可以不设置
  22. width:100px;
  23. height:100px;*/
  24. background-color: gray;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div class="box1">
  30. <div class="box2"> <div class="box3">div3 </div> </div>
  31. <div>
  32. </body>
  33. </html>

方法二:


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title> </title>
  6. <style type="text/css">
  7. .box1{
  8. width: 100%;
  9. height: 100%;
  10. border: 1px solid red;
  11. }
  12. .box2{
  13. width: 400px;
  14. height: 400px;
  15. background-color:skyblue;
  16. position: relative;
  17. display:flex; /*最新特性*/
  18. }
  19. .box3{
  20. /*有无宽高都可以*/
  21. /*width:100px;
  22. height:100px;*/
  23. background-color: gray;
  24. margin:auto;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div class="box1">
  30. <div class="box2"> <div class="box3">div3 </div> </div>
  31. <div>
  32. </body>
  33. </html>

2.有宽高


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title> </title>
  6. <style type="text/css">
  7. .box1{
  8. width: 100%;
  9. height: 100%;
  10. border: 1px solid red;
  11. }
  12. .box2{
  13. width: 400px;
  14. height: 400px;
  15. background-color:skyblue;
  16. /*父块设置垂直居中*/
  17. display: table-cell;
  18. vertical-align: middle;
  19. }
  20. .box3{
  21. width: 100px;
  22. height: 100px;
  23. background-color: gray;
  24. margin: 0 auto; /*子块设置水平居中*/
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div class="box1">
  30. <div class="box2">div2 <div class="box3">div3 </div> </div>
  31. <div>
  32. </body>
  33. </html>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值