亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Sliding effect
Background animation
The first animation is very easy because each project has its own background animation, which means we don't have to care about the text content, as the background automatically fills the entire space.
Combining two effects
Bounce effect? why not? !
Conclusion
Home Web Front-end CSS Tutorial Fancy Menu Navigation Using Anchor Positioning

Fancy Menu Navigation Using Anchor Positioning

Mar 07, 2025 pm 05:14 PM

Fancy Menu Navigation Using Anchor Positioning

I believe you have heard of the CSS anchor point positioning function, right? This feature allows you to link any element on the page to another element, the anchor point. It is very useful for all tooltips, but it also creates many other good results.

This article will study menu navigation, and I rely on anchor positioning to create excellent hover effects on links.

It's cool, right? We have a sliding effect, the blue rectangle perfectly adapts to the text content through a smooth transition. If you are not familiar with anchor positioning, this example is perfect for you as it is simple and will give you the basics of this new feature. We'll learn another example, so stick to the end!

Note that as of this writing, only Chromium-based browsers fully support anchor positioning. Before other browsers can support this feature more widely, you need to view the demo in browsers like Chrome or Edge.

http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15bhttp://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15bhttp://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b initial configuration

Let's start with the HTML structure, which is just a nav element containing an unordered link list:

<nav><ul>
<li><a href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b">Home</a></li>
    <li><a href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b">About</a></li>
    <li><a href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b">Projects</a></li>
    <li><a href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b">Blog</a></li>
    <li><a href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b">Contact</a></li>
  </ul></nav>

We won't spend much time explaining this structure, because it may also be different if your use cases are different. Just make sure the semantics are related to what you are trying to do. As for the CSS section, we will start with some basic styles to create horizontal menu navigation.

ul {
  padding: 0;
  margin: 0;
  list-style: none;
  display: flex;
  gap: .5rem;
  font-size: 2.2rem;
}

ul li a {
  color: http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b000;
  text-decoration: none;
  font-weight: 900;
  line-height: 1.5;
  padding-inline: .2em;
  display: block;
}

So far, nothing special. We removed some default styles and used Flexbox to align the elements horizontally.

Sliding effect

First, let us understand how the effect works. At first glance, it seems we have a rectangle that shrinks to a very small height, moves to a hover element, and then grows to a full height. This is the visual effect, but in reality, multiple elements are involved!

This is my first demonstration, and I use different colors to better understand what is going on.

Each menu item has its own "element" that can shrink or grow. Then we have a common "element" (the one in red) that slides between different menu items. The first effect is done using background animation, and the second effect is where anchor point positioning comes into play!

Background animation

For the first part, we will animate the height of the CSS gradient:

/* 1 */
ul li {
  background: 
    conic-gradient(lightblue 0 0)
    bottom/100% 0% no-repeat;
  transition: .2s;
}

/* 2 */
ul li:is(:hover,.active) {
  background-size: 100% 100%;
  transition: .2s .2s;
}

/* 3 */
ul:has(li:hover) li.active:not(:hover) {
  background-size: 100% 0%;
  transition: .2s;
}

We define a gradient with a width of 100% and a height of 0% at the bottom. The gradient syntax may look weird, but it is the shortest syntax that allows me to have a monochrome gradient.

Related: "How to correctly define monochrome gradient"

Then, if the menu item is hovered or has a .active class, we set the height to 100%. Please note the use of delays here to ensure that growth occurs after shrinkage.

Finally, we need to deal with special cases of .active items. If we hover any item (not the active item), the .active item will get a shrinking effect (gradient height equals 0%). This is what the third selector in the code does.

Our first animation is finished! Note how growth starts after the contraction is completed due to the delay we defined in the second selector. Anchor Positioning Animation

The first animation is very easy because each project has its own background animation, which means we don't have to care about the text content, as the background automatically fills the entire space.

We will use one element to perform a second animation that slides between all menu items while adjusting its width to fit the text of each item. This is where anchor positioning can help us.

Let's start with the following code:

To avoid adding extra elements, I prefer to use pseudo-elements on ul. It should be absolutely positioned, and we will rely on two properties to activate anchor positioning.
<nav><ul>
<li><a href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b">Home</a></li>
    <li><a href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b">About</a></li>
    <li><a href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b">Projects</a></li>
    <li><a href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b">Blog</a></li>
    <li><a href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b">Contact</a></li>
  </ul></nav>

