<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Yasir Tobbileh - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Yasir Tobbileh - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 18 Jun 2026 05:24:35 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/YasirA113/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ What is PostCSS? How to Use Plugins to Automate CSS Tasks ]]>
                </title>
                <description>
                    <![CDATA[ PostCSS is a Node.js tool that transforms your styles using JavaScript plugins.   It generates more downloads per week on NPM than other CSS preprocessors like Sass, Less, and Stylus combined. PostCSS download trends comparing to other CSS preproces... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-is-postcss/</link>
                <guid isPermaLink="false">66bb52ff3c5ab240beb8cd73</guid>
                
                    <category>
                        <![CDATA[ automation ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Node.js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ PostCSS ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Yasir Tobbileh ]]>
                </dc:creator>
                <pubDate>Mon, 31 Jan 2022 17:29:16 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/01/postcss-1.PNG" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>PostCSS is a Node.js tool that transforms your styles using JavaScript plugins.  </p>
<p>It generates more downloads per week on <a target="_blank" href="https://www.npmtrends.com/less-vs-postcss-vs-sass-vs-stylus">NPM</a> than other CSS preprocessors like Sass, Less, and Stylus combined.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/01/trend.PNG" alt="Image" width="600" height="400" loading="lazy">
<em>PostCSS download trends comparing to other CSS preprocessors</em></p>
<h2 id="heading-in-this-article-we-will-discuss">In this article we will discuss:</h2>
<ul>
<li>What is PostCSS?</li>
<li>PostCSS features and advantages</li>
<li>Some popular PostCSS plugins</li>
<li>How to setup PostCSS configurations</li>
</ul>
<h1 id="heading-what-is-postcss">What is PostCSS?</h1>
<p>PostCSS is a JavaScript tool that transforms your CSS code into an abstract syntax tree (AST) and then provides an API (application programming interface) for analyzing and modifying it using JavaScript plugins.</p>
<p>PostCSS provides a large ecosystem of plugins to perform different functionalities like linting, minifying, inserting vendor prefixes, and many other things.</p>
<p>Despite its name, it is neither a post-processor nor a pre-processor, it is just a <strong>transpiler</strong> that turns a special PostCSS plugin syntax into a Vanilla CSS. You can think of it as the <a target="_blank" href="https://babeljs.io/docs/en/"><strong>Babel</strong></a> tool for CSS.</p>
<p>You can use PostCSS in conjunction with existing preprocessors like Sass, Less, and Stylus. Or you can use it as an alternative to all of them since it has all the required functionalities to be used alone.</p>
<p>You may have already been using PostCSS without knowing it. It's used in the popular Autoprefixer plugin which is used to automatically prepend vendor prefixes to CSS properties that require them.</p>
<p>PostCSS is also used by other technologies like Vite and Next.js, as well as the CSS framework <a target="_blank" href="https://tailwindcss.com/docs/using-with-preprocessors#using-post-css-as-your-preprocessor">TailwindCSS</a> which is a PostCSS plugin.</p>
<h1 id="heading-postcss-features-and-benefits">PostCSS Features and Benefits</h1>
<ul>
<li>PostCSS is fully customizable so you can use only the plugins and features you need for your application.</li>
<li>It also produces fast build times compared with other preprocessors.</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/01/1_WVCihSMXXK0xkCw2a3g8iQ.png" alt="Image" width="600" height="400" loading="lazy">
<em>Different build times for different CSS preprocessors comparing to PostCSS</em></p>
<ul>
<li>If you want, you can write your own custom plugins. And you can use it with regular CSS as well as alongside other preprocessors like Sass.</li>
</ul>
<p>PostCSS is all about plugins (on its own, it is simply an API). It has an ecosystem of 356 plugins (as of writing this article). Each plugin was created for a specific task.</p>
<p>You can navigate through the plugins using the <a target="_blank" href="https://github.com/postcss/postcss/blob/main/docs/plugins.md">plugin directory</a> on the official PostCSS GitHub page, or using this <a target="_blank" href="https://www.postcss.parts/">searchable catalog of PostCSS plugins</a>.</p>
<p>Before starting with the code, I highly recommend that you follow these steps:</p>
<ol>
<li>Download or fork the following <a target="_blank" href="https://github.com/adamA113/postcss-tutorial"><strong>postcss-tutorial</strong></a> repository to your machine and try to follow along. (Make sure to read the README.md file.)</li>
<li>Install the <a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=csstools.postcss"><strong>PostCSS Language Support</strong></a> <strong>plugin</strong> if you are using the Visual Studio Code editor, so your editor can recognize any new syntax and stop giving you errors (skip this step if you are using other code editors).</li>
</ol>
<h1 id="heading-popular-postcss-plugins">Popular PostCSS Plugins</h1>
<h3 id="heading-postcss-import">PostCSS Import</h3>
<p>One of the basic and most important plugins to use is <a target="_blank" href="https://github.com/postcss/postcss-import">postcss-import</a>. It lets us import CSS files into other files.</p>
<p>To check how to use this plugin go to <code>src/style.css</code> in the postcss-tutorial repository.</p>
<pre><code class="lang-css"><span class="hljs-keyword">@import</span> <span class="hljs-string">'./components/comp1.css'</span>;
<span class="hljs-keyword">@import</span> <span class="hljs-string">'./components/comp2.css'</span>;
</code></pre>
<p>You can see that it is very similar to the way that we use the @import method in Sass. </p>
<p><strong>Note:</strong> <code>postcss-import</code> is different than the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/@import">import rule</a> in native CSS. You should avoid the import rule in native CSS, since it can prevent stylesheets from being downloaded concurrently which affects the loading speed and performance.</p>
<p>The browser has to wait for every imported file to be loaded instead of being able to load all the CSS files at once.</p>
<h2 id="heading-autoprefixerhttpsgithubcompostcssautoprefixer"><a target="_blank" href="https://github.com/postcss/autoprefixer">Autoprefixer</a></h2>
<p>This is one of the most popular PostCSS plugins. You use it to parse and add vendor prefixes like <code>-webkit</code>, <code>-moz</code>, and <code>-ms</code> to CSS rules using values from the <a target="_blank" href="https://caniuse.com/">Can I Use</a> website.</p>
<p>We use the Can I Use website to see which browsers support a CSS feature with their versions. This helps us determine whether we need to add a prefix or not.</p>
<p>Autoprefixer uses <a target="_blank" href="https://github.com/browserslist/browserslist">Browserslist</a>, so you can specify the browsers you want to target in your project with queries.</p>
<p>We can configure our Browserslist in the package.json file using a “browserslist” key:</p>
<pre><code class="lang-json"> <span class="hljs-string">"browserslist"</span>: [ 
     <span class="hljs-string">"defaults"</span>  
 ]
</code></pre>
<p>The <code>defaults</code> query above is a short version of:</p>
<ul>
<li><code>&gt; 0.5%</code> browsers that have at least 0.5% global usage.</li>
<li><code>last 2 versions</code> the last 2 versions for <em>each</em> browser,</li>
<li><code>Firefox ESR</code> the latest <a target="_blank" href="https://support.mozilla.org/en-US/kb/choosing-firefox-update-channel">Firefox Extended Support Release</a>., </li>
<li><code>not dead</code> browsers that received official support or updates for the last 24 months.</li>
</ul>
<p>Or we can use a <code>.browserslistrc</code> file in the root directory, and inside it we type our configurations.</p>
<pre><code>defaults
</code></pre><p>To test this plugin, we have added some CSS rules that need some prefixes in the <code>src/components/comp2.css</code> file:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">label</span> {
  <span class="hljs-attribute">user-select</span>: none;
}

<span class="hljs-selector-pseudo">::selection</span> {
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">background</span>: blue;
}

<span class="hljs-selector-pseudo">::placeholder</span> {
  <span class="hljs-attribute">color</span>: gray;
}
</code></pre>
<p>Based on our previous "browserslist" settings, the final output will be:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">label</span> {
  <span class="hljs-attribute">-webkit-user-select</span>: none;
     <span class="hljs-attribute">-moz-user-select</span>: none;
      <span class="hljs-attribute">-ms-user-select</span>: none;
          <span class="hljs-attribute">user-select</span>: none;
}

