Cookie Consent by Free Privacy Policy Generator 📌 Using :has as a previous sibling selector

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 Using :has as a previous sibling selector


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

I got very excited this week when I finally used :has. The trouble with it has been browser support, which isn't yet high enough to use it in production code. In particular, it's not going to be in Firefox until 121 (finally!) and that's my favourite browser...

But this week I had an occasion to use it. This was because it was only an aesthetic change, so it won't matter if it doesn't work for people on older browsers. And also because the project I'm using it on has won't be launching for a few months, by which time it'll work in a lot more browsers.

Here is the scenario: I have a list of links. When I hover over any of them, the rest should go a bit transparent. But if I'm not hovering over any, none of them should go transparent.

What makes this slightly tricky is that the links should all have an opacity of 1 by default and only change when another link is hovered over.

The HTML

Here's some HTML I have set up for an example:

<ul>
  <li><a href="#">One</a></li>
  <li><a href="#">Two</a></li>
  <li><a href="#">Three</a></li>
  <li><a href="#">Four</a></li>
  <li><a href="#">Five</a></li>
  <li><a href="#">Six</a></li>
</ul>

Next siblings selector

Since the link is a child of the list item, the simplest thing to do is to put the hover on the list item. If I'm hovering on the link then by definition I'm hovering on the parent of the link.

Targeting the siblings that come after the item we're hovering on is easy:

li:hover ~ li {
  opacity: 0.5;
}

To do the same for the siblings that come before the one we're hovering on involves using :has and effectively writing the same thing, but backwards:

li:has(~ li:hover) {
  opacity: 0.5;
}

This feels so weird to read.

The first snippet here is saying: Apply the CSS to any li that is a (next) sibling of the one being hovered over.

The second snippet here is saying: Apply the CSS to any li that has a (next) sibling being hovered over.

Focusing

Applying something on hover is all very well and good, but what if you always want it to apply on focus? Here we can use focus-within: so if something within the li has focus, apply the CSS. The li can't have focus (unless you add tabindex to it) and the link is the only thing inside it that can be focused.

li:hover ~ li,
li:has(~ li:hover),
li:focus-within ~ li,
li:has(~ li:focus-within) {
  opacity: 0.5;
}

Simple - it's just the same code, but using focus-within instead of hover.

There is a problem with this when one of the links has focus, but you hover on another: everything goes a bit transparent. But arguably, how many users are going to do that? Also arguably, do you need the same effect on focus-within when the link you're focused on has an outline around it?

The final code

Here is the whole code in CodePen, so you can see it in action and have a play (as long as you're using a browser that supports :has, of course):

...



📌 Using :has as a previous sibling selector


📈 75.23 Punkte

📌 How to Increase CSS Class Selector Specificity to Beat the ID Selector Without Using Important


📈 49.29 Punkte

📌 :has(): the family selector


📈 28.25 Punkte

📌 :has(): the family selector


📈 28.25 Punkte

📌 Level Up Your CSS Skills With The :has() Selector


📈 28.25 Punkte

📌 Has() selector #shorts


📈 28.25 Punkte

📌 How to Use the :has() Selector in CSS


📈 28.25 Punkte

📌 An Introduction to the :has() Selector in CSS


📈 28.25 Punkte

📌 Unleash the Power of CSS :has() Selector: A Game-Changer for Styling Your Web Content


📈 28.25 Punkte

📌 My X session selector has learned new tricks with version 0.3, including starting of Wayland sessions.


📈 28.25 Punkte

📌 [dos] - Microsoft Internet Explorer 9 MSHTML - CDisp­Node::Insert­Sibling­Node Use-After-Free (MS13-037) (1)


📈 25.4 Punkte

📌 [dos] - Microsoft Internet Explorer 9 MSHTML - CDisp­Node::Insert­Sibling­Node Use-After-Free (MS13-037) (2)


📈 25.4 Punkte

📌 [dos] - Microsoft Internet Explorer 9 MSHTML - CDisp­Node::Insert­Sibling­Node Use-After-Free (MS13-037) (1)


📈 25.4 Punkte

📌 [dos] - Microsoft Internet Explorer 9 MSHTML - CDisp­Node::Insert­Sibling­Node Use-After-Free (MS13-037) (2)


📈 25.4 Punkte

📌 The Big Bang Theory – Staffel 11: Recap zu Episode 23 &quot;The Sibling Realignment&quot;


📈 25.4 Punkte

📌 SuSE Linux SMT up to 3.0.36 Sibling Server Host Header spoofing weak authentication


📈 25.4 Punkte

📌 Stuxnet sibling theory surges after Iran says nuke facility shut down by electrical fault


📈 25.4 Punkte

📌 Raspberry Pi Pico W review: The Pi's tiny sibling gets a wireless upgrade


📈 25.4 Punkte

📌 Nearby Star Is Sun's Long-Lost Sibling


📈 25.4 Punkte

📌 One More Microsoft Edge Legacy Feature Comes to Chromium Sibling


📈 25.4 Punkte

📌 Maximum absolute difference between the sibling nodes of given BST


📈 25.4 Punkte

📌 Trying to get a sibling to switch; she won’t budge


📈 25.4 Punkte

📌 Trying to get a sibling to switch; she won’t budge


📈 25.4 Punkte

📌 Microsoft Teams has a new history menu for jumping to previous locations


📈 22.64 Punkte

📌 UK Has Seen More Scams In The Last Year Than The Previous Three Years


📈 22.64 Punkte

📌 Time Travel Is Easy With A Time Zone Selector Button


📈 22.09 Punkte

📌 Cisco Gss 4492r Global Site Selector up to 3.0 Crash denial of service


📈 22.09 Punkte

📌 Cisco Gss 4492r Global Site Selector up to 3.0 Crash denial of service


📈 22.09 Punkte

📌 Ethereal up to 0.10.2 Presentation Protocol Selector Crash denial of service


📈 22.09 Punkte

📌 Use the :lang pseudo-class over the lang attribute selector for language-specific styles


📈 22.09 Punkte

📌 Apache Allura up to 1.10.x Dropdown Selector Stored cross site scripting


📈 22.09 Punkte











matomo