9

how can I use a custom CSS (obtained from a bootstrap HTML template) with yii2 framework frontend? (i'm using Yii advanced template)

things i tried and that didn't work:

  • adding the path to the custom css file in frontend\assets\AppAsset.php
  • replaced the bootstrap-theme.css file under frontend\web\assets\e4f17951\css\
  • modified the frontend\config\main.php according to this tutorial

EDIT:

following the tutorial , i added this to main-local.php:

'assetManager' => [
    'bundles' => [
        'yii\bootstrap\BootstrapAsset' => [
             'sourcePath' => 'frontend/web',
             'css' => ['css/bootstrap.css', 'css/agency.css']
            ],
        ],
    ],

maybe something wrong with sourcePath?

any help will be much appreciated.

2
  • Please add details about why it didn't work (only for third) Commented Apr 30, 2015 at 20:28
  • i have added details .. Commented Apr 30, 2015 at 20:54

2 Answers 2

12

You can edit your frontend web config file to point to your custom bootstrap.css. Note that the path is relative.

'assetManager' => [
    'bundles' => [
        'yii\bootstrap\BootstrapAsset' => [
            'css' => [
                'bootstrap.css' => 'path/to/your/bootstrap.css'
            ]
        ]
    ]
],
Sign up to request clarification or add additional context in comments.

1 Comment

the bootstrap html template has the same bootstrap.css file than the YII app. and the second file, 'agency.css' ,is the custom is the one i need, in top of the "original' bootstrap file. PS: sorry for my english .
4

you can actually edit the AppAsset.php file

<?php
namespace frontend\assets;
use yii\web\AssetBundle;
/**
 * Main frontend application asset bundle.
 */
class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        // add your bootstrap or any other css file path
        // example
        'css/bootstrap.css',
        'css/agency.css',
        'css/site.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        // 'yii\bootstrap\BootstrapAsset', remove / comment this
        // as you don't want to use default bootstrap
    ];
}

it should solve your issue

1 Comment

This is a bad idea because conflicts will arise that are not noticeable at first glance. For example, with the “Alert” widget out of the box. This will override the styles after reboot. So it's better to set your own Bootstrap styles in the assetsManager configuration. So, the best way to do this is described here: github.com/yiisoft/yii2-bootstrap5/blob/master/docs/guide/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.