Grouped list of elements by arbitrary attribute with CSS

I'm trying to visually group a list of elements by creating a header above each break in a sequence of attributes. I was able to accomplish this when the group names are known in advance. But what if I want to support an unknown multitude of bands? Is it possible to detect a match when the actual value of an attribute is a variable?

This is how it currently works with predefined attribute values. It's not very extensible.

https://jsfiddle.net/6g7qesja/1/

CSS

.item:first-child:before,
.item:not([data-group="First Group"]) + .item[data-group="First Group"]:before,
.item:not([data-group="Second Group"]) + .item[data-group="Second Group"]:before,
.item:not([data-group="Third Group"]) + .item[data-group="Third Group"]:before {
    content: attr(data-group);
    display: block;
    font-weight: bold;
}

      

Html

<div class="item" data-group="First Group">one</div>
<div class="item" data-group="First Group">two</div>
<div class="item" data-group="First Group">three</div>
<div class="item" data-group="Second Group">four</div>
<div class="item" data-group="Second Group">five</div>
<div class="item" data-group="Second Group">six</div>
<div class="item" data-group="Third Group">seven</div>
<div class="item" data-group="Third Group">eight</div>
<div class="item" data-group="Third Group">nine</div>

      

RENDERS

First group

one

two

three

Second group

four

five

six

Third group

seven

eight

nine

+3
css css-selectors css3


source to share


No one has answered this question yet

Check out similar questions:

3856
Change HTML5 placeholder color with CSS
2926
Is there a CSS parent selector?
2900
Set cellpadding and cellpacing in CSS?
2687
Can CSS be applied to half a character?
1966
How can I select an element with multiple classes in jQuery?
1942
How to move the height: 0; height: auto; using CSS?
1777
How do CSS triangles work?
1236
Is there a previous sibling selector?
1156
What characters are valid in CSS class names / selectors?
745
CSS selector for first element with class



All Articles
Loading...
X
Show
Funny
Dev
Pics