Add top margin to 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 add the top margin to a Label web server control. To add the top margin to a Label control, the asp.net c# developers have to apply a custom CSS style to a Label control. In the below example code, we demonstrate two ways to add the top margin to a Label web server 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 add only the top margin to a Label control. The asp.net developers just have to set the CSS margin-top style value to a specified pixel value.
There is another way to add the top margin to a Label web server control. 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 margin-top CSS style with a specified pixel 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 add the top margin to a Label web server control. To add the top margin to a Label control, the asp.net c# developers have to apply a custom CSS style to a Label control. In the below example code, we demonstrate two ways to add the top margin to a Label web server 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 add only the top margin to a Label control. The asp.net developers just have to set the CSS margin-top style value to a specified pixel value.
There is another way to add the top margin to a Label web server control. 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 margin-top CSS style with a specified pixel 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-margin-top.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
Label1.CssClass = "LabelMarginTopStyle";
//another way to apply top margin in label control.
//Label1.Style.Add("margin-top","50px");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<style type="text/css">
.LabelMarginTopStyle {
margin-top:50px;
}
</style>
<title>asp.net example - label margin-top</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - label margin-top
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Text="sample label to test label top margin."
BorderColor="HotPink"
BorderWidth="1"
Font-Size="X-Large"
Width="350"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="label margin-top"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>