Solved

HowTo: Nicely align input elements within a table?

Posted on 2006-11-16
6
180 Views
Last Modified: 2013-11-19
Problem:
- The code (below), renders a trivial table with two rows.
- The text fields (column 1) render nicely, the input fields (column 2) don't.

  +--------+------------------------+
  |    City|                        |
  +--------+------------------------+
  | Country|                        |
  +--------+------------------------+

- Please note how nicely the left portion of column 2 is seperate from the
  table border.  For example, it looks something like the following for both
  a text input field, and a selection list:

  +-----
  | +---
  | |
  | +---
  +-----

- The right portion (margin?) of the input field is much less nice.  In Firefox 2.0
  it looks something like:

  -----+
  -----|
      ||
  -----|
  -----+

  in IE 7, it is even worse.  It looks like:

  -----+
  -----|
       |
  -----|
  -----+

  How do I have the right input element margin separate from the containing
  table td edge?

======================================================================
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Sizing </title>
<style type='text/css'>
input {
  width            : 100%;
  color            : yellow;
  background-color : blue;
  font-family      : Courier;
  font-size        : 10pt;
}
select {
  width            : 100%;
  color            : yellow;
  background-color : blue;
  font-family      : Courier;
  font-size        : 10pt;
}
table {
  background-color : #112233;
}
td {
  background-color : #fafad2;
  font-size        : 9pt;
  font-family      : Courier;
}
</style>
</head>
<body>
<form name='myform' action=''>
  <table width='20%' cellpadding='4' cellspacing='1'>
    <tr>
      <td width='30%' align='right'>City</td>
      <td width='70%'>
        <input type='text' id='user' size='5'>
      </td>
    </tr>
    <tr>
      <td align='right'>Country</td>
      <td>
        <select id='country'>
          <option value='aus'>Australia</option>
          <option value='deu'>Germany</option>
          <option value='uk'>United Kingdom</option>
          <option value='us'>United States</option>
        </select>
      </td>
    </tr>
  </table>
</form>
</body>
</html>
0
Comment
Question by:HonorGod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 28

Accepted Solution

by:
gamebits earned 128 total points
ID: 17962130
Try

input {
  width            : 95%;

and see if you like it better.

Gamebits
0
 
LVL 12

Expert Comment

by:jessegivy
ID: 17962234
Yea, that was basically my only solution too, and it's pissin me off.  Perhaps gamebits knows why the thing appears to hang over the edge.  I noticed I was thinking of the cellspacing as a border, which it isn't.  

...anyway, I guess the real issue I take here is the excessive use of percentages.  Have you considdered using fixed widths for your table and elements.  The standard is 800X600 interface size to accomodate most resolutions.  I can understand your desire to use percentages to stretch the table and have your site fill the entire screen on all machines, but ultimatly it's not entirely practical in all situations.

...or, as gamebits said, just go with 95%, but my question is "why does the select element display fine and the input doesn't?"
0
 
LVL 41

Author Comment

by:HonorGod
ID: 17962294
jessegivy, that is exactly my point!
I really, really, really dislike this "work around".  It really isn't a fix, it's a hack.  Yuck.
I am still hopeful that someone can help me understand the "right" way to do this.

I still have lots to learn.
Can someone please explain what is actually happening, and how best to resolve this issue?

Thanks
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 12

Assisted Solution

by:jessegivy
jessegivy earned 128 total points
ID: 17963360
>>I still have lots to learn.
don't we all!

I've been writing HTML for over a decade and I'm stumped.  ...Ultimatly I guess, it's just that the folks who design these objects, just do it differently and problems arise.  It seems that the border for the select element is somehow built into the element, and doesn't take up additional screen retail.  Check out this sample code, all I did was get rid of the border and the things line up!!  So, I suppose it's just a matter of how they're rendered.

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Sizing </title>
<style type='text/css'>
input {
width:100%;
border:0;
  color            : yellow;
  background-color : blue;
  font-family      : Courier;
  font-size        : 10pt;
}
select {
width:100%;
  color            : yellow;
  background-color : blue;
  font-family      : Courier;
  font-size        : 10pt;
}
table {
  background-color : #112233;
}
td {
  background-color : #fafad2;
  font-size        : 9pt;
  font-family      : Courier;
}
</style>
</head>
<body>
<form name='myform' action=''>
  <table width='20%' cellpadding='4' cellspacing='1'>
    <tr>
      <td width='20%' align='right'>City</td>
      <td width='80%'>
        <input type='text' id='user' size='5'>
      </td>
    </tr>
    <tr>
      <td align='right'>Country</td>
      <td>
        <select id='country'>
          <option value='aus'>Australia</option>
          <option value='deu'>Germany</option>
          <option value='uk'>United Kingdom</option>
          <option value='us'>United States</option>
        </select>
      </td>
    </tr>
  </table>
</form>
</body>
</html>

...hope this helps in some way, though I can't really imagine how it would?  btw, your html is written perfectly, very easy to read.  Sometimes we have to accept that there are just things in this game that we're gonna have to deal with in one way or another, occasionally, it's a necessity to use the "hack" and as long as it works, there's really no harm.

~Jesse
0
 
LVL 41

Author Comment

by:HonorGod
ID: 17964795
Jesse:
  Thanks for the compliment, and the response.

  As you say, this is rendered much better in both FireFox 2.0, and IE 7.  Unfortunately IE 7 still has a flaw in that the right margin of the input TD
looks something like:

  ------
  -----

  -----
  -----+

  which I find exceedingly strange.

0
 
LVL 41

Author Comment

by:HonorGod
ID: 17972930
Thanks for the help guys.  I guess I'll have to be satisfied with at hack... (sigh)
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question