Skip to main content

Devbit

Check whether there is only one child item using CSS

In breadcrumbs, for example, icons are often assigned for illustrating the following entries. However, under certain circumstances there may only be the initial entry, so the icon points to a blank space.
    .breadcrumb--item:only-child {
      svg {
        display: none;
      }
    }
    

Issue:

In breadcrumbs, for example, icons are often assigned for illustrating the following entries. However, under certain circumstances there may only be the initial entry, so the icon points to a blank space.
 

Breadcrumb <ol> with only one child <li> and icon <svg>
Breadcrumb <ol> with only one child <li> and icon <svg>

Solution:

Make use of the `:only-child` pseudo-class to determine whether there is only one item in the breadcrumb. See more in the Mozilla Dev Documentation

Also check "caniuse" for compatibility information: https://caniuse.com/?search=only-child 

Ein Mann mit kurzem grauem Bart und rasiertem Kopf trägt ein schwarzes T-Shirt mit der Aufschrift „DRPL“ und eine schwarze Kapuzenjacke. Er blickt freundlich in die Kamera. Im Hintergrund sind mehrere Bildschirme und ein Schreibtisch mit Technik-Equipment zu sehen.

Holger Weischenberg

Frontend Developer

Ihre Drupal-Website zukunftssicher machen

Wir prüfen Ihr System und zeigen, was Sie für AI, Wartung und Upgrades brauchen.