057-flex-outer-width.html 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>057 flex outer widths</title>
  6. <style>
  7. body { background: #ffffff; color: #202020; font-family: sans-serif;
  8. margin: 12px; font-size: 18px; }
  9. #row { display: flex; width: 600px; height: 80px; background: #eeeeee; }
  10. #a { width: 100px; margin-left: 20px; margin-right: 10px; padding: 5px;
  11. background: #ffdddd; }
  12. #b { flex-grow: 1; background: #ddffdd; }
  13. #nowrap { display: flex; width: 600px; height: 30px; background: #eeeeff;
  14. margin-top: 12px; }
  15. #nowrap > div { width: 300px; height: 20px; background: #ddddff;
  16. margin-right: 10px; }
  17. #result { background: #ffc8c8; color: #000000; padding: 8px;
  18. margin-top: 12px; }
  19. </style>
  20. </head>
  21. <body>
  22. <h1>057 &mdash; flex outer widths and default nowrap</h1>
  23. <p>Explicit widths are content-box sizes: the flex line must include the
  24. item's margins, padding and borders. A flex row without
  25. <code>flex-wrap</code> must stay on one line.</p>
  26. <div id="row"><div id="a">A</div><div id="b">B</div></div>
  27. <div id="nowrap"><div>1</div><div>2</div><div>3</div></div>
  28. <div id="result">checking...</div>
  29. <script>
  30. setTimeout(function () {
  31. function R(id) { return document.getElementById(id).getBoundingClientRect(); }
  32. var a = R('a'), b = R('b');
  33. var n = document.getElementById('nowrap').children;
  34. var n0 = n[0].getBoundingClientRect();
  35. var n2 = n[2].getBoundingClientRect();
  36. /* In one nowrap row the third 300px item overflows past the first two
  37. (x > 500); if it wrapped it would start back at the container's left. */
  38. var same_row = n2.left > n0.left + 500;
  39. var ok = Math.abs(a.width - 110) <= 2 &&
  40. Math.abs(b.width - 460) <= 10 &&
  41. b.left > a.left + 100 &&
  42. same_row;
  43. var r = document.getElementById('result');
  44. if (r) {
  45. r.style.background = ok ? '#c8f0c8' : '#ffc8c8';
  46. r.textContent = (ok ? 'FLEX-OK ' : 'FLEX-FAIL ') +
  47. 'a=' + Math.round(a.left) + ',' + Math.round(a.width) +
  48. ' b=' + Math.round(b.left) + ',' + Math.round(b.width) +
  49. ' nowrap=' + same_row;
  50. }
  51. }, 300);
  52. </script>
  53. </body>
  54. </html>