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

Proxy Design Pattern and Requisition List in Magento 2

Spread the love Requisition List in Magento 2 Requisition

Google Ads Conversion Tracking Through Magento Platform:

Spread the love Google Ads Conversion Tracking Through

Object Pool Pattern in Magento2

Spread the love What is Object Pool Pattern in Magento 2 An

JENKINS

Spread the love JENKINS Introduction and Installation

Virtual Type and Type

Spread the love Virtual type is one of the most common

Customize the length of Input field

Spread the love Sometimes we need to change the limit the

How to Add Custom Tab on Admin Dashboard and Count Website Total Visitor in Magento 2?

Spread the love In this blog we’ll see How to Add Custom

How to Add Sign Out Tab in Customer Account in Magento 2?

Spread the love In this blog we’ll see How to Add

How to create custom tab in product detail page in magento 2?

Spread the love In this blog we’ll see how to add

Understanding and Implementing CORS & CSRF

Spread the love What is security? The world has become more

bluethinkinc Blogs