We use the anchor-name attribute to define the anchor point. When a menu item is hovered or has a .active class, it becomes an anchor element. If another item is hovering, we also have to remove the anchor point from the .active item (so, the last selector in the code). In other words, only one anchor point is defined at a time.

Then we use the position-anchor attribute to link the pseudo-element to the anchor point. Note how the two use the same notation --li. This is similar to, for example, how we define @keyframes with a specific name and then use it in the animation property. Remember that you have to use the

syntax, which means that the name must always start with two dashes (--).

The pseudo-element is placed correctly, but nothing is seen because we do not define any dimensions! Let's add the following code:

The

height attribute is simple, but anchor() is a new member. Here is what Juan Diego describes it in Almanac:
ul {
  padding: 0;
  margin: 0;
  list-style: none;
  display: flex;
  gap: .5rem;
  font-size: 2.2rem;
}

ul li a {
  color: http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b000;
  text-decoration: none;
  font-weight: 900;
  line-height: 1.5;
  padding-inline: .2em;
  display: block;
}

The

CSS anchor() function takes one side of the anchor element and parses it into its positioned
. It can only be used for inline properties (such as top, bottom, bottom, left, right, etc.), and is usually used to place absolutely positioned elements relative to the anchor point.

Let's check the MDN page as well:

anchor() The CSS function can be used in the embedded attribute value of the anchor positioning element, returning the length value relative to its associated anchor element edge position.

Usually, we use left: 0 to place the absolute element on the left edge of its containing block (i.e., the nearest ancestor with position: relative). left: anchor(left) will do the same, but it will take into account the associated anchor element instead of containing blocks.

That's it - we're done! Hover the menu items in the demo below to see how pseudo-elements slide between them.

Every time you hover your mouse over a menu item, it becomes a new anchor for the pseudo-element (ul:before). This also means that the anchor(...) value will change, resulting in a sliding effect! Let's not forget to use transition or we will have a sudden change.

We can also write code like this:

<nav><ul>
<li><a href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b">Home</a></li>
    <li><a href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b">About</a></li>
    <li><a href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b">Projects</a></li>
    <li><a href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b">Blog</a></li>
    <li><a href="http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b">Contact</a></li>
  </ul></nav>

In other words, instead of using physical properties such as left, right, and bottom, we can rely on the inset abbreviation, and instead of defining position-anchor, we can include the name of the anchor in the anchor() function. We've repeated the same name three times here, which may not be the best option, but in some cases you might want your element to consider multiple anchors, in which case this syntax will make sense.

Combining two effects

Now, we combine the two effects, void, the hallucination is perfect!

Please note the transition value, where the delay is important:

ul {
  padding: 0;
  margin: 0;
  list-style: none;
  display: flex;
  gap: .5rem;
  font-size: 2.2rem;
}

ul li a {
  color: http://ipnx.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b000;
  text-decoration: none;
  font-weight: 900;
  line-height: 1.5;
  padding-inline: .2em;
  display: block;
}

We have a series of three animations—reduce the height of the gradient, slide the pseudo-elements, and increase the height of the gradient—so we need to set a delay between them to put everything together. This is why for pseudo-element slides, we have a delay equal to an animation duration (transition: .2.2s), while for the growth part, the delay equals twice the duration (transition: .2s.4s).

Bounce effect? why not? !

Let's try another weird animation where the highlighted rectangle deforms into a small circle, jumps to the next item, and then deforms back into the rectangle again!

I won't explain this example too much, because this is your homework for analyzing the code! I'll provide some tips so you can unpack what's going on.

As with the previous effect, we combined two animations. For the first one, I will use the pseudo-elements of each menu item, I will resize and border-radius to simulate the deformation. For the second animation, I will create a small circle using the ul pseudo-element, which I move between menu items.

This is another version of the demo, different colors and slower transitions to better visualize each animation:

The tricky part is the jump effect, I used a weird cubic-bezier(), but I have a detailed article explaining this technique in my CSS-Tricks article "Advanced CSS Animation Using cubic-bezier()".

Conclusion

I hope you enjoyed this little experiment using the anchor positioning function. We've only looked at three properties/values, but that's enough to get you ready to use this new feature. The anchor-name and position-anchor attributes are mandatory parts that link an element (usually called the "target" element in this context) to another element (we call the "anchor" element in this context). From there, you can use the anchor() function to control the position.

