0

Is there a way to load the same css file in admin and frontend? I do not want to create 2 files with the same content and include them in adminthml and frontend folders.

I know How to include a js file in admin and frontend. I just put it in js folder and loaded from xml: For instance:

<action method="addItem"><type>js</type><name>datepicker/custom_datepicker.js</name></action>

How can I do the same thing with a CSS File ?

0

You do not have to create two of the same css files. However, you would need to use layout xml for both frontend and adminhtml, but it can be the same css file. For best results, store your file in the js folder, not the skin folder.

In the node you have above, use js_css as the type and then you can actually place CSS files in the js directory.

For example:

<action method="addItem"><type>js_css</type><name>some/dir/example.css</name></action>

Take note of the "type" node. js_css means that it is a css file that lives in the main js directory. The main js directory is not limited to area such as "adminhtml" or "frontend". The actual location of the css file is js/some/dir/example.css

I should note that this little nuance has actually been addressed in Magento 2. However, for M1, you will need to reference the file separately in your layout xml files.

However, now that I think of it, you can create a global observer that adds the file to both frontend and adminhtml.

| improve this answer | |
  • I stored my js file in the js folder, but what about the css file ? – Attila Naghi Jul 27 '16 at 6:14
  • I have just edited my answer. – Shawn Abramson Jul 27 '16 at 6:18
  • You said to store my css file in the JS folder and loaded via XML for both admin and frontend ? Please can you give me an example ? – Attila Naghi Jul 27 '16 at 6:27
  • I have just edited my answer to include an example. I tested it just before posting and it works. – Shawn Abramson Jul 27 '16 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.