<span class="hljs-selector-pseudo">::-moz-selection</span> {
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">background</span>: blue;
}

<span class="hljs-selector-pseudo">::selection</span> {
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">background</span>: blue;
}

<span class="hljs-selector-pseudo">::-moz-placeholder</span> {
  <span class="hljs-attribute">color</span>: gray;
}

<span class="hljs-selector-pseudo">:-ms-input-placeholder</span> {
  <span class="hljs-attribute">color</span>: gray;
}

<span class="hljs-selector-pseudo">::placeholder</span> {
  <span class="hljs-attribute">color</span>: gray;
}
</code></pre>
<h2 id="heading-postcss-preset-envhttpsgithubcomcsstoolspostcss-pluginstreemainplugin-packspostcss-preset-env"><a target="_blank" href="https://github.com/csstools/postcss-plugins/tree/main/plugin-packs/postcss-preset-env">PostCSS Preset Env</a></h2>
<p>This plugin enables us to use modern CSS (like nesting and custom media queries) in our code, by converting it to Vanilla CSS which can be understood by browsers.</p>
<p>It has a <code>stage</code> option which determines which CSS features to polyfill based upon their stability in the process of becoming implemented as a web standard.</p>
<p>The <code>stage</code> can be 0 (experimental) to 4 (stable), or false. Stage 2 is the default.</p>
<p>For nesting, we need to use stage 1.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">module</span>.exports = {
    <span class="hljs-attr">plugins</span>: [
        <span class="hljs-built_in">require</span>(<span class="hljs-string">'postcss-preset-env'</span>)({ <span class="hljs-attr">stage</span>: <span class="hljs-number">1</span> })
    ],
}
</code></pre>
<p>Also, the preset-env plugin includes by default the <a target="_blank" href="https://github.com/postcss/autoprefixer">Autoprefixer</a> plugin and the <code>[browsers](https://github.com/csstools/postcss-preset-env#browsers)</code> option will be passed to it automatically.</p>
<p>In the <code>src/components/comp1.css</code> we have used the nesting feature pretty similarly to what we have in the Sass preprocessor:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">article</span> {
    <span class="hljs-attribute">background</span>: purple;

    &amp; .title {
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">6rem</span>;
    }

    &amp; <span class="hljs-selector-tag">li</span> {
        <span class="hljs-attribute">list-style-type</span>: none;
    }
}
</code></pre>
<p>Since nesting is not supported in today's CSS, we need to convert the code above so that web browsers can understand it.</p>
<p>The following code is the final result:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">article</span> {
    <span class="hljs-attribute">background</span>: purple
}

