Skip to content Skip to sidebar Skip to footer

Override Bootstrap Css In Yii2

I have a NavBar and I want to override all the classes that Yii is putting on that widget. After searching inn Google and reading the docs, I found that this code: Yii::$container-

Solution 1:

If you want to remove collapse and navbar-collapse out of the box, using the widget parameters, you can't.

As you can see in the code for the NavBar component in Yii2, those classes are hard-coded in there.

Your options:

  • Modify the Navbar.php file and remove this classes (not recommended).
  • Write directly in HTML your navbar (not ideal, but is a valid workaround).
  • Create your own custom Navbar Component for Yii2 (recommended, because you can copy the Navbar.php file, remove those classes and change the namespace).

Here is a simple tutorial for creating your components and using them.

Solution 2:

If you want to remove collapse and navbar-collapse out of the box, using the widget parameters, try this:

add to your NavBar...

'collapseOptions' => [
    'class' => [
        'collapse' => '',
        'widget' => '',
    ]
]

will keep like this...

NavBar::begin([
    'brandLabel' => Yii::$app->name,
    'brandUrl' => Yii::$app->homeUrl,
     'collapseOptions' => [
        'class' => [
            'new' => 'navbar-nav-scroll',
            'collapse' => '',
            'widget' => '',
        ],
    ],
    'options' => [
        'class' => 'navbar navbar-expand-sm navbar-dark bg-primary fixed-top',
    ],
]);

it works for me.

Post a Comment for "Override Bootstrap Css In Yii2"