Is there a way to use CSS selectors to get the middle child in a list of elements?
I know that there is no literal :middle-child
selector, but is there another way without resorting to Javascript?
Is there a way to use CSS selectors to get the middle child in a list of elements?
I know that there is no literal :middle-child
selector, but is there another way without resorting to Javascript?
Javascript is the only way to do this client side.
This has been working well for me:
*:not(:first-child):not(:last-child) {
...
}
You can see an example of this here: http://codepen.io/bentomas/pen/Gwqoe
The one caveat to this is that it only works in IE 9+: http://caniuse.com/#feat=css-sel3
:middle-child
pseudo class to do when I went looking for it and found this thread. –
Pas While not elegant, if you know the upper and lower limits of the total number of elements, you could take a brute force approach to select the middle element.
For example, the following rules will select the middle element in a set of 5, 7, or 9 elements.
div:nth-child(3):nth-last-child(3) {
/* The middle element in a set of 5 is the 3rd element */
}
div:nth-child(4):nth-last-child(4) {
/* The middle element in a set of 7 is the 4th element */
}
div:nth-child(5):nth-last-child(5) {
/* The middle element in a set of 9 is the 5th element */
}
Or with Sass:
@for $i from 3 through 5 {
div:nth-child(#{$i}):nth-last-child(#{$i}) {
/* The middle element */
}
}
div:nth-child(-1+#{$i}):nth-last-child(#{$i})
and/or div:nth-child(#{$i}):nth-last-child(-1+#{$i})
to select the element just left or right of center, respectively. –
Beckiebeckley You can use the "not first and not last" approach, like so:
CSS
li:not(:first-child):not(:last-child) {
color:red;
}
HTML
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
Check the JsFiddle
If you want to apply a style to all elements that are neither first children nor last children, you could use :not(:first-child)
, apply the style, and then use :last-child
to 'take the style away' from the last element. But you'd have to think about what happens when there are less than 3 elements.
I've encountered the need to target the middle child on several occasions, and I've taken to using this sass mixin I wrote after referencing many similar questions, and their respective answers.
// Generate a reasonable number rules limited by $n.
@mixin middle-child($n) {
// There is no middle for nChildren less than 3,
// so lets just start at 3.
@for $i from 3 to $n {
// Find the middle, bias right for odd numbers.
$mid: math.ceil(math.div($i, 2));
// Select only those sets of children that number $i.
&:first-child:nth-last-child(#{$i}) {
// Select the middle child of that set.
~ :nth-child(#{$mid}) {
@content; // Apply your styles.
}
}
}
}
Usage:
.navigation {
background-color: #ba0020;
.nav-item {
color: white;
@include middle-child( 8 ) {
font-weight: 900;
}
}
}
Have you tried :nth-child(#)
?
Depending on which one you want to select you just replace # with the number.
/* Selects the single middle element */
.parent :nth-child(odd) {
/* styles */
}
/* Selects the middle elements if there are multiple */
.parent :nth-child(n) {
/* styles */
}
middle element when there's an odd number of elements
.parent :nth-child((n+1)/2) {
/* styles */
}
middle elements when there's an even number of elements
.parent :nth-child(n/2),
.parent :nth-child((n/2)+1) {
/* styles */
}
Javascript is the only way to do this client side.
© 2022 - 2025 — McMap. All rights reserved.