18

I want to adding style to asp.net label, but it wont work.

ASP.NET Mark up
<asp:Label runat="server" ID="lblCommentText"/>

Generated from the backend: Html mark up
<span id="ctl02_ctl36_CommentText">Only the leave the comment please</span>

............................................

I want to add the following style to the label

{
 float:right;
 width:70%;
}

I've tried using

  1. cssClass property

  2. Add this lblCommentText.Attributes.CssStyle.Add("float", "right"); to backend

  3. using javascript
    document.getElementById('<%= lblCommentText.ClientID%>').Style.display = ("float","right");

  4. and also in-line style to the element

none of them works, can someone help me out ?

2
  • 1
    display: inline-block; Commented Nov 26, 2013 at 16:15
  • Couple what @abhitalks said inside a .class and apply that class with the CssClass property on the Label. Commented Nov 26, 2013 at 16:17

4 Answers 4

24

Labels are rendered as spans and spans are basically inline elements. You need to make it block or inline-block in order to get the float and width have effect.

.yourclass {
    display: inline-block;
    float: right;
    width: 70%;
}

And then simply use cssclass:

<asp:Label runat="server" ID="lblCommentText" CssClass="yourclass" />
Sign up to request clarification or add additional context in comments.

Comments

16

Inline:

<asp:Label runat="server" ID="lblCommentText" style="float:right" />

Using class:

<style>
.styleclass{
   float: left;
}

</style>

<asp:Label runat="server" ID="lblCommentText" CssClass="styleclass" />

Using ID;

   <style>
    #ctl02_ctl36_CommentText {
       float: left;
    }

    </style>

 <asp:Label runat="server" ID="lblCommentText" />

Comments

10

If you want to add from code behind then use like below:

lblCommentText .Attributes.CssStyle.Add("float", "right");
lblCommentText.Attributes.CssStyle.Add("width", "70%");

If you want to add from aspx page then create a css class like:

.testClass{float: right;width: 70%;}

and assign like this:

asp:Label runat="server" ID="lblCommentText" runat="server" Text="test data" CssClass="testClass"

Comments

0

asp:label is being converted to span. So, you can set

span { float:left }

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.