-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(select): allow auto-width based on placeholder / value #7627
Comments
This seems to workaround the differences in heights .mat-select {
height: 1.125em;
} |
I believe the height issue was caused by an |
@mmalerba it resizes to fit the form field, but form field doesn't fit its parent (which causes problems, see #7783). Anyway, what I'm talking about in this issue (regarding the width) is having select to be only the width of its content (either selected value, or placeholder).. currently it's either fixed to whatever You can force it to be only the width of the selected value (https://stackblitz.com/edit/material2-select-width-issues-vedgpg?file=styles.scss) but it's not gonna work with placeholder (when there's no value selected yet), because it's absolutely positioned, and this is hard to override, because of where the placeholder is placed in dom, as I described in the initial post. I'm currently getting around it (since I don't need floating placeholders in this particular case) by using the first option without any value set as a placeholder.. not ideal. |
Ok, I've changed the title of the issue to reflect just the width issue, since the height should be resolved |
Something else that should be noted with those css overrides is that you lose the text truncating and ellipsis when you resize the screen |
This is p3?? |
The best workaround I found to fix both value and placeholder was to use ::ng-deep and set a different min-width on .mat-select-placeholder and setting width: fit-content on .mat-select-value and mat-select. Since ng-deep will be deprecated soon this is not a permanent solution but one that works until this issue is resolved. Full example: mat-select {
width: fit-content !important;
min-width: 100%;
}
::ng-deep .mat-form-field-infix {
min-width: 75px !important;
width: fit-content !important;
}
::ng-deep mat-select .mat-select-value {
width: fit-content;
min-width: 2ch;
max-width: 25ch;
}
::ng-deep mat-select .mat-select-placeholder {
width: fit-content;
min-width: 10ch;
}
|
AFAK ng-deep is deprecated :( |
It's worth noting that you don't really need |
Why then its not working without |
View encapsulation works on the component one creates. So if you want to style another's component and yours uses encapsulation you need ng-deep. Material components using ViewEncapsulation.None means the css classes are less specific, so it's easier to override them in your assets/styles.xyz file. |
fixed width on mat-form-field
I want my select to have the width of the selected item, fixed width on mat-form-field makes it impossible, you can override
.mat-form-field
to havewidth
auto
, along with overriding.mat-select-value
to bewidth
auto
andmax-width
100%
, but then you have a problem, because placeholder is absolutely positioned, which means that the select without any value set yet will be zero width (actually 18px because of the arrow).https://stackblitz.com/edit/material2-select-width-issues?file=styles.scss
Now I could also override placeholders not to be absolutely positioned, but that's not gonna work properly either, because the arrow is inside the
md-select
element, while the placeholder is outsideI'm not sure if that 200px width is something that's on material spec, but the DOM could perhaps be structured a bit differently, in a way that wouldn't prevent devs from achieving fluid width (which is a bit closer to how native selects work)
different height when selected vs. placeholder
cc @crisbeto
The text was updated successfully, but these errors were encountered: