<?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[ CSS Modules - 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[ CSS Modules - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 11 Jun 2026 11:18:47 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/css-modules/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Use Sass with CSS Modules in Next.js ]]>
                </title>
                <description>
                    <![CDATA[ Next.js gives us CSS Modules by default, providing benefits like scoped styles and focused development in our app. How can we give our Next.js CSS superpowers with Sass? What are CSS Modules? What is Sass? What are we going to build? Step 0: Creatin... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-sass-with-css-modules-in-next-js/</link>
                <guid isPermaLink="false">66b8e3790a89d796f29a16f7</guid>
                
                    <category>
                        <![CDATA[ CSS Modules ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Next.js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Sass ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Colby Fayock ]]>
                </dc:creator>
                <pubDate>Thu, 07 Jan 2021 17:31:14 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/01/Article.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Next.js gives us CSS Modules by default, providing benefits like scoped styles and focused development in our app. How can we give our Next.js CSS superpowers with Sass?</p>
<ul>
<li><a class="post-section-overview" href="#heading-what-are-css-modules">What are CSS Modules?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-sass">What is Sass?</a></li>
<li><a class="post-section-overview" href="#heading-what-are-we-going-to-build">What are we going to build?</a></li>
<li><a class="post-section-overview" href="#heading-step-0-creating-a-new-nextjs-app">Step 0: Creating a new Next.js app</a></li>
<li><a class="post-section-overview" href="#heading-step-1-installing-sass-in-a-nextjs-app">Step 1: Installing Sass in a Next.js app</a></li>
<li><a class="post-section-overview" href="#heading-step-2-importing-sass-files-into-a-nextjs-app">Step 2: Importing Sass files into a Next.js app</a></li>
<li><a class="post-section-overview" href="#heading-step-3-using-sass-variables-in-a-nextjs-app">Step 3: Using Sass variables in a Next.js app</a></li>
<li><a class="post-section-overview" href="#heading-step-4-using-sass-mixins-with-global-imports-in-nextjs">Step 4: Using Sass mixins with global imports in Next.js</a></li>
</ul>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/C1-hmauMht0" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h2 id="heading-what-are-css-modules">What are CSS Modules?</h2>
<p><a target="_blank" href="https://github.com/css-modules/css-modules">CSS Modules</a> are essentially CSS files that, when imported into JavaScript projects, provide styles that are scoped to that particular part of the project by default.</p>
<p>When importing your module, the classes are represented by an object mapped with each class name, allowing you to apply that class right to your project.</p>
<p>For instance, if I had a CSS module for the title of my page:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.title</span> {
  <span class="hljs-attribute">color</span>: blueviolet;
}
</code></pre>
<p>And I import that into my React project:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> styles <span class="hljs-keyword">from</span> <span class="hljs-string">'./my-styles.css'</span>
</code></pre>
<p>I can then apply that title right to an element as if it were a string:</p>
<pre><code class="lang-jsx">&lt;h1 className={styles.title}&gt;My Title&lt;/h1&gt;
</code></pre>
<p>By scoping styles, you no longer have to worry about breaking other parts of the application with cascading styles. It’s also easier to manage smaller chunks of code that pertain to a specific piece of the application.</p>
<h2 id="heading-what-is-sass">What is Sass?</h2>
<p><a target="_blank" href="https://sass-lang.com/">Sass</a> is an extension of the CSS language that provides powerful features like variables, functions, and other operations that allow you to more easily build complex features into your project.</p>
<p>As an example, if I wanted to store my color above in a variable so I can easily change it later, I can add:</p>
<pre><code class="lang-scss"><span class="hljs-variable">$color-primary</span>: blueviolet;

<span class="hljs-selector-class">.title</span> {
  <span class="hljs-attribute">color</span>: <span class="hljs-variable">$color-primary</span>;
}
</code></pre>
<p>If I wanted to change that color but only in one spot, I can use built-in color functions to change the shade:</p>
<pre><code class="lang-scss"><span class="hljs-variable">$color-primary</span>: blueviolet;

<span class="hljs-selector-class">.title</span> {
  <span class="hljs-attribute">color</span>: <span class="hljs-variable">$color-primary</span>;
  <span class="hljs-attribute">border-bottom</span>: solid <span class="hljs-number">2px</span> darken(<span class="hljs-variable">$color-primary</span>, <span class="hljs-number">10</span>);
}
</code></pre>
<p>One additional benefit is the ability to nest styles. This allows for easier, more logical organization of your CSS.</p>
<p>For instance, if I wanted to change only a <code>&lt;strong&gt;</code> element nested in a title, I can add:</p>
<pre><code>$color-primary: blueviolet;
$color-secondary: cyan;

.title {

  <span class="hljs-attr">color</span>: $color-primary;
  border-bottom: solid <span class="hljs-number">2</span>px darken($color-primary, <span class="hljs-number">10</span>);

  strong {
    <span class="hljs-attr">color</span>: $color-secondary;
  }

}
</code></pre><h2 id="heading-what-are-we-going-to-build">What are we going to build?</h2>
<p>We're going to create a new <a target="_blank" href="https://reactjs.org/">React</a> app using <a target="_blank" href="https://nextjs.org/">Next.js</a>.</p>
<p>With our new app, we'll learn how to install and configure Sass so that we can take advantage its features inside of Next.js.</p>
<p>Once set up Sass, we'll walk through how to use Sass <a target="_blank" href="https://sass-lang.com/documentation/variables">variables</a> and <a target="_blank" href="https://sass-lang.com/documentation/at-rules/mixin">mixins</a> to recreate some of the default elements of the Next.js UI.</p>
<blockquote>
<p>Want to skip the tutorial and dive into the code? Check out <a target="_blank" href="https://github.com/colbyfayock/next-sass-starter">Next.js Sass Starter on GitHub</a>: <a target="_blank" href="https://github.com/colbyfayock/next-sass-starter"><em>https://github.com/colbyfayock/next-sass-starter</em></a></p>
</blockquote>
<h2 id="heading-step-0-creating-a-new-nextjs-app">Step 0: Creating a new Next.js app</h2>
<p>To get started with a new Next.js app, we can use <a target="_blank" href="https://nextjs.org/docs/api-reference/create-next-app">Create Next App</a>.</p>
<p>In your terminal, navigate to where you want to create the new project and run:</p>
<pre><code>yarn create next-app my-next-sass-app
</code></pre><p><em>Note: you can use npm instead of yarn for any examples with installation and package management.</em></p>
<p>Once the installation finishes, you can navigate into the directory, and start your development server:</p>
<pre><code>yarn dev
</code></pre><p>Which should start your new Next.js project at <a target="_blank" href="http://localhost:3000">http://localhost:3000</a>!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/nextjs-app.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>New Next.js app</em></p>
<p>If this is your first time creating a new Next.js app, have a look around! It comes with a basic homepage as well as two CSS files:</p>
<ul>
<li><code>/styles/globals.css</code></li>
<li><code>/styles/Home.module.css</code></li>
</ul>
<p>Here we’ll be focusing on the home file. If you look inside <code>pages/index.js,</code> there, you’ll see that we’re importing the home file, making those styles available.</p>
<p>Next.js has CSS Modules built in by default. This means that when we import our home styles file, the CSS classes are added to the styles object, and we apply each of those class names to the React elements from that object, such as:</p>
<pre><code class="lang-jsx">&lt;h1 className={styles.title}&gt;
</code></pre>
<p>That means that our styles are scoped to that single page.</p>
<p>To learn more about CSS Modules or the built-in support in Next.js, you can check out the following resources:</p>
<ul>
<li><a target="_blank" href="https://github.com/css-modules/css-modules">https://github.com/css-modules/css-modules</a></li>
<li><a target="_blank" href="https://nextjs.org/docs/basic-features/built-in-css-support">https://nextjs.org/docs/basic-features/built-in-css-support</a></li>
</ul>
<h2 id="heading-step-1-installing-sass-in-a-nextjs-app">Step 1: Installing Sass in a Next.js app</h2>
<p>While Next.js comes with some good built-in CSS support, it doesn’t come with Sass completely built in.</p>
<p>Luckily, to get Sass up and running inside of our Next.js app, all we need to do is install the <a target="_blank" href="https://www.npmjs.com/package/sass">Sass package</a> from npm, which will let Next.js include those files in its pipeline.</p>
<p>To install Sass, run the following inside of your project:</p>
<pre><code>yarn add sass
</code></pre><p>And if we start back up our development server and reload the page, we’ll actually notice that nothing has happened yet, which is a good thing!</p>
<p>But next we’ll learn how to take advantage of our CSS superpowers.</p>
<p><a target="_blank" href="https://github.com/colbyfayock/my-next-sass-app/commit/053b07ccaa1eb30a7d0eff15e2e24470564092f6">Follow along with the commit!</a></p>
<h2 id="heading-step-2-importing-sass-files-into-a-nextjs-app">Step 2: Importing Sass files into a Next.js app</h2>
<p>Now that Sass is installed, we’re ready to use it.</p>
<p>In order you use any Sass-specific features though, we’ll need to use Sass files with either the <code>.sass</code> or <code>.scss</code> extension. For this walkthrough, we’re going to use the SCSS syntax and the <code>.scss</code> extension.</p>
<p>To get started, inside of <code>pages/index.js</code>, change the import of the styles object at the top of the page to:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> styles <span class="hljs-keyword">from</span> <span class="hljs-string">'../styles/Home.module.scss'</span>
</code></pre>
<p>And once the page reloads, as we probably expect, the page is actually broken.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/nextjs-error-import.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Next.js failed to compile</em></p>
<p>To fix this, rename the file:</p>
<pre><code>/styles/Home.module.css
</code></pre><p>to</p>
<pre><code>/styles/Home.module.scss
</code></pre><p>The difference is we’re changing the file extension from <code>.css</code> to <code>.scss</code>.</p>
<p>Once the page reloads, we’ll see that our Next.js site is loading and is back ready for action!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/nextjs-app-title.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>New.js app loading</em></p>
<p>_Note: We’re not going to cover the global styles file here – you can do the same thing by renaming the global styles file and updating the import inside of <code>/pages/_app.js</code>_</p>
<p>Next, we’ll learn how to use Sass features for our Next.js app.</p>
<p><a target="_blank" href="https://github.com/colbyfayock/my-next-sass-app/commit/cf2a3f56688a728163f2e2d3229565c4efdd6661">Follow along with the commit!</a></p>
<h2 id="heading-step-3-using-sass-variables-in-a-nextjs-app">Step 3: Using Sass variables in a Next.js app</h2>
<p>Now that we’re using Sass in our project, we can start using some of the basic features like variables.</p>
<p>To show how this works, we’re going to update the blue inside of our app to my favorite color, purple!</p>
<p>At the top of <code>/styles/Home.module.css</code>, add the following:</p>
<pre><code class="lang-scss"><span class="hljs-variable">$color-primary</span>: <span class="hljs-number">#0070f3</span>;
</code></pre>
<p>The color <code>#0070f3</code> is what Next.js uses by default in the app.</p>
<p>Next, update each location that uses that color in our home CSS file to our new variables, such as changing:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.title</span> <span class="hljs-selector-tag">a</span> {
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#0070f3</span>;
</code></pre>
<p>to</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.title</span> <span class="hljs-selector-tag">a</span> {
  <span class="hljs-attribute">color</span>: <span class="hljs-variable">$color-primary</span>;
</code></pre>
<p>If we refresh the page, nothing should change.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/nextjs-app-1.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Unchanged Next.js app</em></p>
<p>But now because we’re using a variable to define that color, we can easily change it.</p>
<p>At the top of the page, change the <code>$color-primary</code> variable to purple or whatever your favorite color is:</p>
<pre><code class="lang-scss"><span class="hljs-variable">$color-primary</span>: blueviolet;
</code></pre>
<p>And when the page reloads, we can see that our colors are now purple!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/nextjs-app-purple.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Next.js app with purple color</em></p>
<p>Variables are just the start to the superpowers Sass gives our CSS, but we can see that they allow us to easily manage our colors or other values throughout our application.</p>
<p><a target="_blank" href="https://github.com/colbyfayock/my-next-sass-app/commit/0a9e72485957154cfc6a9dbe68f2d9d0d056daed">Follow along with the commit!</a></p>
<h2 id="heading-step-4-using-sass-mixins-with-global-imports-in-nextjs">Step 4: Using Sass mixins with global imports in Next.js</h2>
<p>One of the other many features of Sass is mixins. They give us the ability to create function-like definitions, allowing us to configure rules that we can repeat and use throughout our app.</p>
<p>In our example, we’re going to create a new mixin that allows us to create responsive styles using a media query throughout our app. While we can already do that with a media query alone, using a mixin allows us to use a single definition, keeping that consistent and allowing us to manage that responsive definition from one place.</p>
<p>Because this mixin is something we want to use throughout our entire application, we can also use another feature of Sass, which is the ability to import files.</p>
<p>To get started, create a new file under the <code>/styles</code> directory:</p>
<pre><code>/styles/_mixins.scss
</code></pre><p>We’re using an underscore in front of our filename to denote that it’s a partial.</p>
<p>Next, inside of our <code>/styles/Home.module.scss</code> file, let’s import that new file:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@import</span> <span class="hljs-string">"mixins"</span>;
</code></pre>
<p>Once the page reloads, we’ll notice nothing changed.</p>
<p>If we lookout the bottom of <code>Home.module.scss</code>, we’ll see that we’re using a media query to make the <code>.grid</code> class responsive. We’re going to use that as a basis for our mixin.</p>
<p>Inside of <code>_mixins.scss</code>, add the following:</p>
<pre><code>@mixin desktop() {
  @media (max-width: <span class="hljs-number">600</span>px) {
    @content;
  }
}
</code></pre><p><em>Note: while we probably can come up with a better name for this mixin than desktop, we’ll use that as the basis of our example.</em></p>
<p>The <code>@content</code> means that any time we use our desktop mixin, it will include the nested content in that location.</p>
<p>To test this out, back in our <code>Home.module.css</code> file, let’s update our <code>.grid</code> snippet:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@include</span> desktop() {
  <span class="hljs-selector-class">.grid</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">flex-direction</span>: column;
  }
}
</code></pre>
<p>If we open our app back up and shrink the browser window, we can see that we still have our responsive styles!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/nextjs-responsive-styles.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Responsive styles in Next.js</em></p>
<p>We can even take this a step further. Sass allows you to nest styles. For instance, instead of writing:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.footer</span> {
  // Styles
}

<span class="hljs-selector-class">.footer</span> <span class="hljs-selector-tag">img</span> {
  <span class="hljs-attribute">margin-left</span>: <span class="hljs-number">0.5rem</span>;
}
</code></pre>
<p>We can include that <code>img</code> definition right inside of the original <code>.footer</code> definition:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.footer</span> {

  <span class="hljs-comment">// Styles</span>

  <span class="hljs-selector-tag">img</span> {
    <span class="hljs-attribute">margin-left</span>: <span class="hljs-number">0.5rem</span>;
  }

}
</code></pre>
<p>That img definition will compile to <code>.footer img</code>, just the same as if it was written in standard CSS.</p>
<p>So with that in mind, we can use the same concept and move our desktop mixin into our original <code>.grid</code> class:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.grid</span> {

  <span class="hljs-keyword">@include</span> desktop() {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">flex-direction</span>: column;
  }

}
</code></pre>
<p>And if you notice, because we’re inside of the <code>.grid</code> class already, we can remove that from inside of the mixin, as it will already be applied.</p>
<p>This allows for much easier organization of our responsive styles.</p>
<p>Finally, if we look back at our app, we’ll notice that still, nothing has changed, which means we’re successfully using our Sass mixin.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/nextjs-responsive-styles-1.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>No change in our Next.js app</em></p>
<p><a target="_blank" href="https://github.com/colbyfayock/my-next-sass-app/commit/6781916d8c12225c85dfde96bdab59bfaa14e22b">Follow along with the commit!</a>  </p>
<h2 id="heading-what-else-can-we-do-with-sass-and-nextjs">What else can we do with Sass and Next.js?</h2>
<p>We’re only scratching the surface here with Sass. Because our CSS modules now have the power of Sass, we have a ton of capabilities that don’t come by default with CSS.</p>
<h3 id="heading-color-functions">Color Functions</h3>
<p>Sass has a ton of functions built in that allow us to manipulate colors, mixing and matching shades much more easily.</p>
<p>Two that I use often are <a target="_blank" href="https://sass-lang.com/documentation/modules/color#darken">darken</a> and <a target="_blank" href="https://sass-lang.com/documentation/modules/color#lighten">lighten</a>, that allow you to take a color and change the shade.</p>
<p><a target="_blank" href="https://sass-lang.com/documentation/modules/color">Learn more about all of the available color functions in Sass.</a></p>
<h3 id="heading-custom-functions">Custom Functions</h3>
<p>While mixins seem like functions, we can define true functions in Sass that allow us to perform complex operations and produce values based on an input.</p>
<p><a target="_blank" href="https://sass-lang.com/documentation/values/functions">Learn more about custom functions in Sass.</a></p>
<h3 id="heading-other-value-types">Other Value Types</h3>
<p>While most of the time with CSS we’re using strings or numbers, we saw that a simple extension of that is the ability to use variables.</p>
<p>In addition to variables, Sass gives us more value types like <a target="_blank" href="https://sass-lang.com/documentation/values/maps">Maps</a>, which function sort of like an object, and <a target="_blank" href="https://sass-lang.com/documentation/values/lists">Lists</a>, which are kind of like arrays.</p>
<p><a target="_blank" href="https://sass-lang.com/documentation/values">Learn more about the value types in Sass.</a></p>
<h3 id="heading-more">More</h3>
<p>There are a ton of features available in Sass and lots of articles that cover the most used features. Take some time to explore the documentation and find what’s out there!</p>
<div id="colbyfayock-author-card">
  <p>
    <a href="https://twitter.com/colbyfayock">
      <img src="https://res.cloudinary.com/fay/image/upload/w_2000,h_400,c_fill,q_auto,f_auto/w_1020,c_fit,co_rgb:007079,g_north_west,x_635,y_70,l_text:Source%20Sans%20Pro_64_line_spacing_-10_bold:Colby%20Fayock/w_1020,c_fit,co_rgb:383f43,g_west,x_635,y_6,l_text:Source%20Sans%20Pro_44_line_spacing_0_normal:Follow%20me%20for%20more%20JavaScript%252c%20UX%252c%20and%20other%20interesting%20things!/w_1020,c_fit,co_rgb:007079,g_south_west,x_635,y_70,l_text:Source%20Sans%20Pro_40_line_spacing_-10_semibold:colbyfayock.com/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_68,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_145,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_222,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_295,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/v1/social-footer-card" alt="Follow me for more Javascript, UX, and other interesting things!" width="2000" height="400" loading="lazy">
    </a>
  </p>
  <ul>
    <li>
      <a href="https://twitter.com/colbyfayock">🐦 Follow Me On Twitter</a>
    </li>
    <li>
      <a href="https://youtube.com/colbyfayock">📺 Subscribe To My Youtube</a>
    </li>
    <li>
      <a href="https://www.colbyfayock.com/newsletter/">📫 Sign Up For My Newsletter</a>
    </li>
    <li>
      <a href="https://github.com/sponsors/colbyfayock">💝 Sponsor Me</a>
    </li>
  </ul>
</div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to add a CSS Modules Stylesheet to your React component in 4 simple steps ]]>
                </title>
                <description>
                    <![CDATA[ By Holly Atkinson Let’s say you’d like to add a CSS Modules Stylesheet to your project. You can find Create React App’s guidance here, but essentially — and as the guidance states — CSS Modules let you use the same CSS selector in different files wit... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-add-a-css-modules-stylesheet-to-your-react-component-in-4-simple-steps/</link>
                <guid isPermaLink="false">66d45f31052ad259f07e4ae0</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS Modules ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sun, 28 Jul 2019 13:03:32 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9ca135740569d1a4ca4d4a.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Holly Atkinson</p>
<p>Let’s say you’d like to add a CSS Modules Stylesheet to your project. You can find Create React App’s <a target="_blank" href="https://facebook.github.io/create-react-app/docs/adding-a-css-modules-stylesheet">guidance</a> here, but essentially — and as the guidance states — CSS Modules let you use the <em>same</em> CSS selector in different files without worrying about naming clashes. This works because each HTML element in your file that you want to style is automatically given a <em>unique</em> class name.</p>
<p>This can seem quite confusing at first, but really the process to implement CSS Modules can be simplified to just 4 steps, as demonstrated in the below example.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2019/09/1_lmno_4nmNyohGa-gM_GaEw.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>Yes, really!</em></p>
<h3 id="heading-adding-modular-css-to-a-simple-component">Adding modular CSS to a simple  component</h3>
<ol>
<li>A feature of React is that CSS Modules are “turned on” for files ending with the <code>.module.css</code> extension. Create the CSS file with a specific filename in the following format:</li>
</ol>
<pre><code class="lang-bash">Link.module.css
</code></pre>
<ol start="2">
<li>Import styling to your component:</li>
</ol>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> styles <span class="hljs-keyword">from</span> ‘../styling/components/Link.module.css’
</code></pre>
<ol start="3">
<li>The styles in your CSS file can follow your preferred naming convention, for instance:</li>
</ol>
<pre><code class="lang-css"><span class="hljs-selector-class">.bold</span> {  <span class="hljs-attribute">font-weight</span>: bold;}
</code></pre>
<ol start="4">
<li>The style is applied to the HTML element as follows:</li>
</ol>
<pre><code class="lang-jsx">className={styles.bold}
</code></pre>
<p>And that’s it!</p>
<p><em>Photo credit:</em> <a target="_blank" href="https://unsplash.com/photos/72El6N0cmj4?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText"><em>Adrian Swancar</em></a> <em>on</em> <a target="_blank" href="https://unsplash.com/search/photos/confused?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText"><em>Unsplash</em></a></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
