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
cssClass property
Add this lblCommentText.Attributes.CssStyle.Add("float", "right");
to backend
using javascriptdocument.getElementById('<%= lblCommentText.ClientID%>').Style.display = ("float","right");
and also in-line style to the element
none of them works, can someone help me out ?
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.
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.
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" />
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With