SVG 妥协三角
SVG Triangle of Compromise

原始链接: https://me.micahrl.com/blog/svg-triangle-of-compromise/

7 月 29 日,作者最初的帖子中有关 SVG(可缩放矢量图形)使用的错误。 经过 Hacker News 和 Mastodon 的讨论后发现,SVG 提供了一种“不妥协”的解决方案,因为它们既能够使用 CSS 进行风格化,又能够进行缓存,以便在不同页面之间高效加载。 Scott Jehl 在交互式示例中演示了此功能。 就 SVG 的显示方式而言,主要有以下三种选择: 1. 使用 `` 标签 - 允许缓存,同时保留维度。 2. 在 `` 标签内直接定义 SVG - 通过 CSS 提供样式化功能,同时牺牲可缓存性,因为直接包含会阻止缓存。 3. 在“

HTML 缺乏轻松合并外部 HTML 文件或 SVG 图形的能力,而不会因缓存和加载时间差而导致性能问题。 为了解决这个问题,您可以创建“包含”,其中 HTML 文件的某些部分在渲染时被替换为单独文件中的内容。 此方法改进了缓存,并允许更简单地管理大型网站中的重复元素。 使用“包含”可以动态插入内容,例如用户偏好、评论或基于特定条件的附加数据。 这项技术增强了网页设计的灵活性,并为复杂的网站提供了各种好处。 使用“包含”的一个显着优点是提高了跨多个页面的缓存能力,从而加快了加载时间并提高了网站的整体性能。 通过对关键数据部分实施“包含”功能,开发人员可以提高编码实践的效率,减少花在冗余任务上的时间。 此外,利用“包含”的做法简化了将现有 HTML 文档转换为单一结构化框架的过程。 如果使用得当,该策略可以帮助简化大型网站的维护和开发流程。 在需要集成第三方资源或非标准格式的情况下,Tidy-HTML5 等工具可能会很有帮助。 这些工具使用户能够将 XML 处理应用于导入的网页内容,为进一步定制和优化提供增强的功能。 最终,选择以 XML 为中心的方法在前端开发方面比传统 HTML 具有优势,可以直接操作域模型。 利用 XSLT 转换有助于将原始数据无缝转换为精美的 HTML 显示,使设计人员和开发人员能够以最小的开销生成复杂的 Web 应用程序。 理想的场景是 API 端点和网页共享共同点,允许通过优化的模板化界面直接访问和显示基本信息。 操作模板和调整可视化组件可以反映对 CSS 属性的简单更改,让新手程序员可以轻松愉快地进行实验。 虽然 SVG 因其无损缩放和一致的外观而颇具吸引力,但在游戏或专业项目中广泛使用的实用性仍然值得怀疑。 出现的问题涉及嵌入文本和图形的可扩展性和易读性,需要频繁的微调和潜在的特定于平台的解决方案。 因此,建议的图像最大比例
相关文章

原文
This is a short update on 2024-07-29 after some discussion on Hacker News and Mastodon. It turns out that I was wrong about the original thrust of my post 😅. I will make an update, but it's not ready yet (I told my boss that I need to poast but he still wanted me to work today). The short version: SVGs with <use> tags offer a no compromise solution. Scott Jehl made a great interactive demo of this. Thanks to everyone on HN and to Scott for the feedback!
If you're reading this via the web feed (RSS), I recommend viewing it on the web instead, as I'm using <svg> elements as well as some feed-reader-unfriendly CSS and JavaScript in this post.
  • 2024-07-29: Add a note about pending updates and a link to Scott Jehl's <use> demo
  • 2024-07-25: Add list of properties to each subheading, show <use> tags

With SVGs on the web, you get to choose any two:

  • Stylable: can be colored with CSS (including foreground, background, :hover states, etc)
  • Cacheable: load once, use on other pages is free
  • Dimensional: has an intrinsic width and height

stylable cacheable dimensional <iframe> with inline <svg> tag <img> tag <svg> tag nothing 🙃

Referencing SVGs from an <img> element

  • stylable
  • cacheable
  • dimensional

An SVG referenced via an <img> tag works like any other image. If you load the same image on multiple pages, it will be cached and not re-downloaded. It can be used without specifying width and height to be displayed at its natural size, or with only one of the two specified for it to be displayed proportionally, or can be made to automatically fit its container. Other image types like PNG or JPEG files only support being referenced this way in the first place.

Displaying an SVG via an <svg> tag

  • stylable
  • cacheable
  • dimensional

An SVG defined via an <svg> can have its objects styled with CSS. The CSS properties that apply to SVGs are sometimes different than those that apply to HTML elements, like fill instead of color, but it's all CSS in the end, and you can use the same stylesheet to style SVG elements along with the rest of your page.

An SVG defined this way has another advantage: it can use the currentColor value for its fill and stroke properties, which means that it will automatically inherit the text finder color of its parent element.

The shapes support :hover states and other CSS pseudo-classes, too. In fact, they can have styling dynamically applied via JavaScript just like any other element! It's implemented in the diagram above, try hovering over any of the text labels.

Of course, placing the SVG inline to the HTML means it cannot be cached, any more than a <p> or a <div> is cached when it's repeated on multiple pages. This is almost never a problem with text, which tends to be very small and not be repeated on multiple pages, but might waste bandwidth if used for SVGs used often, like a logo or icon.

Displaying an <iframe> with an <svg> inside it

  • stylable
  • cacheable
  • dimensional

It's too bad that an inline <svg> cannot be cached, because it would be nice to be able to style logos and icons with CSS, and those are the things that are most likely to be repeated on multiple pages.

To solve this, we could create an HTML file that contains only the <svg> tag, and reference it on the site via an <iframe>. This solves the cachability problem, but introduces a new one: an <iframe> does not have an intrinsic width or height, at least as far as the browser can tell before rendering it.

You can hack around this dimensionality problem by measuring the <iframe> in JavaScript after it has loaded, although this requires JavaScript and causes a flash of unstyled content and an extra reflow. You could even measure it in advance and store its dimensions somehow, so that your site can set width and height on the <iframe> tag, depending on your requirements.

Apparently I think this is pretty neat, though, because I have referenced iframes a few times before, and I've run into the dimensionality problem with iframes in the past.

Other options

It's also worth mentioning a few other ways to reference SVGs:

  • You could create two SVG files, one for the normal state and one for the hover state, reference them via <img> tags, and swap them via CSS when the user hovers over the element. At least as of the time of this writing, this is what I do for my site logo in the header. This works well if all you need are the two states of hover and normal.
  • You could use the <object> tag, which is like an <iframe> but for multimedia objects like SVGs. It has the same dimensionality problem as the <iframe> tag, and is not stylable with CSS from the parent document, but can be manipulated with JavaScript from the parent.
  • If you're using an SVG for an icon multiple times on the same page, you can embed an <svg> tag with the icon inline in the HTML, and use a <use> tag to reference individual symbols from it. I do this on this page for the check- and x- mark icons and . This mitigates the cachability problem for icons used more than once on the same page, like say a trash can icon on a list of items, but the icons are still not cacheable accross pages. (One way to achieve full cacheability is to fetch the SVG via JavaScript and inject it into the page.)
联系我们 contact @ memedata.com