Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

html - Default input element size

The input element by default is size="20" if this is not defined inline or a CSS style rule is attributed to it.

How do you make the default the actual size of the value? For other elements:

<div style="display: inline; width: auto;">
    The box will only fit the width of it's content
</div>

width: auto is all you need. I cannot put a defined width since the value may be longer. For example:

<input id="short" type="text" value="1000" />
<input id="long" type="text" value=" Areally long name is in this input field." />

I want #short to be the size of 1000 (essentially size="4" + the padding) but the #long to be as long as needed. These inputs are coming in programatically so I can't simply put a short class on the one's I want short and a long class on the one's I want long. I would really just like it if I could put:

input { width: auto; }

Anyway to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The input element by default is size="20"

This "default size" depends on the browser, the version and your OS. It is not possible to do this in inline styles.

the best solution is to set width and padding so it adds up to 100%

input {
  width: 98%;
  padding: 1%;
}

then set it to absolute left and right 0

<fieldset>
    <input type="text" />
</fieldset>

finally add this css

<style type="text/css">
    fieldset {
      position: relative;
    }

    input {
        position: absolute;
        left: 0;
        right: 0;
    }
</style>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...