Yii2: How to change font-family of Select2 widget

痞子三分冷 提交于 2020-01-06 07:56:10

问题


I have a select2 widget and I need to change its font-family.

<?= $form->field($model, 'my_number')
    ->widget(Select2::classname(), [
        'items' => ['One' => 'One', 'Two' => 'Two', 'Three' => 'Three'],
        'class' => 'form-control',
]) ?>

Bootstrap doesn't have a class to change it so I think I need to change its style.


回答1:


you can add style to it like follows:

<?= $form->field($model, 'my_number')
->widget(Select2::classname(), [
    'items' => ['One' => 'One', 'Two' => 'Two', 'Three' => 'Three'],
    'class' => 'form-control',
    'options' => ['style' => 'font:yourfontfamilly'],
]) ?>



回答2:


As you are using Kartik Select2 and to customize the Font-family for the select2 you need to update the following classes that are added in the select2-krajee.css file.They are divided into 2 groups if you want to change the font-family

Changing Font Family

For the Placeholder and selected Option

.select2-container--krajee .select2-selection{
   font-family:Roboto, sans-serif !important;
}

For the Drop-down Options

.select2-container--krajee .select2-results__option{
   font-family:Roboto, sans-serif !important;
}

Changing Font color/size

For the Selected option

.select2-container--krajee .select2-selection--single .select2-selection__rendered{
    color:red !important;
}

For the Default Placeholder

.select2-container--krajee .select2-selection--single .select2-selection__placeholder{
    color:red !important;
}

For changing the color for the dropdown option the same class is used that is listed for the font-family option in the first section on top, i.e .select2-container--krajee .select2-results__option.

Hope it helps



来源:https://stackoverflow.com/questions/54597568/yii2-how-to-change-font-family-of-select2-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!