041-calc-boxes.html 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>041 calc</title>
  6. <style>
  7. html, body { margin: 0; padding: 0; background: #ffffff; color: #202020; }
  8. body { font-family: sans-serif; font-size: 13px; padding: 10px; }
  9. h1 { font-size: 20px; margin: 0 0 6px 0; }
  10. p { margin: 0 0 10px 0; }
  11. .pane { float: left; width: 440px; margin-left: 10px; }
  12. .label { background: #eeeeee; padding: 4px 6px; margin-bottom: 4px; }
  13. .stage { width: 300px; background: #f4f4f4; border: 1px solid #999999; padding: 6px; }
  14. .box { background: #cc3333; color: #ffffff; margin-bottom: 8px; }
  15. .box1 { margin-left: calc(10% + 8px); padding: calc(5% + 4px);
  16. height: calc(50% + 20px); }
  17. .ref1 { margin-left: 38px; padding: 19px; height: 170px; }
  18. .box2 { height: 10px; min-height: calc(20% + 10px); }
  19. .ref2 { height: 70px; }
  20. .box3 { max-width: calc(50% + 20px); height: 24px; }
  21. .ref3 { width: 170px; height: 24px; }
  22. .box4 { width: 50px; min-width: calc(30% + 10px); height: 24px; }
  23. .ref4 { width: 100px; height: 24px; }
  24. .box5 { height: 200px; max-height: calc(10% + 20px); }
  25. .ref5 { height: 50px; }
  26. #result { clear: both; margin-top: 14px; padding: 8px; background: #eeeeee;
  27. color: #000000; font-family: monospace; font-weight: bold; }
  28. </style>
  29. </head>
  30. <body>
  31. <h1>041 &middot; calc in box metrics</h1>
  32. <p>Each A box must have the same box rectangle as its fixed-pixel B reference.</p>
  33. <div class="pane">
  34. <div class="label">A &middot; calc()</div>
  35. <div class="stage" id="sa">
  36. <div class="box box1" id="a1">margin / padding / height</div>
  37. <div class="box box2" id="a2"></div>
  38. <div class="box box3" id="a3"></div>
  39. <div class="box box4" id="a4"></div>
  40. <div class="box box5" id="a5"></div>
  41. </div>
  42. </div>
  43. <div class="pane">
  44. <div class="label">B &middot; reference</div>
  45. <div class="stage" id="sb">
  46. <div class="box ref1" id="b1">margin / padding / height</div>
  47. <div class="box ref2" id="b2"></div>
  48. <div class="box ref3" id="b3"></div>
  49. <div class="box ref4" id="b4"></div>
  50. <div class="box ref5" id="b5"></div>
  51. </div>
  52. </div>
  53. <div id="result">checking...</div>
  54. <script>
  55. setTimeout(function () {
  56. var sa = document.getElementById('sa').getBoundingClientRect();
  57. var sb = document.getElementById('sb').getBoundingClientRect();
  58. var ok = true, details = [];
  59. for (var i = 1; i <= 5; i++) {
  60. var a = document.getElementById('a' + i).getBoundingClientRect();
  61. var b = document.getElementById('b' + i).getBoundingClientRect();
  62. var aw = Math.round(a.width), bw = Math.round(b.width);
  63. var ah = Math.round(a.height), bh = Math.round(b.height);
  64. var ax = Math.round(a.left - sa.left), bx = Math.round(b.left - sb.left);
  65. if (Math.abs(aw - bw) > 1 || Math.abs(ah - bh) > 1 ||
  66. Math.abs(ax - bx) > 1) {
  67. ok = false;
  68. details.push(i + ': A=' + aw + 'x' + ah + '@' + ax +
  69. ' B=' + bw + 'x' + bh + '@' + bx);
  70. }
  71. }
  72. var out = document.getElementById('result');
  73. out.textContent = ok ? 'PASS: calc margins, padding and box sizes' :
  74. ('FAIL: ' + details.join('; '));
  75. out.style.background = ok ? '#ccffcc' : '#ffcccc';
  76. }, 600);
  77. </script>
  78. </body>
  79. </html>