473,574 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there a way to set the width of <option> in a <select>?


--
iTech Consulting Co., Ltd.
Expert of ePOS solutions
Website: http://www.itech.com.hk (IE only)
Tel: (852)2325 3883 Fax: (852)2325 8288
Mar 8 '07 #1
4 39333
Man-wai Chang wrote:
[from the subject line:]
Is there a way to set the width of <optionin a <select>?
You could use styles, like this:

<select style="width: 200px">
or:
<option style="width: 200px">

Both will work syntactically, but the former is guaranteed to produce
the desired effect in all CSS-conforming browsers.

You can also put this in your stylesheet instead of cluttering up the
markup with individual styles (especially if there are more than one
element that needs this width).

--
Kim André Akerĝ
- ki******@NOSPAM betadome.com
(remove NOSPAM to contact me directly)
Mar 8 '07 #2
Scripsit Kim André Akerĝ:
You could use styles, like this:

<select style="width: 200px">
or:
<option style="width: 200px">
But what you _should_ do (assuming it's a good idea to set the width in the
first place - it seldom is) is to place a style sheet rule like
select { width: 15em; }
into your CSS file.
Both will work syntactically, but the former is guaranteed to produce
the desired effect in all CSS-conforming browsers.
No, you cannot guarantee such things. CSS is just optional presentational
suggestions, both in theory and in practice. (Besides, an <optionelemen t
is by default an inline element, so a conforming browser will ignore a width
setting it even for formal reasons.)
You can also put this in your stylesheet instead of cluttering up the
markup with individual styles (especially if there are more than one
element that needs this width).
Well, yes.

But you should also consider _why_ you want to set the width of <select>.
It's almost always based on a wrong analysis and will create problems rather
than solve them.

P.S. Style sheet issues belong to c.i.w.a.stylesh eets. There is no way to
set the width of a select or option element in HTML, except in the sense
that the width is affected by the content length of the longest option. Old
versions of Netscape recognized a width="..." attribute in <select>, but
even modern descendants of Netscape, like Firefox, don't support it any
more.

--
Jukka K. Korpela ("Yucca")
http://www.cs.tut.fi/~jkorpela/

Mar 8 '07 #3
On 2007-03-08, Jukka K. Korpela <jk******@cs.tu t.fiwrote:
Scripsit Kim André Akerĝ:
>You could use styles, like this:

<select style="width: 200px">
or:
<option style="width: 200px">

But what you _should_ do (assuming it's a good idea to set the width in the
first place - it seldom is) is to place a style sheet rule like
select { width: 15em; }
into your CSS file.
>Both will work syntactically, but the former is guaranteed to produce
the desired effect in all CSS-conforming browsers.

No, you cannot guarantee such things. CSS is just optional presentational
suggestions, both in theory and in practice. (Besides, an <optionelemen t
is by default an inline element, so a conforming browser will ignore a width
setting it even for formal reasons.)
But a <selectelemen t, in the "informativ e" CSS 2.1 default stylesheet,
is display: inline-block, on which you can set a width.

This is probably why Kim André Akerĝ said setting width on <select>
rather than on <optionshould work in CSS-conforming browsers. Sounds
like good advice.

[...]
P.S. Style sheet issues belong to c.i.w.a.stylesh eets.
I'll change the Followup-To then (is that the correct thing to do in
this situation?)
Mar 8 '07 #4
Jukka K. Korpela wrote:
Scripsit Kim André Akerĝ:
You could use styles, like this:

<select style="width: 200px">
or:
<option style="width: 200px">

But what you should do (assuming it's a good idea to set the width in
the first place - it seldom is) is to place a style sheet rule like
select { width: 15em; } into your CSS file.
Both will work syntactically, but the former is guaranteed to
produce the desired effect in all CSS-conforming browsers.

No, you cannot guarantee such things. CSS is just optional
presentational suggestions, both in theory and in practice. (Besides,
an <optionelemen t is by default an inline element, so a conforming
browser will ignore a width setting it even for formal reasons.)
Setting the width to the <selectelemen t rather than the <option>
element is preferred if you really need to set the width at all.

