Magento – How do add css, js in customview.phtml in view magento2

blocksmagento-2.0magento2

I have custom view.phtml. I Want add my.css and my.js in <head></head>
2 file in app\code\Test\New\view\frontend\web\css.

How do it?

Best Answer

You can add CSS file as following way for your custom module.

Open you layout file e.g. modulename_index_view.xml and following code in <head>

<head>        
        <css src="Test_New::css/my.css"/>
</head>

to add custom module JS file you can use standard way as mentioned with below link.

How to load custom module js file in magento 2?

My suggestion (Optional)

You can use the standard way of Magento 2 to call css and js rather than Call CSS and JS files directly into layout file.

You can call JS using requirejs module. (You can maintain dependency of code.)

You may call css file using _module.less just put it under app\code\<Vendor>\<ModuleName>\view\frontend\web\css\source. So It will call automatically as well as _module.less will compile by grunt tool and converted into your theme or module css