CSS 选择选项宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18889478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select option width
提问by John Makii
Im not really experienced in CSS and i have no tools that are best used in designing webpages such as microsoft expression.
我在 CSS 方面并没有真正的经验,而且我没有最适合用于设计网页的工具,例如 microsoft expression。
My problem is simple, its somehow vain.. neways,
我的问题很简单,不知何故是徒劳的..neways,
i want my page to look symmetrical as possible so i want my select options to be of same width. I've searched and the best solution is to use width="" option on css. so how do i go about this?
我希望我的页面看起来尽可能对称,所以我希望我的选择选项具有相同的宽度。我已经搜索过,最好的解决方案是在 css 上使用 width="" 选项。那么我该怎么做呢?
have this example:
有这个例子:
<tr>
<td><label for="meal">Meal:</label></td>
<td>
<select name="meal">
<option selected="selected" value="0">Any</option>
<option value="Breakfast">Breakfast</option>
<option value="Brunch">Brunch</option>
<option value="Lunch">Lunch</option>
<option value="Snack">Snack</option>
<option value="Dinner">Dinner</option>
</select>
</td>
<td><label for="cooking">Cooking:</label></td>
<td>
<select name="cooking">
<option selected="selected" value="0">Any</option>
<option value="Baked">Baked</option>
<option value="BBQ">Barbecque</option>
<option value="Boiled">Boiled</option>
<option value="Dfried">Deep Fried</option>
<option value="Grilled">Grilled</option>
<option value="Steamed">Steamed</option>
</select>
</td>
回答by Jukka K. Korpela
If you want to set the same width on all select
elements in a document, you can write e.g.
如果要select
在文档中的所有元素上设置相同的宽度,您可以编写例如
<style>
select { width: 5.5em }
</style>
in the head
part. If you wish to do that for some select
elements only, you need to replace the selector select
by something more restrictive, e.g. select.foo
to restrict the effect to those select
elements that have the class=foo
attribute.
在head
部分。如果您select
只想对某些元素执行此操作,则需要将选择器替换为select
更具限制性的内容,例如select.foo
将效果限制在select
具有该class=foo
属性的元素上。
This is tricky because you need a width that is sufficient for all options, and you probably want to get as close to the maximal width as possible. The widths of options depend on their text andon the font. (Using the px
makes things even worse. Then things would depend on font size, too.)
这很棘手,因为您需要一个足以满足所有选项的宽度,并且您可能希望尽可能接近最大宽度。选项的宽度取决于它们的文本和字体。(使用px
会使事情变得更糟。那么事情也将取决于字体大小。)
Consider whether it is necessary to set the widths the same. It is just an esthetic preference, not shared by all people, and it may in fact be bad for usability. A dropdown with short options should perhaps look different from another dropdown that has long options.
考虑是否有必要将宽度设置为相同。这只是一种审美偏好,并非所有人都共享,实际上可能对可用性不利。带有短选项的下拉菜单可能看起来应该与另一个带有长选项的下拉菜单不同。
回答by alejo802
For your width setting, you have a small typo. You need
对于您的宽度设置,您有一个小错字。你需要
<select style="width: 400px">
回答by Dale
In your stylesheet:
在您的样式表中:
table tr td select{width:150px;}
That will make all the selectboxes inside a table cell the same width. I would not go the inline css route.
这将使表格单元格内的所有选择框具有相同的宽度。我不会走内联 css 路线。
回答by akwasiijunior
I'd put a class on the selects and use
我会在选择和使用上设置一个类
<select class="selectList" id="meal"><option>your options</options></select
and then added this to your css
然后将其添加到您的 css 中
select.selectList { width: 150px; }
Note: label for works with id's and not name and refrain from using inline-style css ().
注意:使用 id 而不是名称的作品的标签,并避免使用内联样式的 css ()。