Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Is using :last-child a best practice when aligning items?

In this video we add a right margin to each <li> element in the .main-nav unordered list. Is it considered a best practice to using the :last-child selector to remove the margin added, so that there is not any right margin for the last element?

This would be a general question when styling grouped items.

1 Answer

Using the :last-child selector is an option in this issue.

I wouldn't necessarily say its a best practise.

However with alot of coding there is many considered acceptable methods for achieving the same result.

What tends to be the most important aspect is writing clear coherent code so that both yourself and others can make sense of it. Comments can be useful in this respect.

An alternative for this example would be to apply a margin of half the size stated in the video to both sides of the list items. This would also provide some spacing either end of the unordered list too.

But what is really important is using what works well for you. Similarly you will learn about other methods of spacing content using a technique called (flex-box)[https://teamtreehouse.com/library/css-flexbox-layout] which might of interest to you.

Hope this helps and happy coding!