Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to set up a GUI website via Visual Web Developer 2010 Express for a user to enter information...
I'm using Labels and Text Boxes.
I'm having a problem setting them up to appear in the middle of the window but aligned properly. In a form, they are within a paragraph (

) and maybe that is the probem!
i.e., say the below is aligned in the center of a webpage, I want each label line to be centered left, and textbox line to be centered off of that (i.e., zip code since it is the longest string).
The lines represent where the textbox should begin...

Name:     ___________
City:     ___________
State:    ___________
Zip Code: ___________


I can't seem to accomplish this!

I was trying to use Format --> Align --> Lefts, but it is greyed out, so I'm assuming it's not an option for an asp:label.
I also tried using Format --> Set Position --> Relative and manually moving it, but it is still not centering.

This is my code...
ASP.NET
<p style="height: 173px" align="center" class="style2" Width="100%">
    <asp:Label ID="LabelName" runat="server"
        style="font-family: Arial; position: relative; top: 0px; left: 0px; color: #000066; font-weight: 700" Width="50px"
        Text="Name:" ForeColor="#000066"></asp:Label>
    <asp:TextBox ID="TextBoxName" runat="server"
        style="color: #000066; font-family: Arial; position: relative; top: 0px; left: 51px;" Width="168px"
        ontextchanged="TextBoxName_TextChanged" ForeColor="#000066"></asp:TextBox>
        <br />
    <asp:Label ID="LabelCity" runat="server"
        style="font-family: Arial; position: relative; top: 2px; left: 0px; color: #000066; font-weight: 700" Width="50px"
        Text="City:" ForeColor="#000066"></asp:Label>
    <asp:TextBox ID="TextBoxCity" runat="server"
        style="color: #000066; font-family: Arial; position: relative; top: 2px; left: 51px;" Width="168px"
        ontextchanged="TextBoxCity_TextChanged" ForeColor="#000066"></asp:TextBox>
        <br />
</p>


This might be trivial to some, but I'm new at this and would like any help!
Thanks,
Cindy
Posted
Updated 8-Feb-12 13:40pm
v2

You can try using HTML Tables[^].

[Update]
Try removing align=center on your table and put this instead
CSS
style="margin: 0 auto;"


I have tested this and it works even when your table is inside a <p>
 
Share this answer
 
v2
Comments
Cindy MCS 8-Feb-12 20:00pm    
I could use HTML tables, but you would think that a web developer tool kit would make it so much easier for a developer!!
I actually tried using a table, but it is not aligned in the center of the web page, it is aligned to the left of the web page.
Maybe I'm missing something!
Here's my code...
<table align=”center”>

<tr>

<td>

<asp:Label ID="LabelName" runat="server"

style="font-family: Arial; color: #000066; font-weight: 700"

Text="Name:">

</td>

<td>

<asp:TextBox ID="TextBoxName" runat="server"

style="color: #000066; font-family: Arial" Width="168px"

ontextchanged="TextBoxName_TextChanged">

<br />

</td>

</tr>

<tr>

<td>

<asp:Label ID="LabelCity" runat="server"

style="font-family: Arial; color: #000066; font-weight: 700"

Text="City:">

</td>

<td>

<asp:TextBox ID="TextBoxCity" runat="server"

style="color: #000066; font-family: Arial" Width="168px"

ontextchanged="TextBoxCity_TextChanged">

</td>

</tr>

</table>

Thanks for the help!
walterhevedeich 8-Feb-12 20:29pm    
Check my updated answer.
Cindy MCS 8-Feb-12 20:59pm    
I took my table out of the paragraph. I was just using the paragraph because I didn't want to deal with tables.
Both <table align=”center”> and <table style="margin: 0 auto;"> work!
I have tried in Firefox and IE!
Is there a difference between the two (align as opposed to style)?
Thanks for the input!
walterhevedeich 8-Feb-12 21:34pm    
style is an attribute where you can inject inline CSS styles on your control. In that code, what we did was we inserted a CSS property called margin and set the top and bottom to zero, while setting the left and right to auto. What that basically means is that we leave the browser in charge to set the left and right margins for us.
Sergey Alexandrovich Kryukov 8-Feb-12 23:30pm    
I voted 5 despite of the extensive, passionate, highly popularized and ideological struggle against using tables for alignment which is considered stylistic, not structural. I consider such idealism as based on mythical thinking and absolutization of internally pretty inconsistent HTML standard. Using tables for alignment is quite rational in my view, as alignment in such cases is a "hidden" form of table structure, so why not using tables explicitly, especially when the results are much more stable and maintainable the the alternatives?
--SA
HTML
try this code

you will see on this sample below on how you will use CSS 
Structured, Organize and Reusable

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <style type="text/css">
        .lbl{font-family: Arial; color: #000066; font-weight: 700}
        .txtbox{color: #000066; font-family: Arial}
        .paddingRight{ padding-right:10px;}
    </style>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <table align="center" cellspacing="0" cellpadding="0">
        <tr>
            <td align="right" class="paddingRight">
                <asp:label id="LabelName" cssclass="lbl" runat="server" text="Name:" xmlns:asp="#unknown" />
            </td>
            <td>
                <asp:textbox id="TextBoxName" cssclass="txtbox" xmlns:asp="#unknown">
                    Width="168px" OnTextChanged="TextBoxName_TextChanged" runat="server">
                    </asp:textbox>
            
            </td>
        </tr>
        <tr>
            <td align="right" class="paddingRight">
                <asp:label id="LabelCity" cssclass="lbl" text="City:" runat="server" xmlns:asp="#unknown" />
            </td>
            <td>
                <asp:textbox id="TextBoxCity" cssclass="txtbox" xmlns:asp="#unknown">
                    Width="168px" OnTextChanged="TextBoxCity_TextChanged" runat="server"></asp:textbox>
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>


Accept Solution or vote
if this will help you
thanks
 
Share this answer
 
Comments
Cindy MCS 9-Feb-12 8:08am    
This worked fine except there was an extra ">" placed after "#unknown" in the textbox "td" cell. I modified and ran the following and everything looks great!
Thank you so much for the help!!!
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>

<style type="text/css">
.lbl{font-family: Arial; color: #000066; font-weight: 700}
.txtbox{font-family: Arial; color: #000066; font-weight:600}
.paddingRight{ padding-right:10px;}
</style>

</head>
<body>
<form id="form1" runat="server">
<div>

<table align="center" cellspacing="0" cellpadding="0">
<tr>
<td align="right" class="paddingRight">
<asp:Label ID="LabelName" cssclass="lbl" runat="server" Text="Name:" xmlns:asp="#unknown">
</td>
<td>
<asp:TextBox ID="TextBoxName" cssclass="txtbox" xmlns:asp="#unknown"
Width="168px" OnTextChanged="TextBoxName_TextChanged" runat="server">

</td>
</tr>
<tr>
<td align="right" class="paddingRight">
<asp:Label ID="LabelCity" cssclass="lbl" Text="City:" runat="server" xmlns:asp="#unknown">
</td>
<td>
<asp:TextBox ID="TextBoxCity" cssclass="txtbox" xmlns:asp="#unknown"
Width="168px" OnTextChanged="TextBoxCity_TextChanged" runat="server">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900