However, <optionWILL syntactically accept a width, although browsers
will interpret this differently. For browsers that support CSS
properly, applying a width to the <selectelemen t is guaranteed to
produce better results than applying it to the <optionelemen t.
You can also put this in your stylesheet instead of cluttering up
the markup with individual styles (especially if there are more
than one element that needs this width).

Well, yes.

But you should also consider why you want to set the width of
<select>. It's almost always based on a wrong analysis and will
create problems rather than solve them.
Say that to the OP. All I pointed out, is how to do it. In any case,
using styles is his/her only valid option for setting the height and
width for any elements aside from <table>, <colgroup>, <col>, <object>,
<img>, as well as for frames (*shudder*). (I just double-checked the
DTD document to be sure.)

--
Kim André Akerĝ
- ki******@NOSPAM betadome.com
(remove NOSPAM to contact me directly)
Mar 8 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2385
by: Ang Talunin | last post by:
Hey, I wondering if it's possible to retrieve all the <option>-fields from a <select> when posting a <form> to a php file Example: I've got a form like this: <form action = phpfile.php method=post > <select name= "name">
6
1705
by: George Hester | last post by:
Try it. Using Microsoft Internet Explorer 5.5 SP2 (that's the version I am using) in Windows 2000 SP3: In a html put this in: <select type="select-one" id="oSel1"> <option value="1" onmouseover="JavaScript:window.status='Hello'; return true;">Hello</option> <option value="2" onmouseover="JavaScript:window.status='Bye'; return...
2
7114
by: LC's No-Spam Newsreading account | last post by:
I have a form arranged in a table (you can see an example in the page http://cosmos.mi.iasf.cnr.it/~lssadmin/Website/LSS/Help/query.html) The table is on three columns but has a structure like this. The part I'm concerned with is the cell FFFFF which contains a multiple <SELECT> list of <OPTION>s. As you can see the first two rows span more...
2
1923
by: Craig Keightley | last post by:
How do I retrive the Name of the item in a select box I have the following form <select name="select"> <option value="1">Monday</option> <option value="2">Tuesday</option> <option value="3">Wednesday</option> <option value="4">Thursday</option> </select>
3
18559
by: Iain Hallam | last post by:
Hi. I've been using display:none on the style property of some <option> elements in my forms, which works fine with Mozilla - as expected it removes the option from my dropdown (although it still exists in the code). Is there an equivalent in IE? The reasoning behind this is that I want users to rank objects using a <select> for each...
6
2396
by: Bonge Boo! | last post by:
This has got to be obvious, but I can't make it work. I have a form called with 3 pull down menus. They are linked to a database which generates the values for the <SELECT? Pull-downs. Lets say I have values selected for all three pull down menus. When I change the first "top-level" menu I want to reset both the second and third menus...
6
19591
by: joseph.lindley | last post by:
Forgive me for I am a bit of a web-dev novice - but I'm not doing too bad. I'm currently working with a bit of javascript to dynamically add <option>s into a select box. My code currently works fine in Internet Explorer, however in Firefox the dropdown only displays the first option in the list, and when clicked the other values aren't...
5
1867
by: bb nicole | last post by:
Below is the list menu of search engine.. How to code if i want to put <option selected>ALL</option> Interface <tr> <td>Job Category:</td> <td><select name="jobCategory"> <option selected></option>
9
5776
by: eihabisaac | last post by:
hey every body how can i get using javascript the selected option <option> ID or Value because when ever i clicked it i get the <select> id and i dont want that <select name="list" id="list" size="10%" > <option id="3" ondblclick="select_song();">Apple <br> </option> <option id="4" ondblclick="select_song();">Orange <br> </option> ...
3
2711
by: sreemathy2000 | last post by:
I have the below code in the form for the select dropdown in my web page. how can i get the selected text from javascript. i was able to get the value, but text shows as undefined. <SELECT name="queue" id="queue"><OPTION selected value="0">Select reason</OPTION><option value="Residential Tier 1 Chat" text="Account Information">Account...
0
7736
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8066
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7826
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8106
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3743
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2251
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1350
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.