Cropping & Scaling Images Using SVG Documents

Sara Soueidan · 2018年1月2日

AI 摘要 AI 生成,可能有误

This article explains how SVG's viewBox and preserveAspectRatio attributes can be used to crop, scale, and position images as a fallback or alternative to the CSS object-fit and object-position properties. The author demonstrates that nearly any combination of object-fit and object-position values can be replicated with preserveAspectRatio, and this technique works in any browser supporting SVG, including Internet Explorer back to IE9. The article also shows how to make the SVG solution accessible by adding a element and appropriate ARIA attributes.

I’m always interested in and thinking about ways to use SVG in my client projects to solve common UI challenges — uses that go beyond simple icon display and animated illustrations. I’m also always researching and looking for practical uses of SVG to add to my talk and workshops material that go beyond SVG the image format, and more into the document nature of it. After all, SVG on the Web isn’t just about displaying pretty and animated illustrations.

I’m also particularly interested in CSS and SVG as a combination (that I like to call “The Power Combo”) to solve common real-world challenges. These two work really well together. And it so happens than some of what’s possible in CSS is also possible in SVG, either because the CSS functionlity was imported from SVG to begin with, or simply because SVG documents also happen to offer tools that achieve the same functionality. And since SVG goes way back and has much better support than newer CSS features, it is possible to use SVG either as a fallback or as a replacement to some CSS functionalities. It all depends on the browser support you’re after.

SVG comes with a pair of attributes — namely viewBox and preserveAspectRatio — that allow us to manipulate the contents of an SVG (whether vector content or raster images) in a myriad of ways to achieve a myriad of things. One thing we can do with these attributes is control the scaling and position of the contents of the SVG. In this article, we’ll use these attributes to crop, scale and position images, as a fallback or alternative to the CSS object-fit and object-position properties.

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 Sara Soueidan

The CSS caret-* properties and custom caret accessibility

AI 摘要This article examines the CSS caret-* properties (caret-color, caret-animation, and caret-shape) and the accessibility considerations surrounding the caret, an often-overlooked focus indicator for editable text and caret browsing mode. It explains how caret browsing enables precise keyboard navigation of web pages, and how operating systems like macOS and Windows provide accessibility settings for caret blinking, thickness, and visibility that browsers honor inconsistently. The author urges caution when applying custom caret styles, since the caret may be the only focus indicator some keyboard or pointer users receive.

My top picks for Interop 2027

AI 摘要The author shares their ranked picks for Interop 2027, a yearly project aiming to improve cross-browser interoperability of key web platform features, using Firefox's community ranking page. Their top choices—the HTML focusgroup attribute and the ariaNotify() API—reflect their preference for features that improve both accessibility/user experience and developer experience, followed by other picks like heading-related HTML attributes, Interest Invokers, and various CSS properties. The author notes that features cannot be downvoted, only ranked or left unranked, and concludes by linking to blog posts where other web developers shared their own rankings.

CSS to speech: alternative text for CSS-generated content

AI 摘要The article explains how CSS-generated content inserted via ::before and ::after pseudo-elements is included in an element's accessible name computation, and why meaningful CSS images, icons, and unicode characters need descriptive alternative text while decorative ones should be hidden from screen readers. It introduces the CSS Generated Content Module Level 3 syntax that lets authors provide alternative text after a slash in the content property (with empty-string alt text for decorative content), noting that browser support is now very good except for older browsers like Internet Explorer. The author shares testing results across browsers and screen readers such as VoiceOver, observing that CSS images without alt text are excluded from the accessibility tree, images with alt text are announced properly, and there are some quirks like NVDA not announcing Chrome's CSS image alt text and Safari only showing broken CSS image alt text when VoiceOver is enabled.

🎥 New (Video) Podcast: "Navigating the World of Web Accessibility with Sara Soueidan", with Kevin Powell

AI 摘要The author and Kevin Powell finally recorded a nearly two-hour video podcast about navigating the world of web accessibility, covering topics like HTML semantics, ARIA, screen readers, the accessibility tree, how CSS affects accessibility, and the author's Practical Accessibility course. They also chatted about shared personal interests such as cooking, fermentation, and nutrition. The author invites front-end designers and developers curious about accessibility to watch or listen and share their feedback.

CSS-only scrollspy effect using scroll-marker-group and :target-current

AI 摘要The article explains how to create a CSS-only scrollspy effect using the new scroll-target-group property combined with the :target-current pseudo-class, which automatically highlights navigation links (like a table of contents) when their target sections are scrolled into view—no JavaScript required. It describes how applying scroll-target-group to a container of HTML anchors promotes them to scroll markers, letting the browser determine and style the active link. The author also covers accessibility considerations, including WCAG contrast requirements for active link styling and using aria-current to convey the active state to screen reader users.