Tuesday, September 17, 2024
HomeProgrammingA number of Anchors | CSS-Tips

A number of Anchors | CSS-Tips


Solely Chris, proper? You’ll wish to view this in a Chromium browser:

That is precisely the kind of factor I like, not for its practicality (cuz it ain’t), however for the way it illustrates an idea. Usually, tutorials and demos attempt to comply with the “guidelines” — no matter these could also be — but breaking them helps you perceive how a sure factor works. That is a kind of.

The idea is fairly easy: one goal factor could be connected to a number of anchors on the web page.

<div class="anchor-1"></div>
<div class="anchor-2"></div>
<div class="goal"></div>

We’ve gotta register the anchors and fasten the .goal to them:

.anchor-1 {
  anchor-name: --anchor-1;
}

.anchor-2 {
  anchor-name: --anchor-2;
}

.goal {
  
}

Wait, wait! I didn’t connect the .goal to the anchors. That’s as a result of we’ve got two methods to do it. One is utilizing the position-anchor property.

.goal {
  position-anchor: --anchor-1;
}

That establishes a target-anchor relationship between the 2 parts. But it surely solely accepts a single anchor worth. Hmm. We’d like greater than that. That’s what the anchor() perform can do. Nicely, it doesn’t take a number of values, however we are able to declare it a number of occasions on completely different inset properties, every referencing a unique anchor.

.goal {
  prime: anchor(--anchor-1, backside);
}

The second piece of anchor()‘s perform is the anchor edge we’re positioned to and it’s gotta be some kind of bodily or logical insetprime, backside, begin, finish, inside, exterior, and many others. — or proportion. We’re bascially saying, “Take that .goal and slap it’s prime edge towards --anchor-1‘s backside edge.

That additionally works for different inset properties:

.goal {
  prime: anchor(--anchor-1 backside);
  left: anchor(--anchor-1 proper);
  backside: anchor(--anchor-2 prime);
  proper: anchor(--anchor-2 left);
}

Discover how each anchors are declared on completely different properties by the use of anchor(). That’s rad. However we aren’t truly anchored but as a result of the .goal is rather like another factor that participates within the regular doc movement. We’ve to yank it out with absolute positioning for the inset properties to take maintain.

.goal {
  place: absolute;

  prime: anchor(--anchor-1 backside);
  left: anchor(--anchor-1 proper);
  backside: anchor(--anchor-2 prime);
  proper: anchor(--anchor-2 left);
}

In his demo, Chris cleverly attaches the .goal to 2 <textarea> parts. What makes it intelligent is that <textarea> means that you can click on and drag it to alter its dimensions. The 2 of them are completely positioned, one pinned to the viewport’s top-left edge and one pinned to the bottom-right.

If we connect the .goal's prime and left edges to --anchor-1‘s backside and proper edges, then connect the goal's backside and proper edges to --anchor-2‘s prime and left edges, we’re successfully anchored to the 2 <textarea> parts. That is what permits the .goal factor to stretch with the <textarea> parts when they’re resized.

However there’s a small catch: a <textarea> is resized from its bottom-right nook. The second <textarea> is positioned in a approach the place the resizer isn’t straight connected to the .goal. If we rotate(180deg), although, it’s all good.

Once more, you’ll wish to view that in a Chromium browser on the time I’m penning this. Right here’s a clip as a substitute for those who choose.

That’s only a background-color on the .goal factor. We are able to put a bit character in there as a substitute as a background-image like Chris did to shine this off.

Enjoyable, proper?! It nonetheless blows my thoughts that is all taking place in CSS. It wasn’t many days in the past that one thing like this could’ve been a job for JavaScript.


Direct Hyperlink →

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments