问题
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