views:

325

answers:

3

I've been goin at it now for an 1.5hrs and I just can't figure out a way to make it the way I want. I'm kind of new with all things web, and it takes about as much time (maybe more?) trying to get things where I want and doing it in a way that is elegant.

Anyways, I have two text boxes, with two labels above them and centered over the box. I want to simply put a button in between them and about centered with the textboxes' height.

This is as close as I can make it get to what I want. I would like the left textbox moved over so it lines up with the words "Jump To:, "Customer:," and "Customer #:" and then have the right box be the same distance on the right with the Next button maybe a just 10px higher but still centered as it is. The problem with this picture is that it only works when the text boxes are shown and expanded, if they are hidden, then the panel actually renders half-way above the Customer # part because of how I manipulated the margins I'm guessing.

http://img192.imageshack.us/img192/259/controls.jpg

Here's my attempt with <div>s

<div style="position:relative; overflow:auto; margin-top:15px; margin-bottom:10px;">
    <div style="width:40%; float:left; margin-left:10px;">
        <div><asp:Label runat="server" ID="lblInfoDesc" /></div>
        <div><asp:TextBox ID="txtInfoDescription" runat="server" TextMode="MultiLine" Rows="3" MaxLength="500" Width="100%" /></div>
    </div>

    <%--<div style="position:absolute; margin-top:25px; margin-left:-10px;"><asp:Button ID="Button1" runat="server" Text="Next" /></div>--%>

    <div style="width:40%; float:right; margin-right:16px;">
        <div><asp:Label runat="server" ID="lblInfoData" /></div>
        <div><asp:TextBox ID="txtInfoData" runat="server" TextMode="MultiLine" Rows="3" MaxLength="500" Width="100%" /></div>
    </div>
</div>

And here's my attempt with table elements

    <div style="margin-bottom:10px;">
        <table style="position:relative; width:100%; margin-bottom:15px;">
            <tr style="text-align:center";>
                <td><asp:Label runat="server" ID="lblInfoDesc" /></td>
                <td></td><td></td><td></td><td></td>
                <td><asp:Label runat="server" ID="lblInfoData" /></td>
            </tr>
            <tr>
                <td style="margin-left: 0px;"><asp:TextBox ID="txtInfoDescription" runat="server" TextMode="MultiLine" Rows="3" MaxLength="500" Width="80%" /></td>
                <td></td><td></td><td></td><td></td>
                <td style="margin-right:10px;"><asp:TextBox ID="txtInfoData"        runat="server" TextMode="MultiLine" Rows="3" MaxLength="500" Width="80%" /></td>
            </tr>
        </table>

        <div style="position:inherit; text-align:center; margin-top:-55px; margin-bottom:25px;">
            <asp:Button ID="Button2" runat="server" Text="Next" />
        </div>
    </div>

All of those <td></td><td></td><td> was to attempt to put spaces between the button and the text boxes because I couldn't get float nor margin to work. Any help would be much appreciated!

And here's the complete page source:

<asp:Panel ID="pnlCustomer" runat="server" style="background-color:#ccccff; width:500px; height:90%; position:relative;" BorderColor="DarkBlue" BorderWidth="2px">

    <div style="position:relative; margin-top:10px; margin-left:10px;">
        <div style="color:#003399; font-size:18px; text-align:left;">Jump To:
            <asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True" 
                              onselectedindexchanged="ddlCategory_SelectedIndexChanged" 
                              style="margin-left:40px;"/>
        </div>
    </div>

    <div style="position:relative; margin-top:10px; margin-left:10px;">
        <div style="color:#003399; font-size:18px; text-align:left;">Customer:
            <asp:DropDownList ID="ddlCustomersList" runat="server" AutoPostBack="true"  
                              OnSelectedIndexChanged="ddlCustomersList_SelectedIndexChanged" 
                              style="margin-left:35px;"/>

            <asp:Button ID="btnAddCustomer" runat="server" Text="Add" OnClick="btnAddCustomer_Click" OnClientClick="return confirm('Warning: This will redirect you from the page');" />
        </div>
    </div>

    <div style="position:relative; margin-top:10px; margin-left:10px;">

        <div style="color:#003399; font-size:18px; text-align:left;">Customer #:
            <asp:DropDownList ID="ddlCustomerNumber" runat="server" AutoPostBack="true"
                              OnSelectedIndexChanged="ddlCustomerNumber_SelectedIndexChanged"
                              style="margin-left:20px;"/>

            <asp:TextBox ID="txtCustomerNumber" runat="server" style="margin-left:20px;" />

            <asp:Button ID="btnModify"    runat="server" Text="Modify"     OnClick="btnModify_Click"    />
            <asp:Button ID="btnCreateNew" runat="server" Text="Create New" OnClick="btnCreateNew_Click" />

            <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />
            <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" OnClientClick="return confirm('Do you want to delete the record ?');" />

            <asp:Button ID="btnSaveNew" runat="server" Text="Save"   OnClick="btnSaveNew_Click" />
            <asp:Button ID="btnCancel"  runat="server" Text="Cancel" OnClick="btnCancel_Click"  />
        </div>

    </div>

<%--    <div style="margin-bottom:10px;">
        <table style="position:relative; width:100%; margin-bottom:15px;">
            <tr style="text-align:center";>
                <td><asp:Label runat="server" ID="lblInfoDesc" /></td>
                <td></td><td></td><td></td><td></td>
                <td><asp:Label runat="server" ID="lblInfoData" /></td>
            </tr>
            <tr>
                <td style="margin-left: 0px;"><asp:TextBox ID="txtInfoDescription" runat="server" TextMode="MultiLine" Rows="3" MaxLength="500" Width="80%" /></td>
                <td></td><td></td><td></td><td></td>
                <td style="margin-right:10px;"><asp:TextBox ID="txtInfoData"        runat="server" TextMode="MultiLine" Rows="3" MaxLength="500" Width="80%" /></td>
            </tr>
        </table>

        <div style="position:inherit; text-align:center; margin-top:-55px; margin-bottom:25px;">
            <asp:Button ID="Button2" runat="server" Text="Next" />
        </div>
    </div>--%>


    <div style="position:relative; overflow:auto; margin-top:15px; margin-bottom:10px;">
        <div style="text-align:center; margin-bottom:-20px; ">
            <asp:Button ID="btnNextInfo" runat="server" Text="Next" />
        </div>

        <div style="width:40%; float:left; margin-left:10px;">
            <div><asp:Label runat="server" ID="lblInfoDesc" /></div>
            <div><asp:TextBox ID="txtInfoDescription" runat="server" TextMode="MultiLine" Rows="3" MaxLength="500" Width="100%" /></div>
        </div>

        <div style="width:40%; float:right; margin-right:16px;">
            <div><asp:Label runat="server" ID="lblInfoData" /></div>
            <div><asp:TextBox ID="txtInfoData" runat="server" TextMode="MultiLine" Rows="3" MaxLength="500" Width="100%" /></div>
        </div>
    </div>

    <div style="margin-top:-20px; position:absolute; font-size:12px;"><asp:Label runat="server" ID="lblErrorMessage" /></div>

</asp:Panel>
+1  A: 

if I haven't misunderstood, it's not that tough. see if following code renders the layout your image says.

<table cellpadding="3px" cellspacing="0" border="0" style="width: 100%;">
 <tr>
  <td align="center" valign="top">
   Label
  </td>
  <td align="center" valign="top">
   &nbsp;
  </td>
  <td align="center" valign="top">
   Label
  </td>
 </tr>
 <tr>
  <td align="center" valign="top">
   <asp:TextBox ID="TextBox1" TextMode="MultiLine" Rows="7" runat="server"></asp:TextBox>
  </td>
  <td align="center" valign="top">
   <asp:Button ID="Button1" runat="server" Text="Button" />
  </td>
  <td align="center" valign="top">
   <asp:TextBox ID="TextBox2" TextMode="MultiLine" Rows="7" runat="server"></asp:TextBox>
  </td>
 </tr>

</table>
Saar
In my mind, tables should be a last resort, not a first choice, unless you're presenting tabular data. However, this solution works, so I'm not downvoting. :) Just not upvoting, either.
John Rudy
@John Rudy - Agreed. I'm not feeling charitable today though, so -1 from me.
Sonny Boy
the people who cry for html tables are to present tabular data. So if tommorow's browser came with capability with tables supporting more flexible/maintainable styles, javascript. They will stand in line to praise tables and curse divs. just thought.
Saar
I used to have the same attitude about tables for years. However, when I went through my ecommerce sites and dumped table layouts for smarter CSS, it saved me 30% of my html output size and a hell of a lot of bandwidth. It also made my sites look a lot better in smaller mobile browsers.
blesh
@John Rudy: +1 for you. I agree table layout is a last resort.
blesh
@Saar: Today's browsers ship with tables being able to present very flexible and maintainable styles. I'm not insanely pure div, but I do prefer being semantically correct. I will use tables when I either (a) am presenting a table (grin) or (b) have no other non-hacky choice. Right tool for the right job is all.
John Rudy
@blesh: 100% agreed, for size and mobile applications. have you designed anytime perfect cross browser with 100% width table structure with div. I am much interested in such solution. no div cell should have fixed size. having columnspan and rowspan capability
Saar
NEWSFLASH: forms are tabular data, with one header row and one data row. Use tables for forms is 100% semantic. `<th>` can be used for labels, and `<td>` for the data people will enter.
DisgruntledGoat
+4  A: 

