{"componentChunkName":"component---src-components-blog-template-js","path":"/blog/2023-12-03-tailwind-css/","result":{"data":{"markdownRemark":{"frontmatter":{"title":"Tailwind CSS","date":"2023-12-03"},"html":"<p>For a long time I wrote CSS the traditional way, either in separate stylesheet files or using CSS modules. Recently I started using Tailwind CSS and it has changed how I think about styling.</p>\n<h3>What Is Tailwind</h3>\n<p>Tailwind is a utility-first CSS framework. Instead of writing custom CSS classes like <code class=\"language-text\">.card-header</code> or <code class=\"language-text\">.primary-button</code>, we compose styles directly in the HTML using small utility classes. Each class does one thing.</p>\n<p>For example, instead of writing this in CSS:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.card {\n  padding: 16px;\n  background-color: white;\n  border-radius: 8px;\n  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);\n}</code></pre></div>\n<p>We write this in the HTML:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">&lt;div class=\"p-4 bg-white rounded-lg shadow\">\n  ...\n&lt;/div></code></pre></div>\n<p>Each class maps to a CSS property. <code class=\"language-text\">p-4</code> is padding, <code class=\"language-text\">bg-white</code> is background colour, <code class=\"language-text\">rounded-lg</code> is border radius, and <code class=\"language-text\">shadow</code> is box shadow.</p>\n<h3>Why I Like It</h3>\n<p>The main benefit for me is speed. I do not have to switch between files or come up with class names. The styling is right there in the component. Naming things is one of the harder parts of programming, and with Tailwind I rarely need to invent CSS class names.</p>\n<p>Another benefit is consistency. Tailwind uses a design system with predefined spacing, colours, and sizing values. Instead of using arbitrary pixel values like <code class=\"language-text\">padding: 13px</code>, we use values from the scale like <code class=\"language-text\">p-3</code> (12px) or <code class=\"language-text\">p-4</code> (16px). This naturally keeps the design consistent across the application.</p>\n<p>Dead code is also less of a problem. With traditional CSS, unused styles tend to accumulate over time because we are not sure if they are still needed somewhere. With Tailwind, the styles are in the markup, so when we remove a component, the styles go with it.</p>\n<h3>Responsive Design</h3>\n<p>Tailwind makes responsive design straightforward using breakpoint prefixes. If we want a grid that shows one column on mobile and three columns on larger screens:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">&lt;div class=\"grid grid-cols-1 md:grid-cols-3 gap-4\">\n  ...\n&lt;/div></code></pre></div>\n<p>The <code class=\"language-text\">md:</code> prefix means the style only applies at the medium breakpoint and above. There are prefixes for <code class=\"language-text\">sm</code>, <code class=\"language-text\">md</code>, <code class=\"language-text\">lg</code>, <code class=\"language-text\">xl</code>, and <code class=\"language-text\">2xl</code>.</p>\n<h3>Customisation</h3>\n<p>The default Tailwind configuration covers most use cases, but we can customise it in the <code class=\"language-text\">tailwind.config.js</code> file. We can add custom colours, fonts, spacing values, or extend the existing ones to match our design system.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">module.exports = {\n  theme: {\n    extend: {\n      colors: {\n        brand: \"#1a73e8\",\n      },\n    },\n  },\n};</code></pre></div>\n<p>After adding a custom colour, we can use it like <code class=\"language-text\">bg-brand</code> or <code class=\"language-text\">text-brand</code> anywhere in the markup.</p>\n<h3>The Trade-Off</h3>\n<p>The most common criticism is that the HTML looks cluttered with all the utility classes. For components with many styles, the class string can get long. In practice, since we use components in React (or similar frameworks), each component is small and self-contained, so the class strings are usually manageable. For repeated patterns, Tailwind also lets us extract classes using <code class=\"language-text\">@apply</code> in CSS, though I try to use that sparingly and prefer keeping things in the markup.</p>"}},"pageContext":{"slug":"/2023-12-03-tailwind-css/"}},"staticQueryHashes":[]}