Related: CSS Anchor Point Points Guide

The above is the detailed content of Fancy Menu Navigation Using Anchor Positioning. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CSS tutorial for creating loading spinners and animations CSS tutorial for creating loading spinners and animations Jul 07, 2025 am 12:07 AM

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

Addressing CSS Browser Compatibility issues and prefixes Addressing CSS Browser Compatibility issues and prefixes Jul 07, 2025 am 01:44 AM

To deal with CSS browser compatibility and prefix issues, you need to understand the differences in browser support and use vendor prefixes reasonably. 1. Understand common problems such as Flexbox and Grid support, position:sticky invalid, and animation performance is different; 2. Check CanIuse confirmation feature support status; 3. Correctly use -webkit-, -moz-, -ms-, -o- and other manufacturer prefixes; 4. It is recommended to use Autoprefixer to automatically add prefixes; 5. Install PostCSS and configure browserslist to specify the target browser; 6. Automatically handle compatibility during construction; 7. Modernizr detection features can be used for old projects; 8. No need to pursue consistency of all browsers,

Styling visited links differently with CSS Styling visited links differently with CSS Jul 11, 2025 am 03:26 AM

Setting the style of links you have visited can improve the user experience, especially in content-intensive websites to help users navigate better. 1. Use CSS's: visited pseudo-class to define the style of the visited link, such as color changes; 2. Note that the browser only allows modification of some attributes due to privacy restrictions; 3. The color selection should be coordinated with the overall style to avoid abruptness; 4. The mobile terminal may not display this effect, and it is recommended to combine it with other visual prompts such as icon auxiliary logos.

Creating custom shapes with css clip-path Creating custom shapes with css clip-path Jul 09, 2025 am 01:29 AM

Use the clip-path attribute of CSS to crop elements into custom shapes, such as triangles, circular notches, polygons, etc., without relying on pictures or SVGs. Its advantages include: 1. Supports a variety of basic shapes such as circle, ellipse, polygon, etc.; 2. Responsive adjustment and adaptable to mobile terminals; 3. Easy to animation, and can be combined with hover or JavaScript to achieve dynamic effects; 4. It does not affect the layout flow, and only crops the display area. Common usages are such as circular clip-path:circle (50pxatcenter) and triangle clip-path:polygon (50%0%, 100 0%, 0 0%). Notice

What is the difference between display: inline, display: block, and display: inline-block? What is the difference between display: inline, display: block, and display: inline-block? Jul 11, 2025 am 03:25 AM

Themaindifferencesbetweendisplay:inline,block,andinline-blockinHTML/CSSarelayoutbehavior,spaceusage,andstylingcontrol.1.Inlineelementsflowwithtext,don’tstartonnewlines,ignorewidth/height,andonlyapplyhorizontalpadding/margins—idealforinlinetextstyling

What is the CSS Painting API? What is the CSS Painting API? Jul 04, 2025 am 02:16 AM

TheCSSPaintingAPIenablesdynamicimagegenerationinCSSusingJavaScript.1.DeveloperscreateaPaintWorkletclasswithapaint()method.2.TheyregisteritviaregisterPaint().3.ThecustompaintfunctionisthenusedinCSSpropertieslikebackground-image.Thisallowsfordynamicvis

How to create responsive images using CSS? How to create responsive images using CSS? Jul 15, 2025 am 01:10 AM

To create responsive images using CSS, it can be mainly achieved through the following methods: 1. Use max-width:100% and height:auto to allow the image to adapt to the container width while maintaining the proportion; 2. Use HTML's srcset and sizes attributes to intelligently load the image sources adapted to different screens; 3. Use object-fit and object-position to control image cropping and focus display. Together, these methods ensure that the images are presented clearly and beautifully on different devices.

What is CSS and what does it stand for? What is CSS and what does it stand for? Jul 03, 2025 am 01:48 AM

CSS,orCascadingStyleSheets,isthepartofwebdevelopmentthatcontrolsawebpage’svisualappearance,includingcolors,fonts,spacing,andlayout.Theterm“cascading”referstohowstylesareprioritized;forexample,inlinestylesoverrideexternalstyles,andspecificselectorslik

See all articles