How do I set vertical space between list items?
To create vertical space between list items, you directly adjust the <li>
elements using CSS margin
:
This simple piece of CSS adds a 10px gap below each list item. Feel free to tweak the pixel value to your heart's content.
Skip the last gap: Pseudo-class magic
While our fast answer does the trick, we might not need extra space after the final item. This is where CSS :not()
pseudo-class comes into play.
Alternatively, the adjacent sibling selector can achieve similar results in just one line:
Debugging aid: visual border
During development, it's handy to visually confirm the margins. Applying a temporary border
can help:
Remember to remove the border once you've achieved the perfect spacing.
Line-height: The unsung hero
The line-height
property also plays a key role in determining the appearance of list items. It fine-tunes the space between text lines within each item:
A well-adjusted line-height
in sync with the margin
can make your list items more appealing.
Backward compatibility: Making old browsers happy
Working with older browsers or considering different browser compatibility? Then you might need to call in our JavaScript back-up:
Though it's wiser to let JavaScript stick to its duties and let CSS handle the styling.
After list: Space matters everywhere!
Consider giving some breathing space after the list as well. It ensures your text and design flow smoothly.
Balancing these values creates a harmonious rhythm throughout your content.
Was this article helpful?