Limit Initial width of select list

前端 未结 6 1107
春和景丽
春和景丽 2021-01-12 20:17

I have a select list, where some clients have entered insanely long options for them, which breaks the design. I\'m wondering if there is a way to set a fixed width on the s

相关标签:
6条回答
  • 2021-01-12 20:32

    Since you don't specify what browser support you need, this may or may not be a solution. This works in most browsers IE9+

    select {
        width: 100%;
        max-width: 150px;
    }
    

    Short and sweet. When expanded, the select will display the whole text (unless it exceeds the viewport!).

    0 讨论(0)
  • 2021-01-12 20:33

    I don't know if you can do it with css (browser independent), but here are 2 other solutions:

    1. Instead of displaying "veeeeery looooooooooong teeeeeeeeext" you can display something like "veeeeery loooo...";

    2. Build your select using divs and lists so when it is closed to have a specific width and when you press something like an arrow to display full width. (I am not sure you understand what I'm trying to say with this one....).

    0 讨论(0)
  • 2021-01-12 20:37

    Not sure if you're open to using jQuery at all but I usually use this method:

    http://css-tricks.com/select-cuts-off-options-in-ie-fix/

    Is a bit choppy but looks better than cutting off the content

    0 讨论(0)
  • 2021-01-12 20:38

    See the working example here.

    You just need to apply width to your select box either inline or CSS. It will be as wide as you have specified but when clicked, it will show all options with whatever width.

    0 讨论(0)
  • 2021-01-12 20:52

    If the list you have (the entries in <select>) are user entered, and the user can enter, say 500 characters, they they definiteky will.

    In this case, I would not go for a <select> list, but a custom list built with, say a <div>.

    This is not difficult, all you need is

    a div that contains the default option,
    a hidden div with all the options
    When the user clicks the default option show the hidden div
    On click of the items in the hidden div (that is now visible) make that the selected item in the first div

    Perhaps there already jquery plugin for this. but i am not sure whether you are open to jquery, I am not a jquery expert anyway.

    I know this comparitively more effort than having a select, but i think it is worth the effort. All the hacks - expand the div onmouseover, onclick etc do not look great, might still break your design, and depending on the amount of data the user can enter, would still not be effective.

    In the custom list approach, you can wrap the elements, so that you are in complete control.

    0 讨论(0)
  • 2021-01-12 20:54

    Add to the CSS something like this:

    select {
        border: 1px solid #838383;
        border-style: outset;
        font-weight: bold;
        color: #3F3F3F;
        max-width: 150px !important;
        overflow: hidden;
    }
    
    option {
        max-width: 120px !important;
        overflow: hidden;
    }
    

    This works for me. Note there are no dots in front of the anchors.

    0 讨论(0)
提交回复
热议问题