Basic Sass Trick

22 / Jun / 2023 by Eram Fatima 0 comments

basic sass trick

Sass (short for “Syntactically Awesome Style Sheets”) is a popular CSS preprocessor that enhances traditional CSS with new features and functionality. It offers a more efficient and powerful way to write CSS, making it easier to manage and maintain complex stylesheets.

Learning Sass is a great way to optimize your website’s CSS and there are many preprocessor options available. However, Sass is often the first step because it offers countless ways to optimize your CSS file. In this blog, we’ll explore some of the basics of Sass and how you can use them in your website, assuming you know how to set it up.

If you’re new to Sass, there are plenty of resources available online that can guide you through the setup process. Once you’re up and running, you can start exploring the many interesting things you can do with Sass to optimize your website’s CSS. From nesting selectors and variables to mixins and functions, Sass offers a wide range of powerful features that can help you write more efficient and maintainable CSS.

You can always visit this site to set up sass into your project.

Let’s Find out some interesting things which we can do in SASS to optimize the website.

Nesting: Sass allows you to nest selectors, which can help improve your code’s readability and organization. For example:

.container{
    width: 100%;
    .container_inner{
        overflow: hidden;
        .container_inner-one{
            height: 100%;
        }
    }
}

Variables

Sass provides a powerful variable feature that allows you to define reusable values, such as colors, font sizes, and spacing. Using variables can help to simplify your code and make it easier to update in the future.

By defining a variable for a value that you use multiple times in your code, you can change the value of that variable in a single place and have it updated throughout your codebase. For example, you could define a variable for your primary brand color and then use that variable wherever that color is needed throughout your stylesheet. This not only simplifies your code but also ensures consistency in your design.

In addition to simplifying your code and improving consistency, variables can also make it easier to work with large, complex stylesheets. By defining variables for common values, you can easily adjust the values of those variables throughout your codebase without having to manually change each instance of the value.

Overall, Sass variables are a powerful tool for simplifying your code, improving consistency, and making your stylesheets more modular and easier to work with over time. Using variables effectively saves time and effort, reduces errors, and ensures that your design remains consistent and up-to-date.

 

$font-base:'Montserrat', sans-serif;
$base-color: #000000;
$link-color: #DB7C65;
body{
    font-family: $font-base;
    color: $base-color;
    background-color: $link-color;
}

Mixins:

Mixins are a powerful feature in CSS that allows you to reuse blocks of code across your site. By creating reusable code blocks, you can significantly reduce code duplication, improve the maintainability of your code, and make your stylesheets more modular.

With mixins, you can define a block of CSS code once and then reuse it throughout your codebase by calling the mixin wherever you need it. This can help reduce the amount of code you need to write and make it easier to update styles globally.

For example, you might create a mixin that defines a set of styles for a button. You could then use this mixin throughout your site whenever you need to style a button, rather than writing out the same styles repeatedly. This not only saves time and effort but also helps to ensure consistency across your site.

Overall, mixins are a powerful tool in CSS that can help you write more efficient, modular, and maintainable code. Using mixins effectively simplifies your codebase, reduces duplication, and makes it easier to manage your stylesheets over time.

@mixin heading2($color){
    font-size: 26px;
    font-weight: 500;
    color: $color;
}
h1{
    @include heading2(#fff);
}
h2{
    @include heading2(#000);
}
h3{
    @include heading2(#ddd);
}

Extend: Sass allows you to extend one selector from another, which can help to reduce duplication and simplify your code. For example:

.button {
  display: inline-block;
  padding: 10px 20px;
  background-color: #007bff;
  color: #fff;
  text-decoration: none;
}
.button-primary {
  @extend .button;
}
.button-secondary {
  @extend .button;
  background-color: #6c757d;
}


Mathematical Operators

Sass provides several mathematical operators (+, -, *, /, %) that you can use to perform calculations in your CSS code. For example:

.container {
  width: 100% / 3;
}
.button {
  padding: 10px 20px;
  font-size: 16px;
  line-height: 1.5em;
  margin-bottom: 20px;
  &--large {
    font-size: 20px;
    line-height: 1.2em;
    margin-bottom: 30px;
  }
}

Breakpoints in Sass can be useful for creating responsive designs that adapt to different screen sizes. Here’s an example of how to define breakpoints using Sass variables:

// Define breakpoints
$mobile: 768px;
$tab: 992px;
$desktop: 1200px;
// Define media queries using mixins

@mixin media-mobile {
  @media (min-width: $breakpoint-small) {
    @content;
  }
}

@mixin media-tab{
  @media (min-width: $breakpoint-medium) {
    @content;
  }
}

@mixin media-desktop{
  @media (min-width: $breakpoint-large) {
    @content;
  }
}

.heading {
    @include media-desktop(){
        font-size: 16px;
      }
    @include media-tab{
      font-size: 14px;
    }
    @include media-mobile() {
        font-size: 12px;
    }
}

Control Directives: Sass provides several control directives, such as @if, @for, and @each, which can help you to write more powerful and flexible code. For example:

@mixin heading($font) {
    font-weight: 500;
    color: #007bff;
    display: block;
    @if $font == 'large' {
        font-size: 32px;
    }
    @else if $font == 'medium' {
        font-size: 24px
    }
  }
  .heading1 {
    @include heading("large");
  }
  .heading2 {
   @include heading("medium");
}

These are some basic examples of SASS. If one wants to learn some basics of it, they can always have a look into this irrespective of backend or frontend developers.

For more advanced learning, one can always visit sass website.
Check out our other blog posts for more insights.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *