简体   繁体   English

如何使用 CSS 或 JQuery 调整 HTML 选择选项下拉框的大小

[英]How to resize HTML Select options Drop Down box using CSS or JQuery

I am having an select box when i click on the select box it is displaying options but the drop down box width is exceeding the width of the select box.当我单击显示选项的选择框时,我有一个选择框,但下拉框宽度超过了选择框的宽度。 How to controle the width and height of that drop down box.如何控制该下拉框的宽度和高度。 using CSS or JQuery.使用 CSS 或 JQuery。

I have tried css by giving width attribute for .selectbox option{width: 300px;} But it did not worked.我已经通过为.selectbox option{width: 300px;}提供width属性来尝试 css 但它没有用。

is there any way that i can control the width of the option box.有什么办法可以控制选项框的宽度。 it is ok if the text of the option wraps to the next line如果选项的文本换行到下一行就可以了

Please see my code jsfiddle.net/5bt80txn请参阅我的代码jsfiddle.net/5bt80txn

You can't do it in select elements.but you can simulation select element.您不能在选择元素中执行此操作。但您可以模拟select元素。

Like this:像这样:

 $(document).ready(function(){ $('.txt,span').click(function(){ $('ul').toggle(); }) $('li').click(function(){ var text = $(this).html(); $('.txt').html(text); $('ul').hide(); }) })
 * { box-sizing: border-box; } .wrapper { width : 100px; position: relative; border: 1px solid #CCC; cursor: pointer; } .txt { width: 90%; height: 20px; padding: 0 10px 0 0; white-space: nowrap; overflow: hidden; } ul { padding: 0; position: absolute; list-style: none; border: 1px solid #CCC; margin-top: 0; display: none; z-index: 100; } li:nth-child(odd) { background-color: #CCC; } span { position: absolute; right: 0; z-index: 200; top: 0; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div class="wrapper"> <div class="txt"></div><span>▾</span> <ul> <li>Welcome</li> <li>Welcome to New World</li> <li>Welcome to my new World of Dreams</li> <li>Yes,Working!</li> </ul> </div>

No there is no way to control that aspect of <option> elements.不,没有办法控制<option>元素的这一方面。

As per MDN on the <option> element :根据<option>元素上的 MDN

Styling the <option> element is highly limited. <option>元素的样式非常有限。 Options don't inherit the font set on the parent.选项不继承父级上设置的字体。 In Firefox, only color and background-color can be set however in Chrome or Safari it's not possible to set any properties.在 Firefox 中,只能设置颜色和背景颜色,但在 Chrome 或 Safari 中无法设置任何属性。 You can find more details about styling in our guide to advanced form styling.您可以在我们的高级表单样式指南中找到有关样式的更多详细信息。

You could replace the native <select> with a custom one, which allows styling, but be careful: You will use a lot of usability and accessibility features.你可以用一个自定义的<select>替换原生的 <select> ,它允许样式,但要小心:你将使用很多可用性和可访问性特性。

Warning of custom select menus自定义选择菜单的警告

The browser is optimising the size of the options based on viewport size already, making sure the options have the proper touch target size and providing a native interface on mobile platforms.浏览器已经根据视口大小优化选项的大小,确保选项具有正确的触摸目标大小并在移动平台上提供原生界面。 It's even placing the whole menu depending on the menu's current scroll position on the viewport, opening it to the top if the menu is on the bottom.它甚至根据菜单在视口上的当前滚动位置放置整个菜单,如果菜单位于底部,则将其打开到顶部。

And last but not least, it is very accessible, it works with all kinds of user input devices (touch, keyboard, mouse, joystick, voice control), and works for screen reader users.最后但同样重要的是,它非常易于访问,它适用于各种用户输入设备(触摸、键盘、鼠标、操纵杆、语音控制),并且适用于屏幕阅读器用户。

So, if you decide that having the option-menu the same width as the select element is very important, you should at least try to find a library that provides above mentioned features as well.因此,如果您决定让选项菜单与选择元素具有相同的宽度非常重要,那么您至少应该尝试找到一个也提供上述功能的库。


You can add width with a value to select tag and the option will follow.您可以添加带有值的宽度以select标签,然后该option将随之而来。

Example: select{ width: 320px;示例:选择{ width: 320px; } }

You have to use the document object model selector option selector or give each option a class and style them.您必须使用文档对象模型选择器option选择器或给每个option一个类并设置它们的样式。 That solution worked for me:该解决方案对我有用:

HTML HTML

<select id="sel">
    <option>Option one</option>
    <option>Option two</option>
    <option>Option three</option>
</select>

CSS CSS

#sel{
    width: 300px;
    height: 40px;
}

/*700px width for example*/

#sel option {
    width: 700px; /*default would be inherit*/
    height: 60px; /*just changed for example*/ 

} }

NOTE:笔记:

This will not work in webkit browsers!这在 webkit 浏览器中不起作用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM