How To Add Css And Js In Magento 2

Web Development Software

Adding CSS and JS files in Magento 2 is an essential part of customizing and enhancing the appearance and functionality of your Magento store. In this article, I’ll guide you through the process of adding CSS and JS files in Magento 2, providing you with detailed steps and personal insights along the way.

Step 1: Create Custom Theme

The first step to adding CSS and JS files in Magento 2 is to create a custom theme. This allows us to have a separate directory for our custom files and ensures that our changes won’t be overwritten during future updates.

To create a custom theme, navigate to the app/design/frontend directory of your Magento installation. Inside this directory, create a new folder for your custom theme. For example, I’ll name my theme mytheme. Inside the mytheme folder, create another folder called web. This is where we’ll place our custom CSS and JS files.

Step 2: Create CSS and JS Files

Once you’ve created the custom theme and the web folder, it’s time to create our CSS and JS files. In the web folder, create a new folder called css and another one called js.

Inside the css folder, create a new CSS file. For example, I’ll name mine custom.css. This is where we’ll write our custom CSS code. Similarly, inside the js folder, create a new JS file. I’ll name mine custom.js. This is where we’ll write our custom JavaScript code.

Step 3: Register CSS and JS Files in Magento

Now that we have our custom CSS and JS files ready, we need to register them in Magento. To do this, create a new file called requirejs-config.js inside the mytheme folder. In this file, we’ll define the dependencies and paths for our CSS and JS files. Here’s an example of how the requirejs-config.js file should look:

var config = {
  map: {
    '*': {
      'my-custom-script': 'js/custom',
      'my-custom-style': 'css/custom'
    }
  }
};

In the above code snippet, we define two mappings: my-custom-script for our JS file and my-custom-style for our CSS file. The paths specified should match the location of your custom files relative to the web folder.

Step 4: Add CSS and JS to Layout Files

With the files registered, we now need to add them to the layout files in Magento. Navigate to the app/design/frontend/mytheme/Magento_Theme/layout directory and open the default_head_blocks.xml file.

Inside the default_head_blocks.xml file, add the following code to include your CSS and JS files:

<head>
  <css src="my-custom-style.css" />
  <script src="my-custom-script.js" />
</head>

Make sure to replace my-custom-style.css and my-custom-script.js with the names of your CSS and JS files respectively.

Step 5: Deploy the Static Content

After making these changes, you need to deploy the static content to reflect the changes on the frontend. Open a terminal or command prompt, navigate to the root directory of your Magento installation, and run the following command:

php bin/magento setup:static-content:deploy

This command will generate the necessary static files for your custom theme, including your CSS and JS files.

Conclusion

Adding CSS and JS files in Magento 2 is a straightforward process that gives you full control over the appearance and functionality of your store. By creating a custom theme, creating the necessary CSS and JS files, registering them in Magento, and adding them to the layout files, you can easily customize your Magento store to meet your specific needs.