473,387 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 39277
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******@NOSPAMbetadome.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 <optionelement
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.stylesheets. 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.tut.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 <optionelement
is by default an inline element, so a conforming browser will ignore a width
setting it even for formal reasons.)
But a <selectelement, in the "informative" 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.stylesheets.
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 <optionelement 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 <selectelement 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 <selectelement is guaranteed to
produce better results than applying it to the <optionelement.
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******@NOSPAMbetadome.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
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...
6
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"...
2
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...
2
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...
3
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...
6
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...
6
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...
5
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"> ...
9
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"...
3
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.