Justify text in a Label control
The Label class Represents a label control, which displays text on a Web
page. The asp.net developers can use the Label control to display text in a
set location on the web page. Unlike static text, asp.net c# developers can
customize the displayed text through the Text property.
The following asp.net c# tutorial code demonstrates how we can justify the text on a Label web server control. To justify text in a Label control, the asp.net c# developers have to apply a custom CSS style to a Label control.
The Label CssClass property gets or sets the Cascading Style Sheet (CSS) class rendered by the Web server control on the client. The CssClass property is inherited from WebControl. The CssClass property value is a String which is the CSS class rendered by the Label server control on the client. The default value of this property is Empty.
So using this CssClass property the asp.net c# developers can justify text in a Label control. The asp.net developers just have to set the CSS text-align style value to justify.
There is another way to justify Label web server control’s text. The asp.net c# developers have to add a Style property to a Label control where they can use the Style property Add() method to add the text-align CSS style with justify value.
The Label Style property gets a collection of text attributes that will be rendered as a style attribute on the outer tag of the Web server control. This property is also inherited from WebControl. The Style property value is a CssStyleCollection that contains the HTML style attributes to render on the outer tag of the Web server control.
The following asp.net c# tutorial code demonstrates how we can justify the text on a Label web server control. To justify text in a Label control, the asp.net c# developers have to apply a custom CSS style to a Label control.
The Label CssClass property gets or sets the Cascading Style Sheet (CSS) class rendered by the Web server control on the client. The CssClass property is inherited from WebControl. The CssClass property value is a String which is the CSS class rendered by the Label server control on the client. The default value of this property is Empty.
So using this CssClass property the asp.net c# developers can justify text in a Label control. The asp.net developers just have to set the CSS text-align style value to justify.
There is another way to justify Label web server control’s text. The asp.net c# developers have to add a Style property to a Label control where they can use the Style property Add() method to add the text-align CSS style with justify value.
The Label Style property gets a collection of text attributes that will be rendered as a style attribute on the outer tag of the Web server control. This property is also inherited from WebControl. The Style property value is a CssStyleCollection that contains the HTML style attributes to render on the outer tag of the Web server control.
label-justify-text.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
Label1.CssClass = "LabelJustifyTextStyle";
//another way to apply label justify text.
//Label1.Style.Add("text-align","justify");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<style type="text/css">
.LabelJustifyTextStyle {
text-align:justify;
}
</style>
<title>asp.net example - label justify text</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - label justify text
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's..."
BorderColor="Red"
BorderWidth="1"
Font-Size="X-Large"
Width="350"
>
</asp:Label>
<br /><br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="label justify text"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>