I am using the following CSS for arranging list of boxes:
li {
display: block;
float: left;
}
I'd like to carefully tune margins of <li>
items so that the last item on the row (horizontal span) would get different margins.
Are there any CSS selectors which could target float elements based on their position?
The :last-of-type selector allows you to target the last occurence of an element within its container. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling content. ).
:last-child will not work if the element is not the VERY LAST element. I think it's crucial to add/emphasize that :last-child will not work if the element is not the VERY LAST element in a container.
Usually, your best bet is to create a negative right margin on your containing block, e.g.:
<ul>
<li>Stuff Here</li>
<li>Stuff Here</li>
<li>Stuff Here</li>
<li>Stuff Here</li>
<li>Stuff Here</li>
<li>Stuff Here</li>
<li>Stuff Here</li>
</ul>
<style>
li {
float: left;
width: 30%;
margin-right: 3%;
}
ul {
margin-right: -3%; /* compensate for last float %/
}
</style>
My example is hastily written, but I'd note that the outer negative margin doesn't have to be the same. You'll have to fiddle.
Does that help?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With