How to add jss and css in magento2












0















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










share|improve this question





























    0















    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










    share|improve this question



























      0












      0








      0








      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










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 mins ago









      Bhakti Thakkar

      48514




      48514










      asked 49 mins ago









      sdfasdfsdfasdf

      1




      1






















          3 Answers
          3






          active

          oldest

          votes


















          1














          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





          share|improve this answer
























          • test_index_index.xml i dont want this

            – sdfasdf
            28 mins ago



















          0














          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.






          share|improve this answer








          New contributor




          Mohammad Faizan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.




























            0














            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>





            share|improve this answer























              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
              });


              }
              });














              draft saved

              draft discarded


















              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









              1














              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





              share|improve this answer
























              • test_index_index.xml i dont want this

                – sdfasdf
                28 mins ago
















              1














              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





              share|improve this answer
























              • test_index_index.xml i dont want this

                – sdfasdf
                28 mins ago














              1












              1








              1







              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





              share|improve this answer













              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






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 43 mins ago









              RizwanRizwan

              644323




              644323













              • 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





              test_index_index.xml i dont want this

              – sdfasdf
              28 mins ago













              0














              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.






              share|improve this answer








              New contributor




              Mohammad Faizan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

























                0














                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.






                share|improve this answer








                New contributor




                Mohammad Faizan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.























                  0












                  0








                  0







                  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.






                  share|improve this answer








                  New contributor




                  Mohammad Faizan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.










                  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.







                  share|improve this answer








                  New contributor




                  Mohammad Faizan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  Mohammad Faizan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 30 mins ago









                  Mohammad FaizanMohammad Faizan

                  112




                  112




                  New contributor




                  Mohammad Faizan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Mohammad Faizan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Mohammad Faizan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.























                      0














                      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>





                      share|improve this answer




























                        0














                        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>





                        share|improve this answer


























                          0












                          0








                          0







                          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>





                          share|improve this answer













                          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>






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 13 mins ago









                          Mahesh PatelMahesh Patel

                          364




                          364






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              Magento 2 controller redirect on button click in phtml file

                              Polycentropodidae