2

I am trying to apply css class style to my @Html.TextBoxFor control in mvc3 razor view. But I am not able to see the defined style applied to the control. Below is my code:

In my .cshtml file I have the control as:

@Html.TextBoxFor(model => model.project.ProjectName, new { @class = "myStyle"})

In .css file I have defined the style as:

.myStyle
{
    width: 150px;
    font-family:Arial, Helvetica, sans-serif;
    font-size: 1.00em;
}

What am I doing wrong? Can anyone help me please?

Thanks, Balaji

6
  • And the generated textbox doesn't have class="myStyle"? Or is there a possibility the CSS file is missing? Commented Jan 11, 2013 at 15:15
  • The code is correct, check if you are including. Css file correctly, in layout or view. Try adding a HTML <input type="text" class="myStyle" /> to see if takes the style... Commented Jan 11, 2013 at 15:16
  • Thanks Brad for the quick turn around. The css file is existing as I am getting styles for other controls on the form. Commented Jan 11, 2013 at 15:20
  • 1
    Thanks Mate for your suggestion. I tried that too and the result is same. No styles. I tried removing all styles for my site and created a new css with only one style and tried to apply that and again the result is same. Commented Jan 11, 2013 at 15:21
  • :) Ok, check answer's @Shyju. And Maybe you could try to put background-color: #F00; (just in case). You could "inspect" elements with firebug or chrome to check html/css rendering Commented Jan 11, 2013 at 15:32

2 Answers 2

2

If you are sure that your style sheet is properly loaded to the page (check the path to style sheet is correct), possible reason for the probelm could be, some other styles are overriding your defined style. Use IMPORTANT to make this override everything else

input.myStyle
{
    width: 150px !IMPORTANT;
    font-family:Arial, Helvetica, sans-serif !IMPORTANT;
    font-size: 1.00em !IMPORTANT;
}
Sign up to request clarification or add additional context in comments.

Comments

0

The .cshtml code looks correct to me. I would inspect the HTML source that gets generated to verify that class="myStyle" is present on the ProjectName input. If it is present, then the problem lies with your CSS (e.g. another style may be overriding myStyle).

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.