It seems like this is more of a CSS and HTML question than an ASP.Net question.

To be clear, centering things vertically in their parent with CSS isn't easy, the trick is:

  • The parent element must have a position:relative or absolute.
  • The child must be wrapped in a div or something positionable.
  • The child must have the top:50%
  • The child must declare a height.
  • the child must have a margin-top = it's height / 2 * -1. (basically you move it up by half it's height.)

Try this:

<style>
div.textboxArea {
    text-align:center;
    float:left;
    width:40%;
    height:400px;
}
.textboxArea  textarea {
    width:80%;
    height:400px;
}
.centerMeVertically  div {
    position:absolute;
    top:50%;
    vertical-align:middle;
    height:30px;
    width:100%;
    margin-top:-15px;
    text-align:center;
}
div.centerMeVertically {
    float:left;
    width:20%;
    text-align:center;
    height:400px;
    position:relative;
}
p {
     height:35px;
     margin:-35px 0 0 0;
}
.container {
    margin-top:35px;
}
</style>
<div class="container">
    <div style="width:100%;">
        <div class="textboxArea">
     <p>Label 1</p>
     <textarea></textarea>
        </div>
        <div class="centerMeVertically">
        <div><button>Button2</button></div>
        </div>
        <div class="textboxArea">
     <p>Label 2</p>
     <textarea></textarea>
        </div>
    </div>
</div>

EDIT: Having seen your revised question, I've revised my answer to center the button only to the textarea vertically.

You have to do some stupid CSS tricks with the top-margin, but it gets the job done. Also notice the container div. This is to make sure that if you place anything "above" this snippet in your markup, it doesn't get overlapped by the labels.

blesh
"in between them and about centered with the textboxes' height." - from the question. I guess I'll just call this thread to the moderator's attention, I sense some spiteful voting going on here.
blesh
Nothing spiteful at all (at least, not from me); one of us is right. It may be me, it may be you. Like I said, if my read of it is wrong, I will flip my vote from - to +, because you will be right. If that is the case, I will also delete my answer, because it will be wrong. I just wish the OP would clarify for us ...
John Rudy
I've updated my original post, sorry for the delay/confusion
Justen
@blesh: I'm editing your post so I can flip my vote accordingly. @Justen, then you for the updated visual; my post will be deleted shortly. This right here is your answer.
John Rudy
Also, blowing away comments which are now factually incorrect.
John Rudy
Very well, sorry for the misunderstanding. I could only read your "tone". Happy holidays.
blesh
+1 to John Rudy's comments on this thread. Good man, sorry for the confusion.
blesh
Not a problem, man. The downside of only having written words to go by is that sometimes it's hard to determine what the true intent behind them is. Like I said on Meta, reasonable people can disagree without negative intent. I hope Justen clicks the accept on this answer; it's definitely the correct one here.
John Rudy
Alright, thanks for helping me. I had to mess with some height values a little bit but I got my desired result! However, I have one side effect from this that I'd like to get rid of. When I press the button that shows those txt boxes, they render outside of my panel (the blue part) unless I add overfill:auto to the container. But if I do that, then when the txt boxes are hidden, the panel is overly large compensating for their height before they are shown.
Justen
Yeah, if that was any way unclear, I've posted another question with pictures at http://stackoverflow.com/questions/1928928/how-do-i-compensate-for-the-overflow-property
Justen
ughhh CSS is so frustrating. After spending all this time to get it to work, and then uploading the site to a live server, it messes up again...
Justen
A: 

I would stick with something simple like this table... then add padding as needed.

<table>
  <tr>
    <td align="center">label a</td>
    <td></td>
    <td align="center">label b</td>
  </tr>
  <tr>
    <td valign="top"><textarea id="txtinfodescription" rows="3" maxlength="500" width="80%"></textarea></td>
    <td valign="middle"><input type="button" id="button2" value="next"/></td>
    <td valign="top"><textarea id="txtinfodata" rows="3" maxlength="500" width="80%"></textarea></td>
  </tr>
</table>

don't get me wrong, you can go the pure CSS route too... but if you layout get much more complex and you need to maintain a fairly rigid "grid-like" structure... tables help.

scunliffe
How is this different from saar's answer (http://stackoverflow.com/questions/1891059/saars-answer/1891150#1891150)?
John Rudy
apparently not much, but when I wrote/posted my answer, there were no other answers. It looks like saar's post won the race though.
scunliffe