472,146 Members | 1,288 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 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 38946
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ang Talunin | last post: by
2 posts views Thread by LC's No-Spam Newsreading account | last post: by
2 posts views Thread by Craig Keightley | last post: by
3 posts views Thread by Iain Hallam | last post: by
9 posts views Thread by eihabisaac | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

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.