Ritz
Ritz

Reputation: 97

How to apply a css class on a MVC Html.TextBox

I have the following tag in my MVC Application. How can I apply a class to my textbox?

<%= Html.TextBox("username", "", new { maxlength = 50 })%>

Upvotes: 0

Views: 665

Answers (3)

DM.
DM.

Reputation: 1847

Based on your code, I believe the Html.TextBox helper method will also automatically create an id from the name field:

id="username"

You could then access the text box via CSS with:

#username {
    /* styles */
}

Upvotes: 0

fernyb
fernyb

Reputation: 983

assuming the class name of the input box is "username" then use:

input[type=text].username {
  /* styles here */
}

Upvotes: 0

Doug Neiner
Doug Neiner

Reputation: 66221

<%= Html.TextBox("username", "", new { maxlength = 50, @class = 'your-classname' })%>

Then in your CSS file you would use this selector:

.your-classname { ...css rules here... }

Upvotes: 10

Related Questions