<span class="hljs-selector-tag">article</span> <span class="hljs-selector-class">.title</span> {
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">6rem</span>;
    }

<span class="hljs-selector-tag">article</span> <span class="hljs-selector-tag">li</span> {
        <span class="hljs-attribute">list-style-type</span>: none;
    }
</code></pre>
<h2 id="heading-postcss-nestedhttpsgithubcompostcsspostcss-nested"><a target="_blank" href="https://github.com/postcss/postcss-nested">PostCSS Nested</a></h2>
<p>If we want <strong>only</strong> to use the nesting feature, then this plugin is the perfect choice as it produce the same result as the previous plugin.</p>
<h2 id="heading-postcss-mixinshttpsgithubcompostcsspostcss-mixins"><a target="_blank" href="https://github.com/postcss/postcss-mixins">PostCSS Mixins</a></h2>
<p>Mixins allow you to define styles that can be re-used throughout your code.</p>
<p>In our code we used some mixins in the <code>src/components/comp1.css</code> file.</p>
<p>We first define the mixin using the keyword <code>@defin-mixin</code> followed by the mixin name. Then we use it by writing the name after the <code>@mixin</code> keyword. </p>
<pre><code class="lang-css"><span class="hljs-keyword">@define-mixin</span> reset-list {
  <span class="hljs-selector-tag">margin</span>: 0;
  <span class="hljs-selector-tag">padding</span>: 0;
  <span class="hljs-selector-tag">list-style</span>: <span class="hljs-selector-tag">none</span>;
}

