简体   繁体   English

输入宽度在css级别和元素级别定义

[英]Input width defined at css level and at the element level

<style>

input[type="text"] 
{
    width: 200px;
    border: 1px solid #CCC;
}

</style>
<body>

<input type="text" name="test" value="test" size="5" />
</body>

When this is viewed on browser (Firefox 5), the "size" it appears is a fixed 200px. 当在浏览器(Firefox 5)上查看时,它出现的“大小”是固定的200px。 It seems the size I specified is completely ignored. 似乎我指定的大小完全被忽略了。

This is from a MVC project, and that is the default style css that comes with it. 这是来自MVC项目,这是它附带的默认样式css。 For the time being, I took off the width: 200px. 暂时,我取下宽度:200px。 But is there a way to keep it there, and override it at the "input" level? 但有没有办法让它保持在那里,并在“输入”级别覆盖它?

I also tried 我也试过了

<input type="text" name="test" value="test" width="50px" />

It did not override at all. 它根本没有覆盖。

If you want to override the CSS width at the element level, try applying the style tag to the element directly: 如果要在元素级别覆盖CSS宽度,请尝试直接将样式标记应用于元素:

<input type="text" name="test" value="test" style="width: 50px;" />

This works for me: 这对我有用:

<html>
   <style>

      input[type="text"] 
      {
         width: 200px;
         border: 1px solid #CCC;
      }
</style>
<body>
      <input type="text" name="test" value="test" style="width: 50px;" />
</body>
</html>

Edited to Add: 编辑添加:

Yes, mixing HTML w/CSS is considered a no-no, so the proper solution would be to extract it out to a class in the end: 是的,混合HTML w / CSS被认为是禁忌,所以正确的解决方案是将它最终提取到一个类:

.my_text_box {
    width: 50px;
 }

Then: 然后:

<input type="text" name="test" value="test" class="my_text_box" />

Don't set display style in the HTML. 不要在HTML中设置显示样式

Use: 使用:

<input class="narrowInput" type="text" name="test" value="test" />

Or: 要么:

<input id="narrowInput" type="text" name="test" value="test" />

Only don't use "narrowInput". 只是不要使用“narrowInput”。 Use a name that has a business meaning, that is: one that describes the purpose of the input data. 使用具有业务含义的名称,即:描述输入数据用途的名称。

Then the CSS is: 然后CSS是:

input.narrowInput
{
    width: 50px;
}

Or: 要么:

#narrowInput
{
    width: 50px;
}

Remove either 删除

input[type="text"] 
{
    width: 200px; //THIS
}

or 要么

<input type="text" name="test" value="test" size="5" <--//THIS />

Choose to style using one or the other. 选择使用其中一种来设计样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM