20

how do you change the width of a textbox in an asp.net-mvc View

i want to have these fields side by side and the state textbox much shorter width

            <p>
                <label for="city">City:</label>
                <%= Html.TextBox("city")%>
                <label for="state">State:</label>
                <%= Html.TextBox("state")%>
            </p>

EDIT:

none of the below answers seem to work for me. I noticed that in site.css i see this:

fieldset p 
{
    margin: 2px 12px 10px 10px;
}

fieldset label 
{
    display: block;
}

fieldset label.inline 
{
    display: inline;
}

legend 
{
    font-size: 1.1em;
    font-weight: 600;
    padding: 2px 4px 8px 4px;
}

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

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

how do i override this behavior for one field (textbox)

1
  • inline styles will allow you to override the style from the css class. When you are trying to specify a one-off style for a particular html element, inline styles are perfect. The trick is to remember to propagate them back to the css class if they become common throughout your html and are no longer "one-off". For instance, if all of your textboxes need to be 300px wide, then just go change the css class. Commented Jun 30, 2009 at 14:23

5 Answers 5

32

I would use the helper signature that takes HTML attributes and assign it a CSS class. You would then use CSS to achieve the desired look and feel.

 <%= Html.TextBox( "state", null, new { @class = "small-input" } ) %>
Sign up to request clarification or add additional context in comments.

6 Comments

Note that "@class" needs the @ (literal) symbol, because class is a reserved word.
@mgroves, absolutely correct. Thanks for clarifying. Also, note that I'm using null for the default value. This will keep the same behavior for the default value (comes from Model or ViewData) that the version without the extra parameters has.
this doesn't seem to do anything. is that because i have <p> around it
please see updated question. it looks like some other css is overriding the answer above. how can i override this css ?
Add something like input[type=text] .small-input { width: 50px; } after the input[type=text] line in site.css.
|
22
<%= Html.TextBox("state", null, new { @style = "width: 300px;" })%>

2 Comments

But if you are trying to just set the width quickly, as a one-off setting, this will work. I also think there are plenty of places where using inline styles is perfectly acceptable. But I also understand your objection to inline styles.
the inline solution seems to be the only thing that trumps this external css
1

css

.yourcssclassname{width:50px;}

html

<%= Html.TextBox("city", null, new{@class="yourcssclassname"})%>

that should do it... you could also obviously send along a style attribute, but css classes are a better path to choose.

string.Empty represents the default value.

Edit: see tvanfosson's post

fixed answer so it solves the problems mentioned in the comments.

3 Comments

If you use null instead of string.Empty it will pull the default value from the Model or ViewData if available.
shouldn't it be: <%= Html.TextBox("city", string.Empty, new{@class="yourclassname"})%> instead of: <%= Html.TextBox("city", string.Empty, new{@class="yourcssclassname"})%>
thanks for the tip tvanfosson I didn't know that! You're right about the typo in the css classname "me" :-)
0

You can use the TextBox helper to add any arbitrary attibutes to your textbox. For instance:


<%= Html.TextBox( "state", null, new { size = "10" } ) %>

Will render something like:


<input type="text" name="state" size="10" />

2 Comments

This doesn't work for me. Does it work for anyone else? IE seems to ignore the setting.
Well, it's rendered server-side, so it shouldn't matter what browser you're using.
0

If using the default MVC template that ships with VS. Explictly specifying a class in the html attributes of the textbox doesn't seem to override Site.css, however, an inline style does work. So:

{@style= "width: 400px;"} 

will work, whereas this wont:

{@class="myclass"}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.