day008布局题:div垂直居中,左右10px,高度始终为宽度一半

day008布局题:div垂直居中,左右10px,高度始终为宽度一半

问题描述: 实现一个div垂直居中, 其距离屏幕左右两边各10px, 其高度始终是宽度的50%。同时div中有一个文字A,文字需要水平垂直居中。

思路一:利用height:0; padding-bottom: 50%;


   
   
  1. <!DOCTYPE html>

  2. <html lang="en">

  3. <head>

  4. <meta charset="UTF-8">

  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">

  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">

  7. <title>Document</title>

  8. <style>

  9. *{

  10. margin: 0;

  11. padding: 0;

  12. }

  13. html, body {

  14. height: 100%;

  15. width: 100%;

  16. }

  17. .outer_wrapper {

  18. margin: 0 10px;

  19. height: 100%;

  20. /* flex布局让块垂直居中 */

  21. display: flex;

  22. align-items: center;

  23. }

  24. .inner_wrapper{

  25. background: red;

  26. position: relative;

  27. width: 100%;

  28. height: 0;

  29. padding-bottom: 50%;

  30. }

  31. .box{

  32. position: absolute;

  33. width: 100%;

  34. height: 100%;

  35. display: flex;

  36. justify-content: center;

  37. align-items: center;

  38. font-size: 20px;

  39. }

  40. </style>

  41. </head>

  42. <body>

  43. <div class="outer_wrapper">

  44. <div class="inner_wrapper">

  45. <div class="box">A</div>

  46. </div>

  47. </div>

  48. </body>

  49. </html>

强调两点:

  1. padding-bottom究竟是相对于谁的?

答案是相对于 父元素的width值

那么对于这个outwrapper的用意就很好理解了。CSS呈流式布局,div默认宽度填满,即100%大小,给outwrapper设置margin: 0 10px;相当于让左右分别减少了10px。

  1. 父元素相对定位,那绝对定位下的子元素宽高若设为百分比,是相对谁而言的?

相对于父元素的(content + padding)值, 注意不含border

延伸:如果子元素不是绝对定位,那宽高设为百分比是相对于父元素的宽高,标准盒模型下是content, IE盒模型是content+padding+border。

思路二: 利用calc和vw


   
   
  1. <!DOCTYPE html>

  2. <html lang="en">

  3. <head>

  4. <meta charset="UTF-8">

  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">

  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">

  7. <title>Document</title>

  8. <style>

  9. * {

  10. padding: 0;

  11. margin: 0;

  12. }

  13. html,

  14. body {

  15. width: 100%;

  16. height: 100%;

  17. }

  18. .wrapper {

  19. position: relative;

  20. width: 100%;

  21. height: 100%;

  22. }

  23. .box {

  24. margin-left: 10px;

  25. /* vw是视口的宽度, 1vw代表1%的视口宽度 */

  26. width: calc(100vw - 20px);

  27. /* 宽度的一半 */

  28. height: calc(50vw - 10px);

  29. position: absolute;

  30. background: red;

  31. /* 下面两行让块垂直居中 */

  32. top: 50%;

  33. transform: translateY(-50%);

  34. display: flex;

  35. align-items: center;

  36. justify-content: center;

  37. font-size: 20px;

  38. }

  39. </style>

  40. </head>

  41. <body>

  42. <div class="wrapper">

  43. <div class="box">A</div>

  44. </div>

  45. </body>

  46. </html>

效果如下:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值