How to add CSS and JavaScript in our custom template

Blog
Spread the love

Technically there is a option to include css and JS in template file but Adobe commerce strongly recommended avoiding this.

This topic describe how to apply custom css and js on template file.

Apply Custom CSS:

There are diffrent ways to apply css on custom page. We can use each method according to out requirement.

  1. Internal CSS
  2. Inline CSS
  3. External CSS

Method 1: Internal CSS

Internal CSS is defined in a page which will apply on the sepecific page.

Example:

Method 2:Inline CSS

The inline css is apply on single element using style attribute.

Example:

Method 3: External CSS

The external css is apply each page in which must has a reference to the external css file.

Please follow below Steps to include external css.

Step1: External CSS are defined in layout file using “css” element, reference by “src” attribute and inside the section.

Example: Create a layout file in :

Bluethink/Crud/View/frontend/layout/filename.xml

Step 2: Create custom.css file in :

“Bluethink/Crud/View/web/css/custom.css”

extention_2023

Step3: Deploy Changes by run below command:

rm -rf var/cache/* var/page_cache/* pub/static/frontend/*

var/view_preprocessed/pub/static/frontend/*

sudo php bin/magento s:s:d -f

sudo php bin/magento c:f

sudo chmod -R 777 var pub generated

Apply Custom JavaScript

There are different ways to apply custom JS in custom phtml file.

Method 1: Using custom module

Include JS file reference in script element inside head section in layout file.

Please follow below steps to apply JS

Step1: Create app/code/Bluethink/Crud/View/frontend/layout/filename.xml

Step 2: Create JS file in app/code/Bluethink/Crud/View/frontend/web/js/custom.js

Method 2: Using theme

Please follow below steps to apply js using custom theme.

Step1: Create layout file in custom theme inside Design folder:

app/design/frontend/{theme/folder/Vendor_Module}/layout/filename.xml

Step2: Create {Vendor_Module}/web/js/filename.js

put the js content in this file like below example:

Method 3: Using requirejs-config.js file:

please follow below steps to apply custom js :

This method can be apply in custom module and custom theme.

Step1: Create requirejs-config.js file in:

app/design/frontend/{theme/folder/Vendor_Module}/requirejs-config.js

or

app/code/Bluethink/Crud/View/requirejs-config.js

Content would be:

Explanation:

There is a keyword customJs which is used inside map keyword. This is a alise name for JS filepath.

Jquery will run before js file by using shim keyword.

Step 2: Create customJs.js file in:

app/design/frontend/{theme/folder/Vendor_Module}/web/js/customJs.js

OR

app/code/Bluethink/Crud/View/web/js/customJs.js

content would be according requirement:

Step 3: Use js in phtml file

welcome to second js file

Method 4: Using Script tag

Custom Js can be apply using script tag in the same phtml file:

bluethinkinc_blog

bluethinkinc_blog

2023-09-06

0

Leave a Reply

Your email address will not be published. Required fields are marked *

Find More Blogs

Customize lead qualification for a seamless experience

Spread the love When the 2024 Release Wave 2 was launched,

Create a Custom Console Command in Spryker

Spread the love In Spryker, developers can create custom

SALESFORCE

Spread the love Salesforce is a cloud-based software

CRUD Operation in Spryker Backoffice with a Custom Module

Spread the love Spryker is a highly flexible and modular

How to create custom module on Spryker for Backoffice?

Spread the love Creating a custom module in Spryker

How to Install Spryker Project with Docker on Local Machine

Spread the love Spryker is a flexible and scalable commerce

How to Add a Custom Tab to the Customer Order Detail Page in Magento 2

Spread the love Adding a custom tab to the Order Detail

Exception Handling in Java

Spread the love Introduction – Exception:An unwanted

How to add custom less file in Magento2

Spread the love Source files *.css included into layout

Spring Boot Microservices

Spread the love Microservices architecture has become

bluethinkinc Blogs