058-svg-viewbox-fill.html 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. <div id="result">checking...</div>
  30. <script>
  31. setTimeout(function () {
  32. function R(id) { return document.getElementById(id).getBoundingClientRect(); }
  33. function B(id) { return getComputedStyle(document.getElementById(id)).backgroundColor; }
  34. var s1 = R('svg1'), s2 = R('svg2');
  35. var r1 = B('rect1'), r2 = B('rect2');
  36. var ok = Math.abs(s1.width - 60) <= 2 &&
  37. Math.abs(s1.height - 60) <= 2 &&
  38. Math.abs(s2.width - 150) <= 2 &&
  39. Math.abs(s2.height - 30) <= 2 &&
  40. r1 === 'rgb(0, 90, 189)' &&
  41. r2 === 'rgb(176, 0, 32)';
  42. var r = document.getElementById('result');
  43. if (r) {
  44. r.style.background = ok ? '#c8f0c8' : '#ffc8c8';
  45. r.textContent = (ok ? 'SVG-OK ' : 'SVG-FAIL ') +
  46. 'icon=' + Math.round(s1.width) + 'x' +
  47. Math.round(s1.height) +
  48. ' wide=' + Math.round(s2.width) + 'x' +
  49. Math.round(s2.height) +
  50. ' fills=' + r1 + '/' + r2;
  51. }
  52. }, 300);
  53. </script>
  54. </body>
  55. </html>