cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 - widget, add css to block

Magento 2 - widget, add css to block

I want to insert a css fom the widget block in the magento head but cannot achieve this, i am using:

namespace Vendor\Module\Block\Widget;

use Magento\Framework\View\Element\Template;
use Magento\Widget\Block\BlockInterface;

class Sliderimage extends Template implements BlockInterface
{
protected function _construct()
{
parent::_construct();

$this->setTemplate('widget/sliderimage.phtml');
}

/**
* @return $this
*/
protected function _prepareLayout()
{
$this->pageConfig->addPageAsset('Vendor_Module::css/sliderimage.css');

return parent::_prepareLayout();
}
}

What am i missing?

 

Thank you,
Damien.

3 REPLIES 3

Re: Magento 2 - widget, add css to block

Hello @KennedyA 

 

protected function _construct() {
 $om = \Magento\Framework\App\ObjectManager::getInstance();
 $page = $om->get('Magento\Framework\View\Page\Config');
 $page->addPageAsset('Vendor_Extension::css/yourstyles.css');
 }

please check above code

 

Hope it will help you

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Magento 2 - widget, add css to block

Hi @KennedyA 

Kindly refer below url :
https://webkul.com/blog/magento2-how-to-add-css-and-js-file-in-module/

It will help you to resolve you issue.

If issue resolve, Please click on 'Kudos' & Accept as Solution!

Problem solved? Click Accept as Solution!

Re: Magento 2 - widget, add css to block

Hi, thank you but i want to add this asset only on page displaying the
widget.