Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input width defined at css level and at the element level

Tags:

html

css

<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. 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. For the time being, I took off the width: 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.

like image 938
DevSharp Avatar asked Aug 07 '11 00:08

DevSharp


People also ask

Which CSS property sets the width of an element?

The width CSS property sets an element's width. By default, it sets the width of the content area, but if box-sizing is set to border-box , it sets the width of the border area.

What defines the width of the input field?

The width attribute specifies the width of the <input> element. Note: The width attribute is used only with <input type="image"> . Tip: Always specify both the height and width attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded.

How do I change the input element width?

This can be done by using the size attribute or by width attribute to set the width of an element. The size attribute works with the following input types: text, search, tel, url, email, and password and not on type date, image, time for these we can use width attribute.


1 Answers

If you want to override the CSS width at the element level, try applying the style tag to the element directly:

<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:

.my_text_box {
    width: 50px;
 }

Then:

<input type="text" name="test" value="test" class="my_text_box" />
like image 88
Evan Avatar answered Oct 03 '22 17:10

Evan