058-svg-viewbox-fill.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>058 SVG viewBox and fill</title>
  6. <style>
  7. body { background: #ffffff; color: #202020; font-family: sans-serif;
  8. margin: 12px; font-size: 18px; }
  9. .icon { width: 60px; height: 60px; background: #eeeeee; }
  10. .wide { width: 150px; height: 30px; background: #eeeeee; margin-top: 8px; }
  11. #result { background: #ffc8c8; color: #000000; padding: 8px;
  12. margin-top: 12px; }
  13. </style>
  14. </head>
  15. <body>
  16. <h1>058 &mdash; SVG viewBox and fill</h1>
  17. <p>Fractional viewBox numbers must not truncate the height, and
  18. <code>width="100%"</code>/<code>fill</code> on a child must fill the box.</p>
  19. <div class="icon">
  20. <svg id="svg1" viewBox="0 0 340.16 340.16">
  21. <rect id="rect1" width="100%" height="100%" fill="#005AB4"></rect>
  22. </svg>
  23. </div>
  24. <div class="wide">
  25. <svg id="svg2" viewBox="0 0 692 140">
  26. <rect id="rect2" width="100%" height="100%" fill="#B00020"></rect>
  27. </svg>
  28. </div>
  29. <p><span id="c1" style="color:#005AB4">blue</span>
  30. <span id="c2" style="color:#B00020">red</span></p>
  31. <div id="result">checking...</div>
  32. <script>
  33. setTimeout(function () {
  34. function R(id) { return document.getElementById(id).getBoundingClientRect(); }
  35. /* SVG paint is `fill`, never the element's background. */
  36. function B(id) { return getComputedStyle(document.getElementById(id)).fill; }
  37. var s1 = R('svg1'), s2 = R('svg2');
  38. var r1 = B('rect1'), r2 = B('rect2');
  39. var ok = Math.abs(s1.width - 60) <= 2 &&
  40. Math.abs(s1.height - 60) <= 2 &&
  41. Math.abs(s2.width - 150) <= 2 &&
  42. Math.abs(s2.height - 30) <= 2 &&
  43. r1 === getComputedStyle(document.getElementById('c1')).color &&
  44. r2 === getComputedStyle(document.getElementById('c2')).color;
  45. var r = document.getElementById('result');
  46. if (r) {
  47. r.style.background = ok ? '#c8f0c8' : '#ffc8c8';
  48. r.textContent = (ok ? 'SVG-OK ' : 'SVG-FAIL ') +
  49. 'icon=' + Math.round(s1.width) + 'x' +
  50. Math.round(s1.height) +
  51. ' wide=' + Math.round(s2.width) + 'x' +
  52. Math.round(s2.height) +
  53. ' fills=' + r1 + '/' + r2;
  54. }
  55. }, 300);
  56. </script>
  57. </body>
  58. </html>