CSS: how to set the width of form control so they all have the same width?
Asked Answered
R

4

6

Consider the following example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <style type="text/css">
            div { width: 15em }
            input, textarea, select { width: 100%;
                -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box }
        </style>
    </head>
    <body>
        <form>
            <div>
                <input value="Input">
            </div>
            <div>
                <textarea>Text area</textarea>
            </div>
            <div>
                <select>
                    <option>One</option>
                    <option>Two</option>
                    <option>Three</option>
                </select>
            </div>
        </form>
    </body>
</html>

On browser that support the border-box box sizing, this is rendered as I want:

Correct rendering http://img.skitch.com/20100522-c75mhdut2q32yc7u5r2tkft1n4.png

On IE 6/7, however, this is rendered as:

IE 6/7 rendering http://img.skitch.com/20100522-f5pkgnwwceaak3t8fqq2w16gfm.png

How can I get the same rendering in IE 6/7 that I get in other browsers, without resorting to setting sizes in pixels?

Ritual answered 22/5, 2010 at 0:20 Comment(1)
Mind if I suggest cross-posting this question to doctype.com ?Quartz
C
1

This is not possible with CSS. I did some research and I found out that the same question was asked before here. The solution is to use this boxsizing.htc file and add the following line to your HTML head:

<!--[if lt IE 8]><style>input, textarea { behavior: url("boxsizing.htc"); }</style><![endif]-->
Contempt answered 22/5, 2010 at 1:34 Comment(0)
M
2

The solution is to use CSS and JavaScript to replace form controls that browser vendors have conspired to make a pain to style. A select is just a drop down menu with an onmouseup event. JS-driven text editors (rich and simple) which can replace a textarea abound online. There are even libs just for this purpose. (Example)

Mufinella answered 22/5, 2010 at 2:3 Comment(0)
M
1

A quick fix could be to IE6/7 browser-hack it with something like select { *width:102.5%; _width:102.5%; } though this may not line up to the pixel if too wide.

Moneychanger answered 22/5, 2010 at 0:57 Comment(1)
The problem with this, is that it locks you to a given font size. Let's say I add a select { *width:102.5% }. Perfect, now the form fields have the same width: img.skitch.com/20100522-ght27dws54jqp8r79bf5nf1hus.png. But now add a body { font-size: 200% }. With the same select { *width:102.5% }, you get: img.skitch.com/20100522-ksie3bm45e83g314869fdhby29.png. So that percentage you put in select { *width:102.5% } would depend on the font size. As your font size changes, you'll need to go change that percentage. Rather than this, I'd prefer to go with a width in pixels.Ritual
C
1

This is not possible with CSS. I did some research and I found out that the same question was asked before here. The solution is to use this boxsizing.htc file and add the following line to your HTML head:

<!--[if lt IE 8]><style>input, textarea { behavior: url("boxsizing.htc"); }</style><![endif]-->
Contempt answered 22/5, 2010 at 1:34 Comment(0)
Z
0

Maybe this could help you: CSS3 support for Internet Explorer 6, 7, and 8. From the page:

IE-CSS3 is a script to provide Internet Explorer support for several of the popular new styles available in the upcoming CSS3 standard.

Zither answered 22/5, 2010 at 0:36 Comment(1)
Unfortunately, this htc doesn't include support for box-sizing.Contempt

© 2022 - 2024 — McMap. All rights reserved.