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

Why does .col + .col work?

In the teacher's notes of this video it says you can use the below code to add a left gutter to all but the first column:

.col + .col { padding-left: 1em; }

It works but I am just wondering if someone can explain the logic behind how it works.

Thanks.

1 Answer

Steven Parker
Steven Parker
243,318 Points

:point_right: Remember that the plus sign (+) is the adjacent sibling combinator.

It selects an element on the right which immediately follows the element on the left. By specifying the same thing twice, it will select all elements of a series except the first, since only that one does not follow one of its own kind.

You can accomplish the same thing — and perhaps be a bit more clear about the intent — using pseudo-selectors:

.col:not(:first-child)
  /* or */
.col:nth-child(1n+2)