<span class="hljs-selector-tag">nav</span> <span class="hljs-selector-tag">ul</span> {
  @mixin reset-list;
}
</code></pre>
<p>Mixins are not supported in today's CSS, so they need to be compiled to Vanilla CSS.</p>
<p>The final code will be:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">nav</span> <span class="hljs-selector-tag">ul</span> {
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">list-style</span>: none;
}
</code></pre>
<h2 id="heading-stylelinthttpsstylelintio"><a target="_blank" href="https://stylelint.io/">Stylelint</a></h2>
<p>This is a CSS linter that helps us avoid errors in our code before they break our User Interface (UI).</p>
<p>It can be configured in <a target="_blank" href="https://stylelint.io/user-guide/configure">multiple ways</a>. One of them through using a <code>stylelint</code> property in <code>package.json</code> as follows:</p>
<pre><code class="lang-json"><span class="hljs-string">"stylelint"</span>: {
    <span class="hljs-attr">"rules"</span>: {
      <span class="hljs-attr">"color-no-invalid-hex"</span>: <span class="hljs-literal">true</span>
    }
  }
</code></pre>
<p>Inside the <code>stylelint</code> we have multiple options to configure. Here we will only cover the "rules" option which lets you define are the rules that the linter should looks for and gives errors when they are not followed.</p>
<p>The <code>"color-no-invalid-hex": true</code> rules give an error in the terminal if an invalid hex value is provided as a color for a given CSS property.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/01/invalid-1.PNG" alt="Image" width="600" height="400" loading="lazy">
<em>Styleint linter gives an error as an invalid hex value provided as color at line 11.</em></p>
<p><strong>Note:</strong> No rules are turned on by default and there are no default values. You must explicitly configure each rule to turn it on.</p>
<h2 id="heading-cssnanohttpscssnanocodocsintroduction"><a target="_blank" href="https://cssnano.co/docs/Introduction/">Cssnano</a></h2>
<p>This is a minifier used to reduce the final CSS file size as much as possible so your code is ready for a production environment.</p>
<p>Some parts will be altered to reduce the size as much as possible, like removing unnecessary spaces, new lines, renaming values and variables, selectors merged together, and so on.</p>
<p>So here is our final CSS code before the minification process:</p>
<pre><code class="lang-css">* {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
}

<span class="hljs-selector-tag">article</span> {
    <span class="hljs-attribute">background</span>: purple
}

<span class="hljs-selector-tag">article</span> <span class="hljs-selector-class">.title</span> {
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">6rem</span>;
    }

<span class="hljs-selector-tag">article</span> <span class="hljs-selector-tag">li</span> {
        <span class="hljs-attribute">list-style-type</span>: none;
    }

<span class="hljs-selector-tag">nav</span> <span class="hljs-selector-tag">ul</span> {
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">list-style</span>: none;
}

<span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-family</span>: sans-serif, Calibri;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
}

<span class="hljs-selector-tag">label</span> {
  <span class="hljs-attribute">-webkit-user-select</span>: none;
     <span class="hljs-attribute">-moz-user-select</span>: none;
      <span class="hljs-attribute">-ms-user-select</span>: none;
          <span class="hljs-attribute">user-select</span>: none;
}

<span class="hljs-selector-pseudo">::-moz-selection</span> {
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">background</span>: blue;
}

<span class="hljs-selector-pseudo">::selection</span> {
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">background</span>: blue;
}

<span class="hljs-selector-pseudo">::-moz-placeholder</span> {
  <span class="hljs-attribute">color</span>: gray;
}

