<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[css - Rado's Blog]]></title><description><![CDATA[css - Rado's Blog]]></description><link>https://blog.rstankov.com/</link><image><url>http://blog.rstankov.com/favicon.png</url><title>css - Rado&apos;s Blog</title><link>https://blog.rstankov.com/</link></image><generator>Ghost 1.8</generator><lastBuildDate>Sat, 13 Jun 2026 13:49:06 GMT</lastBuildDate><atom:link href="https://blog.rstankov.com/tag/css/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Separating list items only with CSS]]></title><description><![CDATA[How often do you have a list of items and have to add spacing or border between them? Here is a simple trick on how to this only with CSS.]]></description><link>https://blog.rstankov.com/separator-between-list-items/</link><guid isPermaLink="false">5c72ca435ebe6b0004dc541a</guid><category><![CDATA[css]]></category><dc:creator><![CDATA[Radoslav Stankov]]></dc:creator><pubDate>Tue, 26 Feb 2019 09:54:47 GMT</pubDate><media:content url="https://images.unsplash.com/photo-1472437774355-71ab6752b434?ixlib=rb-1.2.1&amp;q=80&amp;fm=jpg&amp;crop=entropy&amp;cs=tinysrgb&amp;w=1080&amp;fit=max&amp;ixid=eyJhcHBfaWQiOjEyNzY3fQ" medium="image"/><content:encoded><![CDATA[<div class="kg-card-markdown"><img src="https://images.unsplash.com/photo-1472437774355-71ab6752b434?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyNzY3fQ" alt="Separating list items only with CSS"><p>Very often we have to implement a list of items with spacing or borders between them.</p>
<img src="https://s3.amazonaws.com/rstankov-blog/uploads/2019/02/Screenshot-2019-02-24-at-20.15.36.png" alt="Separating list items only with CSS" width="50%">
<p>The implementation of such styling is straight forward:</p>
<pre><code class="language-jsx">&lt;ul className=&quot;list&quot;&gt;
  {array.map((item, index) =&gt; (
    &lt;li key={item.id} className={index &gt; 0 ? : 'item withBorder' :'item'}&gt;&lt;Item item={item} /&gt;&lt;/li&gt;
  )}
&lt;/ul&gt;
</code></pre>
<pre><code class="language-css">.item {
  /* other properties */
}

.withBorder {
   margin-top: 10px;
   border-top: 1px solid black;
   padding-top: 10px;
}
</code></pre>
<p>I dislike having unneeded logic checks and conditions.  <code>index &gt; 0</code> is simple, but the reader of the code still have to parse it in their head. I call logic like this noise.</p>
<p>Having a lot of noise in your system makes it harder to understand and reason about. In this case, we also have two classes on a single element, and this increases the complexity.</p>
<p>So...can we remove the conditional and use one class name? Yes, we can ?:</p>
<pre><code class="language-jsx">&lt;ul className=&quot;list&quot;&gt;
  {array.map((item) =&gt; (
    &lt;li key={item.id} className=&quot;item&quot;&gt;&lt;Item item={item} /&gt;&lt;/li&gt;
  )}
&lt;/ul&gt;
</code></pre>
<p>The JavaScript portion of the code is a lot simpler now.</p>
<p>...but this CSS portion is more complex:</p>
<pre><code class="language-css">.item {
   /* other properties */
   margin-top: 10px;
   border-top: 1px solid black;
   padding-top: 10px;
}

.item:first-child {
   margin-top: 0;
   border-top: none;
   padding-top: 0;
}
</code></pre>
<p>We simplified the JavaScript by pushing the complexity down to the CSS level. This is usually a good thing.</p>
<p>An issue with this CSS code is that we are setting properties and then resetting them. This often leads people forgetting to reset all properties especially when they adding more in the future. When somebody reads the above styles, he/she have to parse in their head both definitions and see what is negated.</p>
<p>So we can still do better:</p>
<h2 id="thesolution">The solution</h2>
<p>In general, we want to put a certain style on all elements except the first one. By using the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_combinator">adjacent sibling combinator selector</a> we can do just that:</p>
<pre><code class="language-css">.item {
   /* other properties */
}
.item + .item {
    margin-top: 10px;
  border-top: 1px solid black;
  padding-top: 10px;
}
</code></pre>
<p>I find this solution very elegant. The styling will be applied to all elements except the first one. We are setting the properties once.</p>
<p>Having this as separate selector groups the separator logic from the rest of the styles a component</p>
<p><strong>Bonus:</strong> this approach works with Styled-Components as well.</p>
<pre><code class="language-jsx">const Li = styled.li`  
  &amp; + &amp; {
     margin-top: 10px;
    border-top: 1px solid black;
    padding-top: 10px;
  }
`
</code></pre>
<p><strong>Update:</strong> A <a href="https://twitter.com/ventsislaf/status/1100373970141040640">couple</a> of <a href="https://twitter.com/dhruvparmar">people</a> mentioned that you could use <code>:not(first-child)</code>:</p>
<pre><code class="language-css">.item {
   /* other properties */
}
.item:not(first-child) {
    margin-top: 10px;
  border-top: 1px solid black;
  padding-top: 10px;
}
</code></pre>
</div>]]></content:encoded></item></channel></rss>