How to add jss and css in magento2
I have created a block and called it in my phtml file then I call that block in my cms page like this {{block class="abcCustomBlockProduct" template="abc_Custom::product.phtml"}}
and some HTML code in phtml file but I don't have controllers so now how do I add CSS and js file using layout XML
magento2 layout css
add a comment |
I have created a block and called it in my phtml file then I call that block in my cms page like this {{block class="abcCustomBlockProduct" template="abc_Custom::product.phtml"}}
and some HTML code in phtml file but I don't have controllers so now how do I add CSS and js file using layout XML
magento2 layout css
add a comment |
I have created a block and called it in my phtml file then I call that block in my cms page like this {{block class="abcCustomBlockProduct" template="abc_Custom::product.phtml"}}
and some HTML code in phtml file but I don't have controllers so now how do I add CSS and js file using layout XML
magento2 layout css
I have created a block and called it in my phtml file then I call that block in my cms page like this {{block class="abcCustomBlockProduct" template="abc_Custom::product.phtml"}}
and some HTML code in phtml file but I don't have controllers so now how do I add CSS and js file using layout XML
magento2 layout css
magento2 layout css
edited 2 mins ago
Bhakti Thakkar
48514
48514
asked 49 mins ago
sdfasdfsdfasdf
1
1
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can add your js and css using layout by below method
Edit your layout file app/code/vendor/module/view/frontend/layout/test_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<head>
<css src="css/mycustomcss.css" />
<script src="js/mycustomjs.js"/>
</head>
<body>
//your code here
</body>
</page>
Create your js and css file in your module as below path.
app/code/vendor/module/view/frontend/web/css/mycustomcss.css
app/code/vendor/module/view/frontend/web/js/mycustomjs.js
test_index_index.xml i dont want this
– sdfasdf
28 mins ago
add a comment |
vendormagentomodule-themeviewfrontendlayoutdefault_head_blocks.xml
override this (default_head_blocks.xml) xml file in your custom theme like below
appdesignfrontendPackagecustom-themeMagento_Themelayoutdefault_head_blocks.xml
add below code in default_head_blocks.xml file
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<link src="js/custom.css"/> OR <css src="css/custom.less"/>
<script src="js/custom.js"/>
</head>
</page>
keep your js and css/less file in
appdesignfrontendPackagecustom-themewebcsscustom.css
appdesignfrontendPackagecustom-themewebjscustom.js
after check your changes.
New contributor
add a comment |
Magento 2 brings some new features in javascript. One of them is “requireJS” (javascript file loader) and second is “jQuery“, a very popular js library. One of the biggest benefits is using requireJS which means that Magento 2 is ready for the upcoming HTTP2 protocol.
1) Place your requirejs-config.js file in one of the following directories (according to the location of your custom script):
Your theme files: <theme_dir>
Or
Your module view files: /view/frontend
requirejs-config.js
var config = {
"map": {
"*": {
"menu": "js/navigation-menu"
}
}
};
2) Load your js files in your template files
<script type="text/javascript">
require([
'menu'
], function(menu){
$(document).ready( function() {
//Do your actions here
});
});
</script>
Refer this Magento Dev Docs for more information https://devdocs.magento.com/guides/v2.3/javascript-dev-guide/javascript/custom_js.html
For loading CSS, you can directly load it through layout XML file
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<head>
<css src="css/mycustomcss.css" />
</head>
</page>
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f258143%2fhow-to-add-jss-and-css-in-magento2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can add your js and css using layout by below method
Edit your layout file app/code/vendor/module/view/frontend/layout/test_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<head>
<css src="css/mycustomcss.css" />
<script src="js/mycustomjs.js"/>
</head>
<body>
//your code here
</body>
</page>
Create your js and css file in your module as below path.
app/code/vendor/module/view/frontend/web/css/mycustomcss.css
app/code/vendor/module/view/frontend/web/js/mycustomjs.js
test_index_index.xml i dont want this
– sdfasdf
28 mins ago
add a comment |
You can add your js and css using layout by below method
Edit your layout file app/code/vendor/module/view/frontend/layout/test_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<head>
<css src="css/mycustomcss.css" />
<script src="js/mycustomjs.js"/>
</head>
<body>
//your code here
</body>
</page>
Create your js and css file in your module as below path.
app/code/vendor/module/view/frontend/web/css/mycustomcss.css
app/code/vendor/module/view/frontend/web/js/mycustomjs.js
test_index_index.xml i dont want this
– sdfasdf
28 mins ago
add a comment |
You can add your js and css using layout by below method
Edit your layout file app/code/vendor/module/view/frontend/layout/test_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<head>
<css src="css/mycustomcss.css" />
<script src="js/mycustomjs.js"/>
</head>
<body>
//your code here
</body>
</page>
Create your js and css file in your module as below path.
app/code/vendor/module/view/frontend/web/css/mycustomcss.css
app/code/vendor/module/view/frontend/web/js/mycustomjs.js
You can add your js and css using layout by below method
Edit your layout file app/code/vendor/module/view/frontend/layout/test_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<head>
<css src="css/mycustomcss.css" />
<script src="js/mycustomjs.js"/>
</head>
<body>
//your code here
</body>
</page>
Create your js and css file in your module as below path.
app/code/vendor/module/view/frontend/web/css/mycustomcss.css
app/code/vendor/module/view/frontend/web/js/mycustomjs.js
answered 43 mins ago
RizwanRizwan
644323
644323
test_index_index.xml i dont want this
– sdfasdf
28 mins ago
add a comment |
test_index_index.xml i dont want this
– sdfasdf
28 mins ago
test_index_index.xml i dont want this
– sdfasdf
28 mins ago
test_index_index.xml i dont want this
– sdfasdf
28 mins ago
add a comment |
vendormagentomodule-themeviewfrontendlayoutdefault_head_blocks.xml
override this (default_head_blocks.xml) xml file in your custom theme like below
appdesignfrontendPackagecustom-themeMagento_Themelayoutdefault_head_blocks.xml
add below code in default_head_blocks.xml file
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<link src="js/custom.css"/> OR <css src="css/custom.less"/>
<script src="js/custom.js"/>
</head>
</page>
keep your js and css/less file in
appdesignfrontendPackagecustom-themewebcsscustom.css
appdesignfrontendPackagecustom-themewebjscustom.js
after check your changes.
New contributor
add a comment |
vendormagentomodule-themeviewfrontendlayoutdefault_head_blocks.xml
override this (default_head_blocks.xml) xml file in your custom theme like below
appdesignfrontendPackagecustom-themeMagento_Themelayoutdefault_head_blocks.xml
add below code in default_head_blocks.xml file
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<link src="js/custom.css"/> OR <css src="css/custom.less"/>
<script src="js/custom.js"/>
</head>
</page>
keep your js and css/less file in
appdesignfrontendPackagecustom-themewebcsscustom.css
appdesignfrontendPackagecustom-themewebjscustom.js
after check your changes.
New contributor
add a comment |
vendormagentomodule-themeviewfrontendlayoutdefault_head_blocks.xml
override this (default_head_blocks.xml) xml file in your custom theme like below
appdesignfrontendPackagecustom-themeMagento_Themelayoutdefault_head_blocks.xml
add below code in default_head_blocks.xml file
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<link src="js/custom.css"/> OR <css src="css/custom.less"/>
<script src="js/custom.js"/>
</head>
</page>
keep your js and css/less file in
appdesignfrontendPackagecustom-themewebcsscustom.css
appdesignfrontendPackagecustom-themewebjscustom.js
after check your changes.
New contributor
vendormagentomodule-themeviewfrontendlayoutdefault_head_blocks.xml
override this (default_head_blocks.xml) xml file in your custom theme like below
appdesignfrontendPackagecustom-themeMagento_Themelayoutdefault_head_blocks.xml
add below code in default_head_blocks.xml file
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<link src="js/custom.css"/> OR <css src="css/custom.less"/>
<script src="js/custom.js"/>
</head>
</page>
keep your js and css/less file in
appdesignfrontendPackagecustom-themewebcsscustom.css
appdesignfrontendPackagecustom-themewebjscustom.js
after check your changes.
New contributor
New contributor
answered 30 mins ago
Mohammad FaizanMohammad Faizan
112
112
New contributor
New contributor
add a comment |
add a comment |
Magento 2 brings some new features in javascript. One of them is “requireJS” (javascript file loader) and second is “jQuery“, a very popular js library. One of the biggest benefits is using requireJS which means that Magento 2 is ready for the upcoming HTTP2 protocol.
1) Place your requirejs-config.js file in one of the following directories (according to the location of your custom script):
Your theme files: <theme_dir>
Or
Your module view files: /view/frontend
requirejs-config.js
var config = {
"map": {
"*": {
"menu": "js/navigation-menu"
}
}
};
2) Load your js files in your template files
<script type="text/javascript">
require([
'menu'
], function(menu){
$(document).ready( function() {
//Do your actions here
});
});
</script>
Refer this Magento Dev Docs for more information https://devdocs.magento.com/guides/v2.3/javascript-dev-guide/javascript/custom_js.html
For loading CSS, you can directly load it through layout XML file
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<head>
<css src="css/mycustomcss.css" />
</head>
</page>
add a comment |
Magento 2 brings some new features in javascript. One of them is “requireJS” (javascript file loader) and second is “jQuery“, a very popular js library. One of the biggest benefits is using requireJS which means that Magento 2 is ready for the upcoming HTTP2 protocol.
1) Place your requirejs-config.js file in one of the following directories (according to the location of your custom script):
Your theme files: <theme_dir>
Or
Your module view files: /view/frontend
requirejs-config.js
var config = {
"map": {
"*": {
"menu": "js/navigation-menu"
}
}
};
2) Load your js files in your template files
<script type="text/javascript">
require([
'menu'
], function(menu){
$(document).ready( function() {
//Do your actions here
});
});
</script>
Refer this Magento Dev Docs for more information https://devdocs.magento.com/guides/v2.3/javascript-dev-guide/javascript/custom_js.html
For loading CSS, you can directly load it through layout XML file
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<head>
<css src="css/mycustomcss.css" />
</head>
</page>
add a comment |
Magento 2 brings some new features in javascript. One of them is “requireJS” (javascript file loader) and second is “jQuery“, a very popular js library. One of the biggest benefits is using requireJS which means that Magento 2 is ready for the upcoming HTTP2 protocol.
1) Place your requirejs-config.js file in one of the following directories (according to the location of your custom script):
Your theme files: <theme_dir>
Or
Your module view files: /view/frontend
requirejs-config.js
var config = {
"map": {
"*": {
"menu": "js/navigation-menu"
}
}
};
2) Load your js files in your template files
<script type="text/javascript">
require([
'menu'
], function(menu){
$(document).ready( function() {
//Do your actions here
});
});
</script>
Refer this Magento Dev Docs for more information https://devdocs.magento.com/guides/v2.3/javascript-dev-guide/javascript/custom_js.html
For loading CSS, you can directly load it through layout XML file
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<head>
<css src="css/mycustomcss.css" />
</head>
</page>
Magento 2 brings some new features in javascript. One of them is “requireJS” (javascript file loader) and second is “jQuery“, a very popular js library. One of the biggest benefits is using requireJS which means that Magento 2 is ready for the upcoming HTTP2 protocol.
1) Place your requirejs-config.js file in one of the following directories (according to the location of your custom script):
Your theme files: <theme_dir>
Or
Your module view files: /view/frontend
requirejs-config.js
var config = {
"map": {
"*": {
"menu": "js/navigation-menu"
}
}
};
2) Load your js files in your template files
<script type="text/javascript">
require([
'menu'
], function(menu){
$(document).ready( function() {
//Do your actions here
});
});
</script>
Refer this Magento Dev Docs for more information https://devdocs.magento.com/guides/v2.3/javascript-dev-guide/javascript/custom_js.html
For loading CSS, you can directly load it through layout XML file
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<head>
<css src="css/mycustomcss.css" />
</head>
</page>
answered 13 mins ago
Mahesh PatelMahesh Patel
364
364
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f258143%2fhow-to-add-jss-and-css-in-magento2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown