question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Select] Make width of select box as long as label.

See original GitHub issue
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

Select boxes have the same width as the label for the specific select box.

Current Behavior

Select box always has a width of 38px, no matter which label.

Context

I created a select box like this

const formControlOptions = {
    disabled: requirementFilter.values.length <= 1,

};

<FormControl {...formControlOptions}>
    <InputLabel htmlFor={requirementFilter.id}>
        {requirementFilter.label}
    </InputLabel>
    <Select key={requirementFilter.id}
            value={this.state.selectedFilters[requirementFilter.id] || ""}
            inputProps={{
                name: requirementFilter.id,
                id: requirementFilter.id,
            }}
            onChange={this.handleRequirementFilterSelected}
            children={requirementFilter.values.map((value: RequirementFilterValue) => {
                return (
                    <MenuItem value={value.id} data-parent={value.parent}>
                        {value.label} ({value.item_count})
                    </MenuItem>
                );
    })}/>
</FormControl>

This creates a selectbox, that looks like this

image

However, I want it to look like this

image

I could set the min-width, however, I want all select boxes to have a dynamic width, because some labels are shorter, therefore I also want a lower min-width for other select boxes, so min-width is not a possible solution.

Your Environment

Tech Version
Material-UI v1.0.0-beta.39
React 16
browser Chrome (latest)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
kobelobstercommented, Apr 5, 2018

Okay, thanks to you pointing out that the label is the problem, I tried to do this

label {
  position: relative;
  padding-right: 25px;
  &+div{
    margin-top: 0;
    &:before {
      bottom: 5px;
    }
    &:after {
      bottom: 5px;
    }
    svg {
      top: 0;
    }
  }
}

This leads to the expected result. Since I’m no CSS expert, do you see any major problems with that solution? It’s resolution independent because we display this site only to users with a specific resolution.

2reactions
kobelobstercommented, Apr 6, 2018

Ah, I think there was a misunderstanding. I was just pointing out how I solved this, not how the library should solve this! So, this wasn’t a suggestion, but merely a description how I implemented it in case someone else stumbles upon this issue.

Thanks for your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to control the width of select tag? - html
You need to use max-width instead. This way, it will let the select menu expand to its longest option, but if that expands...
Read more >
[Select] Make width of select box as long as label. #10917
Select box always has a width of 38px, no matter which label. Context. I created a select box like this. const formControlOptions =...
Read more >
How to Expand Select Box to its Default Width on Focus ...
Answer: Use the CSS :focus pseudo-class. By default the size of the <select> element is depend on the size of the largest <option>...
Read more >
Controlling the width of SELECT lists
The default width of a SELECT form control is usually dependent on the width of the widest OPTION item in the list; the...
Read more >
How To Set Width Of Select Option In HTML
In this tutorial we will show the solution of how to set width of select option in HTML, in HTML there is a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found