asp.net example - label text align center
Label is an asp.net web server control. label control allow us to display text on web page. label control has no built in property
or method to align its displayed text on web page. so we need to take help from core css style to align label control text to center.
label control Style property get a collection of text attributes that will be rendered as a style attribute on the outer tag (label control). this property value type is System.Web.UI.CssStyleCollection which represent a CssStyleCollection that contains the html style attributes to render on the outer tag of the label web server control. so we can add css style to label control by using its Style property's Add() method.
we added the css style 'text-align' and its value 'center' to the label control by using its Style property Add() method. so the label control display its text as center aligned.
we also can apply label text to center aligned by attaching a css class to label control, after declaring a css class on web page header section. in this css section we set a css style 'text-align' and its value to 'center'.
the following asp.net c# example code demonstrate us how can we apply label text align center in an asp.net application.
label control Style property get a collection of text attributes that will be rendered as a style attribute on the outer tag (label control). this property value type is System.Web.UI.CssStyleCollection which represent a CssStyleCollection that contains the html style attributes to render on the outer tag of the label web server control. so we can add css style to label control by using its Style property's Add() method.
we added the css style 'text-align' and its value 'center' to the label control by using its Style property Add() method. so the label control display its text as center aligned.
we also can apply label text to center aligned by attaching a css class to label control, after declaring a css class on web page header section. in this css section we set a css style 'text-align' and its value to 'center'.
the following asp.net c# example code demonstrate us how can we apply label text align center in an asp.net application.
label-text-align-center.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
Label1.CssClass = "LabelTextAlignStyle";
//another way to apply css style in label control.
//Label1.Style.Add("text-align","center");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<style type="text/css">
.LabelTextAlignStyle {
text-align:center;
}
</style>
<title>asp.net example - label text align center</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - label text align center
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Text="sample label to test label text align center."
BorderColor="HotPink"
BorderWidth="1"
Font-Size="Large"
Width="500"
>
</asp:Label>
<br /><br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="label text align center"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>


- How to add an onClick event to a Label
- How to create a multiline Label
- How to change Label width using CSS
- How to change Label text from JavaScript
- How to add a background image to a Label
- How to display none in a Label
- How to encode html in a Label
- How to change DropDownList item background color
- How to highlight an item in a DropDownList
- How to apply ListBox auto height