These SCSS Mixins are useful when coding Front-end

Tram Ho

Hello everyone, as on the title, I will introduce you the mixins that I like the other Front-end dev most commonly used in a project.

There are many places that recommend mixins to solve prefix problems for older browsers, but I personally find it a bit redundant, because today we often use PostCSS or Autoprefixer to add them ourselves. So I will not mention mixins like that anymore.

Ok, that’s long enough, I’ll get to the main point right away ?

1. Responsive

This is the most used mixin with me, used to code responsive websites (Mobile first).

Here I divided the grid of Bootstrap 4, respectively 320, 576, 768, 992, 1200 and improved the 1600 milestone for larger designs. How to use:

Pretty simple, right ? Now I’m going to add the second most important thing that almost every project uses

2. Gutter (Only applicable to projects using Bootstrap)

This is the code for you to adjust the distance between the columns in Bootstrap. Suppose you have 1 row of products consisting of 4 products. The default bootstrap is 30px between products. What if you want to be 50px away? Of course you can customize in the core of bootstrap, but that will affect the whole Grid system while you only want this row to be 50px away. So we will need this mixin:

Usage as follows: Assuming you want 50px, we will declare the class in scss to reuse many places.

SCSS

HTML

Note: You may wonder why my mixins have the @if and @else paragraphs that look redundant. The reason is because if you notice gutter is greater than 30, when going down to the mobile screen, the total padding 2 on the left and right of the column will be larger than the default of the bootstrap container (30px), resulting in a horizontal scroll bar, which makes the aesthetics less responsive . So, for gutter> 30px, we only apply for large screen

3. Placeholder

This is the code so we can customize the input’s placeholder. Note that this code applies only to SCSS version 3.4

How to use:

Result:

4. Gradient

This is a mixin to add a background gradient to the DOM. We have $ start-color: start color, $ end-color: end color, $ orientation: The direction of the gradient. Orientation can be vertical (vertical), horizontal (horizontal) or left nothing, it will be a circular gradient.

How to use:

Okay, so I introduced 4 Mixins that I see every project I need to use. Thank you for reading, if there are any other useful mixins, you can comment to let me know ?

Share the news now

Source : Viblo