When is the ideal moment to utilize SASS extensions?

My current team uses SASS and I've noticed that we're extending very simple styles in our code. I fail to see the purpose of doing this. Am I overlooking something?

Below are some examples of styles from a _Common.scss file that is imported and utilized across other SASS files:

.visibility-hidden{visibility: hidden;}
.display-inline { display: inline; }
.display-inline-block { display: inline-block; }
.display-block { display: block; }
.display-none { display: none; }
.display-box { display: box; }

.float-left { float: left; }
.float-right { float: right; }
.clear-both { clear: both; }

.width-percent-100 { width: 100%; }
.width-percent-65 { width: 65%; }
.width-percent-50 { width: 50%; }
.width-percent-45 { width: 45%; }
.width-percent-40 { width: 40%; }
.width-percent-33 { width: 33%; }
.width-percent-30 { width: 30%; }
.width-percent-20 { width: 20%; }

.height-percent-100 { height: 100%; }

.cursor-pointer { cursor: pointer; }

.underline { text-decoration: underline; }
.text-decoration-none { text-decoration: none; }
.bold { font-weight: bold; }
.font-weight-normal { font-weight: normal; }
.text-align-center { text-align: center; }
.text-align-left { text-align: left; }
.text-align-right { text-align: right; }

.font-10 { font-size: 10px; }
.font-11 { font-size: 11px; }
.font-12 { font-size: 12px; }
.font-13 { font-size: 13px; }
.font-14 { font-size: 14px; }
.font-15 { font-size: 15px; }
.font-16 { font-size: 16px; }
.font-17 { font-size: 17px; }
.font-18 { font-size: 18px; }

.font-percent-65 { font-size: 65%; }
.font-percent-80 { font-size: 80%; }
.font-percent-90 { font-size: 90%; }
.font-percent-100 { font-size: 100%; }
.font-percent-110 { font-size: 110%; }
.font-percent-120 { font-size: 120%; }
.font-percent-130 { font-size: 130%; }
.font-percent-140 { font-size: 140%; }
.font-percent-150 { font-size: 150%; }
.font-percent-160 { font-size: 160%; }
.font-percent-170 { font-size: 170%; }
.font-percent-180 { font-size: 180%; }

For instance:

#CategoriesContainer
{
  ul{
    li{
        &:first-child{
          @extend .font-11;
        }
        a
        {
          @extend .font-11;
          @extend .text-decoration-none;
        }
    }
  }
}

Answer №1

When using the extend feature, it is important to only do so when you have a specific attribute that will be used multiple times. It doesn't make sense to extend a class with a single attribute that already includes the unit value in its name.

For a better explanation on when to extend, refer to the SASS reference guide

Consider the example of two classes:

.error {
  border: 1px #f00;
  background-color: #fdd;
}
.seriousError {
  border-width: 3px;
}

.error represents a basic style while .seriousError is meant for a clear indication of a serious error. Instead of using both classes in the HTML to combine styles, we can extend .seriousError with .error:

.seriousError {
  @extend .error;
  border-width: 3px;
}

By doing this, we avoid duplicating code in our Sass file while achieving the desired styles on the page.

Refer to the guide for more examples and best practices. Avoid extending classes with single attribute classes and refrain from explicitly stating values/attributes in the selector for better semantic coding.

Take a look at this post for insights on semantic coding and its importance in your development approach.

Answer №2

This code is unnecessarily bloated and not a recommended way to extend classes. One possible reason for using it could be to easily change properties like font size, but then the class naming becomes pointless. It would make more sense to use a class name like "small-font" instead.

It's important to consider the semantic meaning of class names and avoid overly specific or presentational ones. This usage of extending classes with a 1:1 mapping defeats the purpose of the @extend feature, which is meant to reduce the amount of CSS you write.

A better example of using @extend would be:

.media {
    padding:1em;
    border-color:blue;
    background-color:red;
    clear:left;
}

.my-media {
    @extend .media;
    background-color:green;
}

Answer №3

The Concept of Atomic CSS

Yahoo! has introduced the concept of Atomic CSS, which involves using simple CSS rules directly in your markup. Thierry Koblentz discusses this approach in a Smashing Magazine article, highlighting its benefits for large projects with inconsistent styles. Unlike OOCSS, which relies on reusable base styles for components, Atomic CSS allows for more flexibility but may require writing additional extension classes or overrides.

While Atomic CSS offers advantages, as noted by Wesley, it can pose challenges when making global style changes across a project, such as adjusting text sizes for specific selectors.

Experimenting with a variation of this technique in a sizable project, I've found it useful for handling one-off styles without embedding hard values directly in selectors. For example, consider the following CSS snippet (example fiddle):

_colors.scss

.text-white {
  color: $white;
}

.blue {
  @extend .text-white;
  background: $blue;
}

_effects.scss

.circle {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  text-align: center;
  line-height: 50px;
  font-size: 40px;
}

.matted {
  border: 4px solid $white;
}

.shadow {
  @include box-shadow(0 1px 4px 1px rgba($black, 0.25));
}

HTML:

<div class="blue matted circle shadow">?</div>

Considerations for Specificity

It's important to note that using Atomic CSS can lead to specificity issues, particularly when extending base-level classes with similar CSS properties. In the following example (fiddle), the border-radius might not render as expected due to conflicting CSS rules. If multiple atomic selectors reuse the same properties, conflicts can arise.

_colors.scss

.text-white {
  color: white;
}

.blue {
  @extend .text-white;
  background: royalblue;
}

_effects.scss

.squared-top {
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

.rounded {
  border-radius: 10px;
}

.circle {
  width: 50px;
  height: 50px;
  border-radius: 50%;
}

HTML:

<span class="circle blue rounded squared-top"></span>

Answer №4

By following this approach, you have the option to seamlessly integrate it into the HTML code. This seems to follow the OOCSS methodology, making it easier to extend the styling since it's already included in the CSS. While this method offers great flexibility, it also runs the risk of becoming disorganized if not managed properly.

Answer №5

The utilization of the extend option in this context is lacking. It is best suited for expanding classes with additional content, and in such situations, extend can prove to be quite beneficial. To learn more about extend and its various options, you can refer to this informative guide here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with positioning in IE11 when using `top:0` and `bottom:0` inside a table

Expanding on a previous inquiry of mine, I have created this interactive demo The demo features two columns on top of one column in smaller views, and 3 columns in a row in larger views. While the layout functions as intended, I noticed a lack of spacing ...

Ensure that the element is positioned above other elements, yet below the clickable area

Currently, I am working on developing a notification system that will display a small box in the top right corner of the screen. However, I am facing an issue where the area underneath the notification is not clickable. Is there a way to make this part o ...

Adding Multiple CSS Classes to an HTML Element using jQuery's addClass Feature

Take a look at this HTML code snippet: <html> <head> <style type="text/css"> tr.Class1 { background-color: Blue; } .Class2 { background-color: Red; } </style> </head> <body> < ...

Two elements occupy the rest of the vertical space

In the center, there is a div with content inside. I want the top and bottom divs to occupy the remaining space. Similar to this example <div class="container"> <div class="top"> </div> <div class="middle"> C ...

Is it possible to export CSS stylesheets for shadow DOM using @vanilla-extract/css?

I have been exploring the use of @vanilla-extract/css for styling in my React app. The style method in this library exports a className from the *.css.ts file, but I need inline styling to achieve Shadow DOM encapsulation. During my research, I came acros ...

What could be causing the issue: Unable to locate or read the file: ./styles-variables?

I'm currently following a tutorial on how to create responsive layouts with Bootstrap 4 and Angular 6. You can find the tutorial here. I've reached a point where I need to import styles-variables.scss in my styles file, but I keep encountering t ...

align video element to the right within a container div

I'm attempting to align a video element to the bottom right inside a div. Here is the code I have so far, which successfully aligns the video element to the bottom but not to the right side of the div container: video { } .border.rounded-0.border- ...

Cannot access mobile navigation due to expanded browser search bar

Greetings, all! Currently, as I am in the process of developing a website for my company, I have encountered an issue with the navigation menu, specifically on mobile devices, or more accurately, mobile browsers(most of them). The hamburger icon seems to ...

A step-by-step guide on making an animation series with html and css keyframes

I've been working on creating a captivating animation using HTML and CSS keyframes, but I seem to have hit a roadblock. Despite trying different rotations and transforms, I can't seem to achieve the desired effect depicted in the image below. The ...

Are Bootstrap Input groups inconsistent?

Hey there! I've been working on the sign-in example, but I seem to have hit a roadblock. In my local setup, the top image is what I see in my browser after running the code, while the desired layout that I found on the Bootstrap site is the one below ...

Struggling to target specific elements in CSS that are dynamically generated through JavaScript

After using JavaScript to generate some divs, I ended up with the following code: const container = document.querySelector('#container'); for(let i = 1; i < 17; i++) { var row = document.createElement('div'); row.id = 'r ...

I require the slideshow div to smoothly move elements down as it transitions between slides

I'm facing an issue with a jquery slideshow that contains quotes of varying lengths. The problem is, the longer quotes overlap with the content below them. I'm trying to figure out how to make the longer quotes push the rest of the page down, avo ...

Spread out elements equally into columns

I am facing a challenge with arranging items within a single div called .wrap. The goal is to place an unknown number of items (.thing) into four columns, stacked on top of each other. <div class="wrap"> <div class="thing"> thing1 </div ...

creating center-focused gradients in react native using LinearGradient

Hey there, I'm currently facing an issue with applying a box shadow to one of the buttons in my React Native application. Unfortunately, the box shadow property is not supported in Android. To work around this limitation, I am experimenting with creat ...

CSS selector for detecting elements with empty or whitespace-only content

A special selector called :empty allows us to target elements that are entirely devoid of content: <p></p> However, in some cases, the element may appear empty due to line breaks or white spaces: <p> </p> In Firefox, a workar ...

Dynamic HTML gallery that supports the addition of fresh images

Looking to showcase images from a local folder in a slideshow on a simple web page? The page should automatically update when new images are added or removed from the folder. I am not an expert and prefer a minimalist design with perhaps just a transitio ...

Show items in the sequence of clicking

Is there a way to display elements in the order they're clicked, rather than their order in the HTML using jQuery? For example: CSS code: .sq{ display:none; } HTML Code: <a href="#" id="1">A</a> <a href="#" id="2">B</a> ...

Design HTML dropdown menu

I have a sleek menu design with rounded buttons, and I am looking to extend the same style to the dropdown list. I have experimented with various approaches but there are two specific issues that I need assistance with: 1. Achieving rounded corners simil ...

Properly appears in Chrome, but experiences issues in Internet Explorer 11

The design of the website looks seamless in Chrome and Firefox, however, when I view it in Internet Explorer, I notice a significant gap on the right side of the first two pages. How can I fix this issue specifically for Internet Explorer? Should I use a m ...

Make changes to external CSS using HTML and JavaScript

Is it possible to dynamically change the value of a background in an external CSS file using JavaScript? Currently, all my pages are connected to a CSS file that controls the background for each page. I am attempting to modify the background by clicking a ...