<?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:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Hjertholm on Medium]]></title>
        <description><![CDATA[Stories by Daniel Hjertholm on Medium]]></description>
        <link>https://medium.com/@danielhjertholm?source=rss-a4f00af08400------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*sH-w9GmvpIfVbydu.</url>
            <title>Stories by Daniel Hjertholm on Medium</title>
            <link>https://medium.com/@danielhjertholm?source=rss-a4f00af08400------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 25 May 2026 22:04:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@danielhjertholm/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[CSS Grid with Inner Borders Only]]></title>
            <link>https://medium.com/@danielhjertholm/css-grid-with-inner-borders-only-ff26b9ede32f?source=rss-a4f00af08400------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff26b9ede32f</guid>
            <category><![CDATA[css-grid]]></category>
            <category><![CDATA[borders]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[grid-layout]]></category>
            <category><![CDATA[grid]]></category>
            <dc:creator><![CDATA[Daniel Hjertholm]]></dc:creator>
            <pubDate>Thu, 02 Jan 2020 13:52:58 GMT</pubDate>
            <atom:updated>2020-07-22T20:27:35.043Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*qgHy_BIefnaw-xAvk2pG1A.png" /></figure><p>So, you want to add borders to your grid, but only between the cells, not around the grid itself. This is surprisingly difficult, as targeting grid-gaps with CSS is not possible. A common workaround is to give the grid container a background color, and make a very small grid-gap between the cells, so that the background becomes the border. The CodePen below uses this solution.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdanhje%2Fembed%2Fpreview%2FZEYXQRW%3Fheight%3D600%26slug-hash%3DZEYXQRW%26default-tabs%3Dcss%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2Fdanhje%2Fpen%2FZEYXQRW&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fm.cdpn.io%2Fscreenshot-coming-soon-small.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/f87abe6453f43d3ae6a4941fcf254d22/href">https://medium.com/media/f87abe6453f43d3ae6a4941fcf254d22/href</a></iframe><p>This can work, but if you want dotted or dashed borders, you’re out of luck. You are also stuck with the small grid-gap. If you want a bigger gap, you will have to fake it using paddings or similar. And if you want the whole grid to be transparent in order to show the background, another solution is needed.</p><p>Another way to achieve inner borders, which lets you keep your grid-gap, is to turn the ::before and ::after pseudo elements of the grid cells into horizontal and vertical borders. Since we’ll be using absolute positioning of the pseudo-elements, and since absolute positioned elements are positioned relative to their nearest positioned ancestor, we need to make all grid cells positioned. This shouldn’t have any side-effects, as the cells’ positions are fixed in the grid. We also need to make sure overflow is visible (which it should be by default). For the purpose of this demonstration, we’ll give the cells a background color as well.</p><pre>.grid &gt; * {<br>  position: relative;<br>  overflow: visible;<br>  bakcground-color: #ccc;<br>}</pre><p>Let’s say you have a 3-column grid with a grid-gap of 20px. Targeting cells in the second and third column using the pseudo-class :nth-child, we can add grid-lines to the left of each cell. Since we are placing the border on the pseudo-element ::before instead of the cell itself, we can offset it to the left without affecting the cell. We set the height of the pseudo-element equal to the cell heigh pluss the gap height using calc().</p><pre>.grid &gt; :nth-child(3n+2)::before,<br>.grid &gt; :nth-child(3n+3)::before {<br>  content: &#39;&#39;;<br>  position: absolute;<br>  top: 0;<br>  left: -10px;<br>  height: calc(100% + 20px);<br>  border-left: 1px solid red;<br>}</pre><p>The result can be seen below. Not bad, but the grid lines extend below the bottom of the last row.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/1*Nj2fCFUFfAI8A55GA2Pqhw.png" /></figure><p>We could fix this by targeting the last row with :nth-child and making the lines 100% rather than calc(100% + 20px) there, but since we don’t necessarily know how many rows we will have, this is not the best solution. A better way is to make an exeption to the the first row, and let all the following rows have the same css.</p><pre>/* Vertical lines to the left of cells in the top row */<br>.grid &gt; :nth-child(2)::before,<br>.grid &gt; :nth-child(3)::before {<br>  content: &#39;&#39;;<br>  position: absolute;<br>  top: 0;<br>  left: -10px;<br>  height: 100%;<br>  border-left: 1px solid red;<br>}</pre><pre>/* Vertical lines to the left of cells in all other rows */<br>.grid &gt; :nth-child(3n+5)::before,<br>.grid &gt; :nth-child(3n+6)::before {<br>  content: &#39;&#39;;<br>  position: absolute;<br>  top: -20px;<br>  left: -10px;<br>  height: calc(100% + 20px);<br>  border-left: 1px solid red;<br>}</pre><p>Now we’re getting somewhere!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/1*pqdOztDKlgVg2BYE8NV8Ug.png" /></figure><p>For the horizontal grid lines, we’ll use the ::after pseudo-element.</p><pre>/* Horizontal lines above cells in the first column */<br>.grid &gt; :nth-child(3n+4)::after {<br>  content: &#39;&#39;;<br>  position: absolute;<br>  top: -10px;<br>  left: 0;<br>  width: 100%;<br>  border-top: 1px solid red;<br>}</pre><pre>/* Horizontal lines above cells in all other columns */<br>.grid &gt; :nth-child(3n+5)::after,<br>.grid &gt; :nth-child(3n+6)::after {<br>  content: &#39;&#39;;<br>  position: absolute;<br>  top: -10px;<br>  left: -20px;<br>  width: calc(100% + 20px);<br>  border-top: 1px solid red;<br>}</pre><p>Voilà!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*ijG7zkcfaAPWF42Y2pSvaQ.png" /></figure><p>The complete code can be found in the CodePen below.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdanhje%2Fembed%2Fpreview%2FNWPvQqe%3Fheight%3D600%26slug-hash%3DNWPvQqe%26default-tabs%3Dcss%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2Fdanhje%2Fpen%2FNWPvQqe&amp;image=https%3A%2F%2Fscreenshot.codepen.io%2F3828115.NWPvQqe.small.6cdccb5b-17a5-409d-9261-070cdbab8f6e.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/7364a79b6220004de3e3617cb7b5325e/href">https://medium.com/media/7364a79b6220004de3e3617cb7b5325e/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff26b9ede32f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>