How to set ion-select component with 100% width in

2020-07-02 08:29发布

I would like to set my ion-select with 100% of width.

I have tried with css class like this:

.mySelect { width: 100% !important; }

But it is not working.

标签: ionic2
5条回答
虎瘦雄心在
2楼-- · 2020-07-02 08:49

The answer from Luis Antonio Pestana is good if you associate it with the code of his initial question. So to be more clear, the complete code to get the ion-select to take 100% of its container is :

.your_ion_select {
    max-width: 100% !important;
    width: 100% !important;
}
查看更多
家丑人穷心不美
3楼-- · 2020-07-02 08:59

You don't need !important at all! Ionic has specified a min-width of 45% (or whatever as per your version of ionic). All you have to do is over-ride it with a max-width of 100%. At the same time, the select, being an inline element, will only expand to fit its contents. So it won't fill the whole width. So to make this happen, you simply add width: 100% to it. So your final CSS class may look something like this:

.full-width-select {
  width: 100%;
  max-width: 100%;
}
查看更多
我想做一个坏孩纸
4楼-- · 2020-07-02 09:05

Increasing the specificity of the selector allows for doing it without the !important on it:

ion-select.myCustomSelect{
  width: 100%;
  max-width: 100%;
}

Which makes it a bit cleaner, since !important should be used as sparingly as possible, as noted in the Stack Overflow article here: Should I avoid using !important in CSS?

查看更多
姐就是有狂的资本
5楼-- · 2020-07-02 09:09

I try the each solutions but found a perfect way to do this :) just modify your sass file by adding this

ion-select{
    max-width: 70% !important;// your choice :)
}
查看更多
看我几分像从前
6楼-- · 2020-07-02 09:13

I did it.

For someone who wants the solution, here is the code:

.myCustomSelect{
  max-width: 100% !important;
}

You must have to override the 'max-width' css property.

查看更多
登录 后发表回答