惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog RSS Feed
W
WeLiveSecurity
I
InfoQ
L
Lohrmann on Cybersecurity
Simon Willison's Weblog
Simon Willison's Weblog
腾讯CDC
S
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threat Research - Cisco Blogs
P
Palo Alto Networks Blog
Attack and Defense Labs
Attack and Defense Labs
I
Intezer
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
S
Securelist
T
Tailwind CSS Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
T
Tor Project blog
博客园 - 叶小钗
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
Project Zero
Project Zero
C
Cybersecurity and Infrastructure Security Agency CISA
Apple Machine Learning Research
Apple Machine Learning Research
V
Visual Studio Blog
Know Your Adversary
Know Your Adversary
T
The Blog of Author Tim Ferriss
N
News and Events Feed by Topic
小众软件
小众软件
G
Google Developers Blog
F
Full Disclosure
O
OpenAI News
The Last Watchdog
The Last Watchdog
G
GRAHAM CLULEY
TaoSecurity Blog
TaoSecurity Blog
U
Unit 42
Jina AI
Jina AI
S
SegmentFault 最新的问题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
Y
Y Combinator Blog
N
News and Events Feed by Topic
K
Kaspersky official blog

Ahmad Shadeed

Better fluid sizing with round() Use Cases for Field Sizing The Basics of Anchor Positioning Item Flow CSS Relative Colors Balancing Text In CSS Should masonry be part of CSS grid? CSS display contents CSS Grid Areas CSS Cap Unit An Interactive Guide to CSS Container Queries CSS :has() Interactive Guide CSS Nesting UX in DevTools CSS Nesting Future CSS: State Container Queries Rebuilding a comment component with modern CSS Conditional CSS with :has and :nth-last-child CSS Text balancing with text-wrap:balance CSS Masking Do we need CSS flex-wrap detection? My CSS Wishlist Conditional CSS CSS Style Queries Inside the mind of a frontend developer: Article layout Inside the mind of a frontend developer: Hero section CSS container queries are finally here The CSS behind Figma Learn CSS Subgrid CSS :has Parent Selector Aligning Content In Different Wrappers Flexbox Dynamic Line Separator Hello, CSS Cascade Layers Building UI Components With SVG and CSS A Deep CSS Dive Into Radial And Conic Gradients Defensive CSS Building Real-life Components: Facebook Messenger Conditional Border Radius In CSS CSS Container Query Units Less Absolute Positioning With Modern CSS Aligning a Button Label Vertically Comparing Design Mockups To Code Result Using HSL Colors In CSS Custom Scrollbars In CSS Let CSS Container Queries For Designers The State of CSS Cross-Browser Development Overflow Issues In CSS Inspect Element As A Way To Increase Your Curiosity Handling Text Over Images in CSS Digging Into CSS Logical Properties Clipping Scrollable Areas On The inline-start Side Understanding Clip Path in CSS The Art of Building Real-life Components Handling Short And Long Content In CSS CSS Scroll Snap A Deep Dive Into CSS Grid minmax() CSS Variables 101 Finding The Root Cause of a CSS Bug Learn CSS centering How to detect browser support for Flexbox Gap CSS Mistakes While On Autopilot Digging Into the Flex Property Understanding CSS Multiple Backgrounds Aligning Logo Images in CSS Grid for layout, Flexbox for components Colors in CSS Thinking About The In-between Design Cases min(), max(), and clamp() CSS Functions Image Techniques On The Web Everything About Auto in CSS Learn Box Alignment Let Learn CSS Positioning Intrinsic Sizing In CSS CSS Grid Template Areas In Action Hiding Elements On The Web Creating a Variable Color Font From Scratch Building a Football Ticket With CSS and SVG Blending Modes in CSS CSS Variables With Inline Styles Implementing Dark Mode For My Website Rebuilding Apple Music Header in HTML & CSS Accessible Checkbox Layout Flickering On Browser Resize Enhancing The Clickable Area Size Custom Underlines with SVG Part 3: The Process of Implementing A UI Design From Scratch Part 2: The Process of Implementing A UI Design From Scratch Building An Old Nav Design CSS Flexbox: 5 Real World Use Cases I Used CSS Inline Flex For The First Time The Process of Implementing A UI Design From Scratch Common CSS Issues For Front-End Projects Handling Long and Unexpected Content in CSS How to Build Web Form Layouts With CSS Grid Grid Layout Ah-ha Moment Enhancing Our Components with CSS :empty Building Resizeable Components with Relative CSS Units CSS Writing Mode The Journey of Learning Front End Web Development on a Daily Basis
First Look At The CSS object-view-box Property
Ahmad Shadeed · 2022-05-20 · via Ahmad Shadeed

