14

The wanted result

My goal is an alignment as shown in the attached image (the fields on the left may have any width, but the ones on the right should begin at the same X coordinate).

Right now I am using a simple table code to achieve this:

<table><tr>
<td>Left1</td><td>Right 1</td></tr>
<tr><td>Left 2</td><td>Right 2</td></tr></table>

However, I've heard that using tables is generally bad. Is there a way I could achieve the same design using CSS? The website is being designed for mobile devices which might not support fancy CSS, so the code must be as simple as possible.

EDIT: since I still occasionally get a notification on this question from people who (presumably) are just starting out with HTML like I was when I made it, please refer to the accepted answer by B T as this is by far the best way to achieve this functionality. The question suggested as a "possible duplicate" (31 May 2016) does not currently offer the table-row/table-column CSS-based approach and requires you to do guess work.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
AM-
  • 871
  • 1
  • 10
  • 20
  • 4
    What's wrong with a table? This is what they are meant for. Anything else would probably be more complex than using a table. – Will Jun 13 '12 at 19:01
  • 2
    As I said, all the design articles I've read discourage the use of tables, hence the question. – AM- Jun 13 '12 at 19:02
  • 1
    I would agree that table's isn't the best way to go here. Yes, tables get the job done, but they are not semantically correct, as you are not creating a table. It is very trivial to achieve the same using CSS (see my answer) – Ayush Jun 13 '12 at 19:03
  • 3
    Using tables for layout is not good design practice. Use tables for tabular data, that what it was intended for. – saluce Jun 13 '12 at 19:04
  • Possible duplicate of [Forms - Can they be done without tables?](http://stackoverflow.com/questions/591539/forms-can-they-be-done-without-tables) – Ciro Santilli OurBigBook.com May 30 '16 at 22:03
  • @CiroSantilli巴拿馬文件六四事件法轮功 Please see my edit on why this question should be preferred over the one at https://stackoverflow.com/questions/591539/can-form-styling-be-done-without-tables – AM- May 31 '16 at 10:49
  • @AM- I propose this: copy / adapt that answer to the other question ;-) – Ciro Santilli OurBigBook.com May 31 '16 at 11:37

5 Answers5

14

I found a much easier way to do this by accident. Say you have the following:

<div class='top'>
  <div>Something else</div>
  <div class='a'>
    <div>Some text 1</div>
    <div>Some text 2</div>
  </div>
  <div class='a'>
    <div>Some text 3</div>
    <div>Some text 4</div>
  </div>
</div>

You can align Some text 1 and Some text 2 using css table display styling like this:

.a {
  display: table-row;
}
.a div {
  display: table-cell;
}

The coolest thing is that as long as the 'top' div is NOT styled display: table, then other things like "Something else" can be ignored in terms of alignment. If the 'top' div IS styled display: table, then "Some text 1" will be aligned with "Something else" (ie it treats all its children like table rows, even if they have a different display style).

This works in Chrome, not sure if its supposed to behave this way, but I'm glad it works.

  .a {
      display: table-row;
    }
    .a div {
      display: table-cell;
    }
   <div class='top'>
      <div>Something else</div>
      <div class='a'>
        <div>Some text 1</div>
        <div>Some text 2</div>
      </div>
      <div class='a'>
        <div>Some text 3</div>
        <div>Some text 4</div>
      </div>
    </div>
parseguy
  • 129
  • 2
  • 17
B T
  • 57,525
  • 34
  • 189
  • 207
  • 2
    Much more robust and maintainable solution than those above as it requires no width-guessing. – P-Gn Nov 10 '15 at 20:11
  • This is the first solution I've seen that avoids specifying widths and behaves just like a table. Perfect! – Greg Jan 26 '16 at 18:35
  • I needed this because I wanted to extract a search result into it's own component but didn't want it's top-level tag to be a `