html - Multiple nth-last-child does't work as expected. Why? -
i have following html:
<ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> <li>11</li> <li>12</li> </ul> and following css applied it:
body { padding: 0; margin: 0; display: flex; align-items:center; justify-content:center; height: 100vh; background: #eee; } ul { max-width: 400px; min-width: 400px; } ul > li { overflow: hidden; float: left; width: 30%; height: 50px; margin: 1%; background: orange; color: #fff; display: flex; align-items:center; justify-content:center; } ul > li:nth-child(3n + 1):nth-last-child(3) { background: #727272; } you can see same in fiddle here.
now following selector:
ul > li:nth-child(3n + 1):nth-last-child(3) { background: #727272; } seems selecting first element in last row, not quite understanding how ?
also
ul > li:nth-child(3n + 1):last-child { background: #727272; } does't work. why ?
i infact expect following selector select first element on last row:
ul > li:nth-child(3n + 1):nth-last-child(1) { background: #727272; } so misinterpreting here , why multiple nth-child selection not work way expect ?
how trying achieve: select first element in last row .. believe have acheived , don't know how works.
this..
ul > li:nth-child(3n + 1):nth-last-child(1) { background: #727272; } does not work because doesn't meet both conditions.
it's not :nth-last-child(1) , :nth-child(3n+1) isn't selected.
this works
ul > li:nth-child(3n + 1):nth-last-child(3) { background: #727272; } becuasie both li:nth-child(3n + 1) , :nth-last-child(3).
however, fact selects first element in last row purely coincidental.
css cannot detect layout. has no idea in row @ all. css selects elements , nothing else.
Comments
Post a Comment