The Layout Maestro

I spent years teaching CSS layout on this blog. I put everything I know into The Layout Maestro course: 70+ lessons and 150+ interactive examples that teach you how to think CSS layouts, not just memorize syntax.

Get the course

I have always wanted a native CSS way to crop an image and position it in any direction I need. This has been possible by using an additional HTML element combined with different CSS properties that I will explain later.

In this article, I will walk you through the new CSS property object-view-box that was suggested by Jake Archibald early this year. It allows us to crop or resize replaced HTML elements like an </img> or a <video>.

The problem

In the following example, we have an image that needs to be cropped. Notice how we only want a specific portion of that image.

Currently, we can solve that by one of the following:

  • Using an <img> and wrapping it in an additional element
  • Using the image as a background-image and modifying the position and size

Wrapping in an additional element

This is a common solution to this problem, here is what’s happening:

  • Wrapped the image in another element (The <figure> in our case)
  • Added position: relative and overflow: hidden
  • Added position: absolute to the image, and played with the positioning and sizing values to achieve the result
<figure>
  <img src="img/brownies.jpg" alt="" />
</figure>
figure {
  position: relative;
  width: 300px;
  aspect-ratio: 1;
  overflow: hidden;
  border-radius: 15px;
}

img {
  position: absolute;
  left: -23%;
  top: 0;
  right: 0;
  bottom: 0;
  width: 180%;
  height: 100%;
  object-fit: cover;
}

Applying the image as a background

In this solution, we use a <div> and apply the image as a background, then we alter the position and size values.

<div class="brownies"></div>
.brownies {
  width: 300px;
  aspect-ratio: 3 / 2;
  background-image: url("brownies.jpg");
  background-size: 700px auto;
  background-position: 77% 68%;
  background-repeat: no-repeat;
}

It works great, but what if we want to apply the above to an <img>? Well, that’s what object-view-box is about.

Introducing object-view-box

I got excited when I saw that the property object-view-box might be shipped in Chrome 104. Available now in Chrome canary.

According to the CSS spec:

The object-view-box property specifies a “view box” over an element, similar to the <svg viewBox> attribute, zooming or panning over the element’s contents.

The property takes the value <basic-shape-rect> = <inset()> | <rect()> | <xywh()>. For the demo in this article, I will focus on the inset() usage.

Let’s get back to the problem.

With object-view-box, we will be able to use inset to draw a rectangle from the four sides (top, right, bottom, left), and then apply object-fit: cover to avoid distortion.

<img src="img/brownies.jpg" alt="" />
img {
  aspect-ratio: 1;
  width: 300px;
  object-view-box: inset(25% 20% 15% 0%);
}

How is that even working? Don’t worry, I will explain everything below. Let’s go!

The image intrinsic size

The intrinsic size is the default image width and height. The image I’m dealing with has a size of 1194 × 1194 px.

img {
  aspect-ratio: 1;
  width: 300px;
}

With the above CSS, the rendered size of the image will be 300 × 300 px.

Our goal is to draw a rectangle on the intrinsic image. To do that, we will use the inset() value.

Applying the inset value

The inset() value will be based on the original image width & height, resulting in a cropped image. It will help us in drawing an inset rectangle and control the four edges, similar to dealing with margin or padding.

The inset value defines an inset rectangle. We can control the four edges, just like we deal with margin or padding. In the following example, the card has an inset of 20px from all the edges.

Let’s get back to the CSS for the object-view-box.

img {
  aspect-ratio: 1;
  width: 300px;
  object-view-box: inset(25% 20% 15% 0%);
}

Here is how the above looks behind the scenes. The values 25%, 20%, 15%, and 0% represent the top, right, bottom, and left respectively.

Fix image distorsion

If the size of the image is square, the cropped result will be distorted.

This can be easy with the well-supported object-fit property.

img {
  aspect-ratio: 1;
  width: 300px;
  object-view-box: inset(25% 20% 15% 0%);
  object-fit: cover;
}

That’s it. Isn’t that just amazing? Being able to do art direction on an <img> can be very useful for responsive design, or even showing different parts of an image.

Zooming in or out

We can use the inset to zoom in or out an image. As per my testing, transition or animation won’t work with object-view-box.

We can also zoom out with a negative inset value.

Imagine how this will be useful for being able to zoom an image, without wrapping it in an additional element.

Demo

Here is a demo that you can test in Chrome Canary today. Please make sure to enable the “Experimental Web Platform features” flag.

See the Pen CSS :has - Color theme by Ahmad Shadeed (@shadeed) on CodePen.

Conclusion

I’m so excited about other potential use-cases for this new feature. That was a quick first look, and I will come back later with more explorations.