限制选择列表的初始宽度
我有一个选择列表,其中一些客户为他们输入了非常长的选项,这破坏了设计。我想知道是否有一种方法可以在选择小部件上设置固定宽度,但它生成的下拉菜单仍然足够宽以显示所有选项。
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 select widget, but still have the drop down menu it produces still be wide enough to display all of the option.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
由于您没有指定所需的浏览器支持,因此这可能是也可能不是解决方案。这适用于大多数浏览器 IE9+
简短而甜蜜。展开时,
select
将显示整个文本(除非超出视口!)。Since you don't specify what browser support you need, this may or may not be a solution. This works in most browsers IE9+
Short and sweet. When expanded, the
select
will display the whole text (unless it exceeds the viewport!).请参阅此处的工作示例。
您只需将
width
应用于内嵌的选择框或CSS。它的宽度将与您指定的宽度一样,但单击时,它将显示具有任何宽度的所有选项。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.我不知道你是否可以使用 css (独立于浏览器)来做到这一点,但这里有另外 2 个解决方案:
1. 您可以显示类似“veeeeeery loooo...”的内容,而不是显示“veeeeeery looooooooooong teeeeeeeeext”;
2. 使用 div 和列表构建您的选择,以便当它关闭时具有特定宽度,并且当您按箭头之类的东西时显示完整宽度。 (我不确定你是否明白我想表达的意思......)。
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....).
如果您拥有的列表(
在这种情况下,我不会选择
这并不难,你所需要的只是
可能已经有 jquery 插件。但我不确定你是否对jquery持开放态度,反正我也不是jquery专家。
我知道这比选择要付出更多的努力,但我认为这是值得的。所有的技巧 - 在鼠标悬停时展开 div、onclick 等看起来不太好,可能仍然会破坏您的设计,并且根据用户可以输入的数据量,仍然不会有效。
在自定义列表方法中,您可以包装元素,以便您完全控制。
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
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.
在 CSS 中添加如下内容:
这对我有用。请注意,锚点前面没有点。
Add to the CSS something like this:
This works for me. Note there are no dots in front of the anchors.
不确定您是否愿意使用 jQuery,但我通常使用此方法:
http://css-tricks.com/select-cuts-off-options-in-ie-fix/
有点断断续续,但看起来比切断内容要好
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