1

I'm using ng2-select2 in my Angular2 project and I want to add some style such as width, height, border-radius, font-weight, font-size, ... but I couldn't manage it.

My Html code is like:

<select2 [data]="exampleData"></select2>

I also tried adding class and id to my select2 element, but it doesn't work. Do you have any idea how can I do that?

3 Answers 3

5

If you want to use "width", you can use it with Select2Options class.

In your component just define one object of Select2Options:

let options: Select2Options;

Set the attribute that you want:

this.optionsSelect = {
  placeholder: "Select option...",
  allowClear: true,
  width: "100%"
}

In your html, put the [options] input with the name of your option:

<select2 [data]="seccion" [cssImport]="true" (valueChanged)="onSeccionChange($event)" [options]="optionsSelect"></select2>

With this class you can customize some things, for example: add a placeholder, or allow to keep the select without selection.

In my case , I'm trying to change background-color but I can't find a solution.

Sign up to request clarification or add additional context in comments.

1 Comment

For your background case, you could add in the optionsSelect the attribute dropdownCss with the style you need, e.g.: dropdownCss: {backgroundColor: 'red'}
1

You could try to customize component

    @Component({
            selector: "your-component",
            templateUrl: "your-template.component.html",
            styles: [`
                    :host >>> .select2-container {
                                min-width: 1000px;
                    }
            `]
    })

Comments

0

you have to use width attribute as mentioned there in the repo description like this

<select2 [width]="dynamic_width"></select2>

refer from here

https://github.com/NejcZdovc/ng2-select2#inputs

else you can use ngStyle or ngClass for dynamic binding to style (PS:- Not Tested)

4 Comments

Yes, But just width, and it should only be used inline. It doesn't work if you put it to the css file
no it will not work in css you have to set in HTML itself, also Just width because author of repo mentioned only width there. see the attached link @Kavoos
@pardeepjain_90, I kinda find a solution! I need to open Chrome console. There you can see that the <select2> element converted to different element and I have to find the right class they use, and then overwrite them. Anyhow it's very annoying!
great if you find out the solution, yes the only option left is to overright the default class of element.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.