Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad use "display: table;" to organise a layout into 2 columns?

Tags:

html

css

I am trying to make a 2 column layout, apparently the bane of CSS. I know you shouldn't use tables for layout, but I've settled on this CSS. Note the use of display: table etc.

div.container {
  width: 600px; height: 300px; margin: auto;
  display: table; table-layout: fixed;
  }

ul {
  white-space: nowrap; overflow: hidden;
  display: table-cell;
  width: 40%;
  }

div.inner {
  display: table-cell;
  width: auto;
  }

With this layout:

<div class="container">

  <ul>
    <li>First</li>
    <li>Second</li>
    <li>Third</li>
    </ul>

  <div class="inner">
    <p>Hello world</p>
    </div>

  </div>

This seems to work admirably. However, I can't help wondering - am I obeying the letter of the "don't use tables" rule, but not the spirit? I think it's ok, since there's no positioning markup in the HTML code, but I'm just not sure about the "right" way to do it.

I can't use css float, because I want the columns to expand and contract with the available space.

Please, stack overflow, help me resolve my existential sense of dread at these pseudo-tables.

like image 743
Colen Avatar asked Apr 22 '10 03:04

Colen


People also ask

Is it ever okay to use tables for layout?

Tables for Layout Are Invalid in HTML 4.01 The HTML 4 specification states: "Tables should not be used purely as a means to layout document content as this may present problems when rendering to non-visual media." So, if you want to write valid HTML 4.01, you can't use tables for layout.

What is wrong with using a table for page layout?

HTML tables were originally intended to be used for presenting tabular data, not for layout. The World Wide Web Consortium (W3C®) discourages use of tables for layout because they are striving for a web in which content and structure are completely separate from presentation.

Is it bad practice to use tables in HTML?

They are not bad practice if they are used as intended. The role they are intended for is as a means of doing layout for tabular data.

What is the purpose of display table?

Setting display to table makes the element behave like a table. So you can make a replica of an HTML table without using the table element and corresponding elements such as tr and td . For example, in HTML, you can make a table with the <table> element and also a <div> , or any container of your choice.


1 Answers

There are two essential arguments for not using tables:

  1. Semantic markup.
  2. Avoiding tag soup. (too many tags)

So, your method is not really violating either of those goals. However, you should check this code in IE7 and IE6 - you are likely to see some inconsistencies there.

As was mentioned - don't worry about sticking to these rules too tightly. They are guidelines, and you should use the right tool for the job you are doing. What is important here is knowing what the various techniques are best for, and when to use them. Sometimes, using a table is the best tool for what you are trying to do, sometimes it is not.

like image 200
Jeff Fohl Avatar answered Oct 04 '22 03:10

Jeff Fohl