Center a label in flow layout

cpopham

Junior Contributor
Joined
Feb 18, 2004
Messages
273
In flowlayout I thought that you used the <center> tag to center things like labels, but I have found out that is not the case. What tag would you use? Or how to you center your elements like labels in flow layout?

Chester
 
An asp:label control is rendered as a span tag in html. Span tags don't take up the full screen/page width so you can't center them with their default behavior. There are several ways you could approach this - here's one (that works in IE - I can't speak for other browsers and how they might interprest these styles).

Add two styles to your label control - one to give it a width, and one to align its text. For example - to center align an asp:label named 'lblTest':

Code:
//  C# Example that's be almost identical (minus the semi-colons) in VB.Net:
lblTest.Style.Add("width", "100%");
lblTest.Style.Add("text-align", "center");

The above is how you'd do it in code behind. If you wanted to do the centering in your aspx then add a style attribute to your asp:label control tag (in html view) that looks like this:

Code:
<asp:label id="lblTest" style="width: 100%; text-align: center;" runat="server" />

Paul
 
Last edited:
Back
Top