<span class="hljs-selector-pseudo">:-ms-input-placeholder</span> {
  <span class="hljs-attribute">color</span>: gray;
}

<span class="hljs-selector-pseudo">::placeholder</span> {
  <span class="hljs-attribute">color</span>: gray;
}
</code></pre>
<p>After the minification process, our final CSS code that is ready for the production environment will be like this:</p>
<pre><code class="lang-css">*{<span class="hljs-attribute">margin</span>:<span class="hljs-number">0</span>;<span class="hljs-attribute">padding</span>:<span class="hljs-number">0</span>}<span class="hljs-selector-tag">article</span>{<span class="hljs-attribute">background</span>:purple}<span class="hljs-selector-tag">article</span> <span class="hljs-selector-class">.title</span>{<span class="hljs-attribute">font-size</span>:<span class="hljs-number">6rem</span>}<span class="hljs-selector-tag">article</span> <span class="hljs-selector-tag">li</span>{<span class="hljs-attribute">list-style-type</span>:none}<span class="hljs-selector-tag">nav</span> <span class="hljs-selector-tag">ul</span>{<span class="hljs-attribute">list-style</span>:none;<span class="hljs-attribute">margin</span>:<span class="hljs-number">0</span>;<span class="hljs-attribute">padding</span>:<span class="hljs-number">0</span>}<span class="hljs-selector-tag">body</span>{<span class="hljs-attribute">font-family</span>:sans-serif,Calibri;<span class="hljs-attribute">font-size</span>:<span class="hljs-number">16px</span>}<span class="hljs-selector-tag">label</span>{<span class="hljs-attribute">-webkit-user-select</span>:none;<span class="hljs-attribute">-moz-user-select</span>:none;<span class="hljs-attribute">-ms-user-select</span>:none;<span class="hljs-attribute">user-select</span>:none}<span class="hljs-selector-pseudo">::-moz-selection</span>{<span class="hljs-attribute">background</span>:blue;<span class="hljs-attribute">color</span>:<span class="hljs-number">#fff</span>}<span class="hljs-selector-pseudo">::selection</span>{<span class="hljs-attribute">background</span>:blue;<span class="hljs-attribute">color</span>:<span class="hljs-number">#fff</span>}<span class="hljs-selector-pseudo">::-moz-placeholder</span>{<span class="hljs-attribute">color</span>:gray}<span class="hljs-selector-pseudo">:-ms-input-placeholder</span>{<span class="hljs-attribute">color</span>:gray}<span class="hljs-selector-pseudo">::placeholder</span>{<span class="hljs-attribute">color</span>:gray}
</code></pre>
<h2 id="heading-postcss-normalizehttpsgithubcomcsstoolspostcss-normalize"><a target="_blank" href="https://github.com/csstools/postcss-normalize">PostCSS Normalize</a></h2>
<p>This plugin lets you use some parts of the popular library <a target="_blank" href="https://github.com/csstools/normalize.css">normalize.css</a> or <a target="_blank" href="https://github.com/csstools/sanitize.css">sanitize.css</a>.</p>
<p>These CSS libraries provide consistent, cross-browser default styling of HTML elements, also they correct bugs and common browser inconsistencies.</p>
<p>This plugin depends on the values you provides for the <code>"**browserslist"**</code> to show the correct styles for the HTML elements. Here is an <a target="_blank" href="https://github.com/csstools/postcss-normalize#examples">example</a> of that.</p>
<h1 id="heading-how-to-set-up-postcss">How to Set Up PostCSS</h1>
<p>To start using PostCSS, we need first to install it and its <a target="_blank" href="https://github.com/postcss/postcss-cli">command-line interface</a> (CLI) globally using this command:</p>
<pre><code class="lang-bash">npm i -g postcss-cli
</code></pre>
<ul>
<li><code>-g</code> to download it globally.</li>
</ul>
<p>Then install PostCSS locally using the following command:</p>
<pre><code class="lang-bash">npm i -D postcss
</code></pre>
<ul>
<li><code>-D</code> is short for <code>--save-dev</code> to save the installed packages as dev dependencies.</li>
</ul>
<p>To begin using PostCSS, we need to have at least one plugin downloaded.</p>
<p>If you are following along using the <a target="_blank" href="https://github.com/adamA113/postcss-tutorial">postcss-tutorial</a> repo, you can simply run <code>npm install</code> to download all the packages and dependencies.</p>
<h2 id="heading-setup-postcss-using-the-postcss-cli"><strong>Setup PostCSS Using the PostCSS CLI</strong></h2>
<p>The General Syntax for the command that needs to be run in the terminal is:</p>
<pre><code> postcss [input.css] [OPTIONS] [-o|--output output.css] [--watch|-w]
 postcss &lt;input.css&gt; [OPTIONS] --dir &lt;output-directory&gt; [--watch|-w]
</code></pre><p>We can run the following command directly in the terminal:</p>
<pre><code class="lang-bash">postcss src/style.css --use postcss-import --dir public --watch
</code></pre>
<p>The <code>--use</code> option lists the plugins we're using.</p>
<p>The <code>--watch</code> option watches the files for any changes and recompiles them.</p>
<h2 id="heading-setup-postcss-through-npm-scripts-in-the-packagejson-file"><strong>Setup PostCSS Through NPM scripts in the package.json File</strong></h2>
<p>Inside the package.json file in the "scripts", we need to type the following:</p>
<pre><code class="lang-json"><span class="hljs-string">"postcss:watch"</span>: <span class="hljs-string">"postcss src/style.css --use postcss-import 
--dir public --watch"</span>
</code></pre>
<p>The above command will create a new directory called 'public' which contains our final Vanilla CSS file, which has the same name as the source file (style.css).</p>
<p>If we want the output file to have a different name than the source file name, we need to replace <code>--dir public</code> with <code>-o public/&lt;file-name&gt;</code>.</p>
<p>Like for example: <code>-o public/main.css</code>.</p>
<p>We can configure our command to run in PostCSS CLI with different <a target="_blank" href="https://github.com/postcss/postcss-cli">options</a> to get our desired result.</p>
<p>Now to run the command above, we type <code>npm run &lt;command name&gt;</code> in our terminal. (our <code>&lt;command name&gt;</code> is <strong>postcss:watch</strong>, you can pick any name you want).</p>
<p>As our project gets bigger, we are more likely to add more plugins. For every plugin used, we need to write its name down after the <code>--use</code> keyword in the command above which makes it incredibly long and not a good practice.</p>
<p>The alternative solution is to create a postcss.config.js file.</p>
<h2 id="heading-setup-postcss-by-setting-up-a-postcss-config-file">Setup PostCSS by Setting Up a PostCSS Config File</h2>
<p>In the root directory of your project, create a file and name it <strong>postcss.config.js</strong>.</p>
<p>The code inside it will look like this:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">module</span>.exports = {
    <span class="hljs-attr">plugins</span>: [
         <span class="hljs-built_in">require</span>(<span class="hljs-string">'postcss-import'</span>),
        <span class="hljs-built_in">require</span>(<span class="hljs-string">'postcss-mixins'</span>),
        <span class="hljs-built_in">require</span>(<span class="hljs-string">"stylelint"</span>),
        <span class="hljs-built_in">require</span>(<span class="hljs-string">'postcss-preset-env'</span>)({ <span class="hljs-attr">stage</span>: <span class="hljs-number">1</span> }),
        <span class="hljs-built_in">require</span>(<span class="hljs-string">'cssnano'</span>),
    ],
}
</code></pre>
<p>Inside the plugins array, we add our plugins.</p>
<p><strong>Note:</strong> It is very important to add the postcss-import plugin at the top of our list since it is required by the documentation.</p>
<p>The command that runs PostCSS in our package.json file needs to be changed to:</p>
<pre><code class="lang-json"><span class="hljs-string">"postcss:watch"</span>: <span class="hljs-string">"postcss src/style.css --dir public --watch"</span>
</code></pre>
<p>As you can see, the only change required is to remove the <code>--use</code> option since the list of our plugins is mentioned is a separate file now.</p>
<h2 id="heading-setup-postcss-by-using-task-runners-or-module-bundlers"><strong>Setup PostCSS by Using Task Runners (or M</strong>odule Bundlers)</h2>
<p>PostCSS can be set to work with various task runners like <a target="_blank" href="https://github.com/postcss/gulp-postcss">Gulp</a>, <a target="_blank" href="https://github.com/C-Lodder/grunt-postcss">Grunt</a>, and module bundlers like <a target="_blank" href="https://github.com/egoist/rollup-plugin-postcss">Rollup</a> and <a target="_blank" href="https://github.com/webpack-contrib/postcss-loader">Webpack</a>.</p>
<p>In this section, we'll see how to set up Grunt for PostCSS.</p>
<p>First, we need to install grunt locally into the “dev” dependencies:</p>
<pre><code class="lang-bash">npm i -D grunt
</code></pre>
<p>And then install grunt-cli globally:</p>
<pre><code class="lang-bash">npm install -g grunt-cli
</code></pre>
<p>Now we need to create a file in the root of our project and name it <strong>Gruntfile.js</strong>.</p>
<p>Then we need to install a specific plugin <a target="_blank" href="https://www.npmjs.com/package/@lodder/grunt-postcss">@lodder/grunt-postcss</a> to let us run PostCSS using Grunt through the following command:</p>
<pre><code class="lang-bash">npm i -D @lodder/grunt-postcss
</code></pre>
<p>Inside the <code>initCnfig</code> function we set up our PostCSS configuration.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">grunt</span>) </span>{

    grunt.initConfig({
        <span class="hljs-attr">postcss</span>: {
            <span class="hljs-attr">options</span>: {
                <span class="hljs-attr">processors</span>: [
                    <span class="hljs-built_in">require</span>(<span class="hljs-string">'postcss-import'</span>)(),
                    <span class="hljs-built_in">require</span>(<span class="hljs-string">'postcss-mixins'</span>),
                    <span class="hljs-built_in">require</span>(<span class="hljs-string">"stylelint"</span>),
                    <span class="hljs-built_in">require</span>(<span class="hljs-string">'postcss-preset-env'</span>)({ <span class="hljs-attr">stage</span>: <span class="hljs-number">1</span> }),
                    <span class="hljs-built_in">require</span>(<span class="hljs-string">'cssnano'</span>)(),
                ]
            },
            <span class="hljs-attr">dist</span>: {
                <span class="hljs-attr">src</span>: <span class="hljs-string">'src/style.css'</span>,
                <span class="hljs-attr">dest</span>: <span class="hljs-string">'public/style.css'</span>
            }
        }
    })

    grunt.loadNpmTasks(<span class="hljs-string">'@lodder/grunt-postcss'</span>);
}
</code></pre>
<p>Here we will stick to the basic minimum to run PostCSS, which is:</p>
<ul>
<li>Calling our plugins inside the <code>processors</code> array.</li>
<li>Setting up the source file and destination file in the <code>dist</code> object.</li>
</ul>
<p>For more configuration, you can always check out the official documentation for the <a target="_blank" href="https://github.com/C-Lodder/grunt-postcss">@lodder/grunt-postcss</a>.</p>
<p>To finish our configuration, we need to load our plugin using the <code>grunt.loadNpmTasks</code> method.</p>
<p>Finally to run our Grunt task we type:</p>
<pre><code class="lang-bash">grunt postcss
</code></pre>
<h1 id="heading-conclusion"><strong>Conclusion</strong></h1>
<p>PostCSS has been out there since 2015, and it is very popular among CSS preprocessors.</p>
<p>You can use it as a stand-alone tool or in conjunction with other existing preprocessors.</p>
<p>When you use it and how (stand-alone or in conjunction) depends on your project needs.</p>
<p>Now it is your time to go and discover the wide variety of plugins it offers and start playing around with it. Happy Coding :)</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
