Skip to content

Instantly share code, notes, and snippets.

@Charlie-robin
Last active June 4, 2025 15:08
Show Gist options
  • Save Charlie-robin/724a65bfa090f1ce4e4e49cd77dc8735 to your computer and use it in GitHub Desktop.
Save Charlie-robin/724a65bfa090f1ce4e4e49cd77dc8735 to your computer and use it in GitHub Desktop.
Setting up SASS & Using post-css to auto prefix your css

Setting up SASS/SCSS & Prefixing with POSTCSS

Hey 👋,

There are two parts to this gist and the first is independant.

For the second part you need to have completed the first part.

You do not need both just if you want them they are below.

Setting up SASS/SCSS

In your project in the CLI follow these steps:

  1. Create a package.json
  • npm init or npm init -y for a blank package.json
  1. Add the SASS package as a devDependancy
  • npm install sass --save-dev
  1. Update your scripts in the package.json. You have different options to choose from.
  • "compile:scss": "sass scss/main.scss main.css" will compile the scss file
  • "watch:scss": "sass --watch scss/main.scss main.css" will watch the file and compile everytime it is saved.
  • scss/main.scss is the path to the scss file you may have to change it based on your project.
  • main.css is the output file you may have to change it based on your project.
  1. Run your new scripts based on what you want to do.
  • npm run compile:scss
  • npm run watch:scss

Example package.json

{
  "name": "Demo",
  "version": "1.0.0",
  "description": "Demoing SASS",
  "scripts": {
    "watch:scss": "sass --watch scss/main.scss main.css",
    "compile:scss": "sass scss/main.scss main.css"
  },
  "author": "Charlie Richardson",
  "license": "ISC",
  "devDependencies": {
    "sass": "^1.39.0"
  }
}

Setting up post-css to autoprefix your compiled CSS file

This will prefix your css to work across different browsers from the last 10 versions of each.

  1. Add the following devDependencies
  • npm install autoprefixer npm-run-all postcss postcss-cli --save-dev
  1. Add a prefix script to your package.json
  • "prefix:css": "postcss main.css --use autoprefixer -r"
  • main.css is the target file or path to the target file you may have to change it based on your project.
  • -r rewrites the file if you have your unprefixed css in main.css and run the script it will rewrite the file with the prefixed css
  1. Add a build script to your package.json
  • "build:css": "npm-run-all compile:scss prefix:css"
  • This script will compile your scss into css and then prefix it.
  1. Add this key and value to your package.json
  • "browserslist": "last 10 versions"

You will want to run this script this before you add commit and push changes to your repo. You don't need to do it every time just so the css on github has been prefixed.

Example package.json

{
  "name": "Demo",
  "version": "1.0.0",
  "description": "Demoing sass & postcss",
  "browserslist": "last 10 versions",
  "scripts": {
    "watch:scss": "sass --watch scss/main.scss main.css",
    "compile:scss": "sass scss/main.scss main.css",
    "prefix:css": "postcss main.css --use autoprefixer -r",
    "build:css": "npm-run-all compile:scss prefix:css"
  },
  "author": "Charlie Richardson",
  "license": "ISC",
  "devDependencies": {
    "autoprefixer": "^10.3.4",
    "npm-run-all": "^4.1.5",
    "postcss": "^8.3.6",
    "postcss-cli": "^8.3.1",
    "sass": "^1.39.0"
  }
}

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment