6 tips to avoid headaches in SCSS
Tags: webdev, beginners, css, tailwindcss
Published: 2021-12-13 14:03:23

<!-- wp:heading -->
<h2>1. Never style elements</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This is probably the biggest cause of headaches, at least in my experience. It seems innocent enough to style a button or a heading - they're buttons and headings, of course they should look like buttons and headings.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// don't do this!
button {
  background-color: $color-btn;
  border-radius: 4px;
}

// nooooo!
h2 {
  font-size: 40px;
  font-weight: bold;
}</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>But!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>What if you have an element that should look like a link, but is really a button? Or if you have a button which needs to look different from all the others, such as with hamburger/pancake menus (there's a lot of debate on exactly which food item these look like, I think it's a pancake).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>What if your headings need a different size or style in a particular context?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In all these cases, you would have to manually un-style these elements in their own class. And because element styling wins specificity wars over classes, you would often need to use the <code>!important</code> rule - a huge no-no.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Instead, assign classes to your elements. This is much more expressive than styling raw elements as you can state the intention of the element through it's class. You can also reuse the class on different elements, such as with buttons and links.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// better
.button {}
.heading-2 {}</code></pre>
<!-- /wp:code -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// now you can reuse the classes!
&lt;button class="button"&gt;I am a button&lt;/&gt;
&lt;a class="button"&gt;I am link, but I look like a button&lt;/&gt;</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>If you need to style elements for a Rich Text Editor or WYSIWYG section, or anywhere else you might not be able to add classes directly, you can do so by nesting. By putting a <code>.rte</code> class on the container for these sections, you can scope your element styles to only target what you need to.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// _rte.scss

.rte {
  &amp; h2 {
    font-size: 28px;
    font-family: $font-headings;
    font-weight: bold;
    line-height: 1.3;
  }

  &amp; p {
    font-size: 16px;
    margin-bottom: 16px;
  }

  &amp; ul {
    // etc...
  }
}</code></pre>
<!-- /wp:code -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// article template

&lt;article class="rte"&gt;
  &lt;!-- content added by cms --&gt;
&lt;/article&gt;</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2>2. Strive to never use !important</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Your CSS should follow rules, and your classes shouldn't overstep each other. The more tangled up in the web of specificity and nested selectors your code becomes, the harder your codebase will be to work with. You'll end up adding <code>!important</code> to properties that really have no need to be, and the more of these rules you have in the first place the more you'll have to use it again to override these rules. It's a downward spiral of manually forcing specificity, and it has rippling consequences for your CSS.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are a few ways to avoid using !important[]: For one, keeping your CSS structure as flat as possible can avoid any specificity wars that could crop up. Nesting CSS, and using complex selectors to target classes in specific situations, are things many developers do without realising they are lining up headaches and delays for themselves in future.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>3. Use reset.css</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Cross-browser consistency is one of the major problems surrounding not only CSS, but also JS and even accessibility with ARIA. In CSS, we can lay out a level playing field using a reset.css file. This will remove any browser-specified styles, such as margins, font sizes, button styles etc. so every browser will show the same thing.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>reset.css is also great because we don't need to waste time overwriting default styles: If your &lt;button /&gt; needs to have a transparent background and no borders, we would normally need to specify these in our own CSS. This way, we know any CSS we are writing is <em>additive</em> instead of being <em>subtractive</em>.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>.button {
  // these properties wouldn't be necessary with reset.css
  background-color: transparent;
  border: none;
}</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Subtractive CSS, where we write styles to overwrite other styles, is generally a big no-no as it adds a level of confusion to what a class is doing. CSS classes, as with anything in programming, should be concise and to the point. If you find yourself writing subtractive or overwriting CSS, it may be worth tracing back to find what it's overwriting, and breaking down the original styles further using placeholders[link] or mixins[link].</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You could also use normalize.css. This will maintain a set of default page margins and other element-level styles across browsers. Personally, I prefer reset.css due to the fact that I want to control everything on a page without being interrupted by default styles - I don't want any page to have a margin on the body, for example, and writing code to undo these margins makes my code confusing. However, this might work better for you if you want some defaults.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>4. Use variables to stay DRY</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>DRY, meaning Don't Repeat Yourself, is a key concept to understand for any kind of programming. In SCSS, the best place to start with this is in using variables, placeholders and mixins. The most common of these you'll be using is variables.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's say you're looking at a design you need to create, and it has many colours across it. You can make your life much easier by assigning these colours to variables, perhaps with a kind of hierarchy which relates to the brand you're working with. There are lots of different ways to name variables, <a href="https://css-tricks.com/what-do-you-name-color-variables/">as outlined in this CSS Tricks article.</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Another great use of SCSS variables is naming the top level of components. Here, I use <code>$module</code> to store the name of the component and am then free to reuse it in other places, without needing to worry if I want to change the name of that component for some reason.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// we can change $module at any time in one place
$module: 'nav';

.#{$module} {
  &amp;__dropdown-link {
    position: relative;
  }

  &amp;__dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    display: hidden;
    
    .#{$module}__dropdown-link:hover &amp; {
      display: block;
    }
  }</code></pre>
<!-- /wp:code -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// above css applied to html, for reference

&lt;li class="nav__dropdown-link&gt;
  &lt;a&gt;Link title&lt;/a&gt;
  &lt;ul class="nav__dropdown"&gt;
    ...
  &lt;/ul&gt;
&lt;/li&gt;</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2>5. Use placeholders</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Placeholders are a great way to save time and file size with SCSS. They are essentially groups of properties which you can apply in a similar way to variables, and further build on top of them.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I've used placeholders on <a href="https://charliejoel.dev/">my own site</a> to help generate a set of paragraph styles using the <a href="https://grtcalculator.com/">Golden Ratio Typography Calculator</a> (check it out!) which downsize appropriately at different resolutions:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// these styles...

%t-1\/2 {
  font-size: 14px;
  line-height: 1.786;
}

%t-1 {
  font-size: 18px;
  line-height: 1.722;
}

%t-2 {
  font-size: 23px;
  line-height: 1.652;
}
</code></pre>
<!-- /wp:code -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// ...can be easily reused for different breakpoints
.t {
  &amp;-1 {
    @extend %t-1\/2;
    @screen lg { // @screen is a tailwind directive - these are just desktop-first breakpoints!
      @extend %t-1;
    }
  }
  &amp;-2 {
    @extend %t-1;
    @screen lg {
      @extend %t-2;
    }
  }
}</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>As well as needing to write less code, we are also building a sensible design system with our CSS to ensure consistency between elements and enforce a single source of truth.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>6. Don't set a max-width on &lt;main&gt;</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>More often than not, any content on a webpage has a certain <code>max-width</code> which it cannot exceed. This helps with readability and keeps the page content flush all the way down.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you're a beginner, it may be instinctive to set this <code>max-width</code> on an element that's quite high up in the hierarchy - the <code>&lt;main&gt;</code> element for instance, or even the <code>&lt;body&gt;</code>. On a page that has the same background color all the way down, this might be perfectly fine. Most webpages, however, are broken up by different sections that have a background color spanning the entire width of the page. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The issue you will run into by setting a max-width on the <code>&lt;main&gt;</code>, is that this background color will be limited by the max-width. Beyond this <code>max-width</code>, the background color will be white (or whatever color the <code>&lt;body&gt;</code> is set to).</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":95,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://api.charliejoel.dev/wp-content/uploads/2021/12/image-3-1024x449-1.png" alt="" class="wp-image-95"/><figcaption>Ugly whitespace!</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>You can avoid this by segregating this max-width effect into sections. To do so, you will need a parent element which takes on the background color you want, and a child element inside it that applies the max-width effect. Repeat for each section of the page, and you will end up with the desired effect.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// index.html

&lt;main&gt;
  &lt;section class="section section--bg-primary"&gt;
    &lt;div class="section__inner"&gt;
      &lt;h2&gt;Hello world!&lt;/h2&gt;
      &lt;p class="paragraph"&gt;
        Lorem ipsum dolor sit amet...
      &lt;/p&gt;
    &lt;/div&gt;
  &lt;/section&gt;
  &lt;section class="section section--bg-secondary"&gt;
    &lt;div class="section__inner"&gt;
      &lt;h2&gt;Hello again world!&lt;/h2&gt;
      &lt;p class="paragraph"&gt;
        Lorem ipsum dolor sit amet...
      &lt;/p&gt;
    &lt;/div&gt;
  &lt;/section&gt;
&lt;/main&gt;</code></pre>
<!-- /wp:code -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// layout/_section.scss

.section {
  // use the parent element to add padding
  padding: 48px 16px;

  &amp;--bg-primary {
    background-color: $color-primary;
    color: white;
  }
  
  &amp;--bg-secondary {
    background-color: $color-secondary;
    color: black;
  }

  &amp;__inner {
    // set the max-width on the inner element
    max-width: $max-w-page;
    width: 100%;
    margin: 0 auto;
  }
}</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Here it is - a layout with full-width background colors, padding and a max-width. You might want to set some of these values as variables or placeholders so you can repeat in other areas - for instance, a nav bar will often need the same effect, as will a footer.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":81,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://api.charliejoel.dev/wp-content/uploads/2021/12/image-4-1024x452.png" alt="" class="wp-image-81"/><figcaption>Lovely full-width background color</figcaption></figure>
<!-- /wp:image -->