mathspp
  • Blog
    • Pydon'ts
    • Problems
    • TIL
    • Twitter threads
  • Books
  • Talks
  • Trainings
    • Advanced iteration
    • Python for scripting and automation
    • Rust for Python developers
  • Courses
  • About
Link blog

Dynamic table highlighting with CSS :has

on 06-02-2025 17:42

In this BlueSky post the user shows some crazy CSS that allows for dynamic table highlighting. The CSS included in that post was this:

td:has(~ td:hover), /* previous sibling cells */
table:has(td:nth-of-type(3):hover) /* column cells */
tr:not(:first-of-type):has(~ tr:hover)
td:nth-of-type(3) {
    background: var(--highlighted);
}

This CSS should highlight a row up to the cell that is hovered and it should also highlight a column up to the cell that is hovered. After removing the :not(:first-of-type) because I have proper table headers, the CSS seems to do the highlighting well for any row but only works for the 3rd column, as the example table below shows. I'm not sure yet if there's a way to not hardcode the 3 in the CSS to work for any column independently or if I'd have to write selectors explicitly for all columns.

# English Portuguese French German Italian
1JanuaryJaneiroJanvierJanuarGennaio
2FebruaryFevereiroFévrierFebruarFebbraio
3MarchMarçoMarsMärzMarzo
4AprilAbrilAvrilAprilAprile
5MayMaioMaiMaiMaggio
6JuneJunhoJuinJuniGiugno
7JulyJulhoJuilletJuliLuglio
8AugustAgostoAoûtAugustAgosto
9SeptemberSetembroSeptembreSeptemberSettembre
10OctoberOutubroOctobreOktoberOttobre
11NovemberNovembroNovembreNovemberNovembre
12DecemberDezembroDécembreDezemberDicembre

Previous link Next link

There's 10 types of people in the world, those who know binary and those who don't.

mathspp
  • Blog
    • Pydon'ts
    • Problems
    • TIL
    • Twitter threads
  • Books
  • Talks
  • Trainings
    • Advanced iteration
    • Python for scripting and automation
    • Rust for Python developers
  • Courses
  • About