Yii2: How to add custom attributes in <link> tag

Created on 16 Mar 2017  路  4Comments  路  Source: yiisoft/yii2

I want to add attribute media="screen" in tag, all assets paths are defined in AppAsset.php file

 public $css = [

               'frontend/web/css/site.css',

        'frontend/web/css/custome.css',

        'frontend/web/css/font-awesome.css',

        'frontend/web/css/jquery.bxslider.css',        

    ];

i see no room for that, i have also checked documentation but found nothing.

Docs

What is the expected result?

<link href="/assets/597be7e4/css/bootstrap.css" rel="stylesheet" media="screen">

What do you get instead?

<link href="/assets/597be7e4/css/bootstrap.css" rel="stylesheet">

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0
| PHP version | 7.0.1
| Operating system | Ubuntu 14.04

Thanks !!!

question

Most helpful comment

You can also specify options as

['frontend/web/css/site.css', 'media' => 'screen']

It's documented: http://www.yiiframework.com/doc-2.0/yii-web-assetbundle.html#$css-detail

All 4 comments

there is the $cssOptions

so you can define

public $cssOptions = ['media' => 'screen'];

this will add media="screen" to all css files in this assetBundle.

If you want to have different media types i would suggest to create a seperate bundle per media and require the media bundles in the main bundle.

You can also specify options as

['frontend/web/css/site.css', 'media' => 'screen']

It's documented: http://www.yiiframework.com/doc-2.0/yii-web-assetbundle.html#$css-detail

Thank you for your question.
In order for this issue tracker to be effective, it should only contain bug reports and feature requests.

We advise you to use our community driven resources:

If you are confident that there is a bug in the framework, feel free to provide information on how to reproduce it. This issue will be closed for now.

_This is an automated comment, triggered by adding the label question._

thanks @Aradiv @samdark :)

Was this page helpful?
0 / 5 - 0 ratings