I don’t understand why these two examples behave differently. The objective of the HTML and CSS is simply to align the divs horisontally and let the last div (to the right) take up the remaining space (remaining width of container).
using specific id for right item:
<style type="text/css">
#left {
float: left;
padding: 0 1cm;
background-color: #ff0000;
}
#right {
width: 100%;
background-color: #00FF00;
}
</style>
<div>
<div id="left">item 1</div>
<div id="left">item 2</div>
<div id="right">last</div>
</div>
using :last-child:
<style type="text/css">
#left {
float: left;
padding: 0 1cm;
background-color: #ff0000;
}
#left:last-child {
width: 100%;
background-color: #00FF00;
}
</style>
<div>
<div id="left">item 1</div>
<div id="left">item 2</div>
<div id="left">last</div>
</div>
You are using IDs incorrectly. An ID represents a unique element of a page. Try this:
HTML
CSS
Edit
Your
#rightexample displayed differently because it was not inheritingfloat: left;. If you want to mimic that functionality, addfloat: none;to the CSS like so: