Problem
Styles are not updated when last-child
changes due to the addition of additional elements dynamically using JavaScript.
Example
Click "Add additional blocks" in the snippet below. When new blocks are added, the fourth .block
element ("Control block 4") will not have a lower red border, even if it is no longer the last element in #container
.
$("#addMore").one("click", function() { $("#container").append($("#container").html()); $(this).remove(); });
.block { border-bottom: 1px solid red; counter-increment: block; margin: 20px 0; position: relative; } .block:after { content: " " counter(block); } .block:last-child { border-bottom: 0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <div class="block">Test block</div> <div class="block">Test block</div> <div class="block">Test block</div> <div class="block">Test block</div> </div> <div id="addMore">Add more blocks</div>
Js fiddle
Expected Behavior
After clicking the "Add more blocks" button, all .block
elements should have a lower red frame, except for the eighth block ("test block 8").
Browsers done
Tested and not working in Chrome 55.0.2883.87 m. The expected behavior returned by IE 11.0.9600.18538 and Firefox 50.1.0.
Can this be solved without:
- Using JavaScript (i.e. dynamically adding a class that removes the red border from the last element)?
- Restructuring CSS to not use
last-child
(i.e. instead of first-child
and border-top
)?
source share