Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding style to asp.net label

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 ?

like image 948
cool_spirit Avatar asked Nov 26 '13 16:11

cool_spirit


People also ask

How can we call CSS class in code behind in asp net?

If you want to add attributes, including the class, you need to set runat="server" on the tag. Thanks, I was sure it would be this simple. @Tyler, no. This adds a new class name to the control.

What is Label control in asp net?

ASP.NET Web Forms Label This control is used to display textual information on the web forms. It is mainly used to create caption for the other controls like: textbox. To create label either we can write code or use the drag and drop facility of visual studio 2017.


2 Answers

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" />
like image 127
Abhitalks Avatar answered Nov 13 '22 09:11

Abhitalks


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" />
like image 41
Irfan TahirKheli Avatar answered Nov 13 '22 11:11

Irfan TahirKheli