我想创建表只使用<div>标签和CSS。

这是我的样本表。

<body>
  <form id="form1">
      <div class="divTable">
             <div class="headRow">
                <div class="divCell" align="center">Customer ID</div>
                <div  class="divCell">Customer Name</div>
                <div  class="divCell">Customer Address</div>
             </div>
            <div class="divRow">
                  <div class="divCell">001</div>
                <div class="divCell">002</div>
                <div class="divCell">003</div>
            </div>
            <div class="divRow">
                <div class="divCell">xxx</div>
                <div class="divCell">yyy</div>
                <div class="divCell">www</div>
           </div>
            <div class="divRow">
                <div class="divCell">ttt</div>
                <div class="divCell">uuu</div>
                <div class="divCell">Mkkk</div>
           </div>

      </div>
  </form>
</body>

和风格:

<style type="text/css">
    .divTable
    {
        display:  table;
        width:auto;
        background-color:#eee;
        border:1px solid  #666666;
        border-spacing:5px;/*cellspacing:poor IE support for  this*/
       /* border-collapse:separate;*/
    }

    .divRow
    {
       display:table-row;
       width:auto;
    }

    .divCell
    {
        float:left;/*fix for  buggy browsers*/
        display:table-column;
        width:200px;
        background-color:#ccc;
    }
</style>

但此表格不适用于IE7及以下版本。请给我你的解决方案和想法。 谢谢。


当前回答

考虑到Grid-Css,我没有看到任何答案。我认为这是一种非常优雅的方法:grid-css甚至支持行跨度和列跨度。在这里你可以找到一篇非常好的文章:

https://medium.com/@js_tut/css-grid-tutorial-filling-in-the-gaps-c596c9534611

其他回答

有点跑题,但可以帮助某人更干净的HTML… CSS

.common_table{
    display:table;
    border-collapse:collapse;
    border:1px solid grey;
    }
.common_table DIV{
    display:table-row;
    border:1px solid grey;
    }
.common_table DIV DIV{
    display:table-cell;
    }

HTML

<DIV class="common_table">
   <DIV><DIV>this is a cell</DIV></DIV>
   <DIV><DIV>this is a cell</DIV></DIV>
</DIV>

适用于Chrome和Firefox

你可以创建“新标签”基于经典的表格,一个“div表”:

<STYLE>
dtable {
 display:table;
 width:100%;
 border:1px solid #000;
 border-spacing:5px;
}
dtable dtr {
 display:table-row;
 clear: both;
}
dtable dtd {
 display:table-column;
 padding:5px;
}
</STYLE>

<DTABLE>
 <DTR>
  <DTD>row 1 col 1</DTD>
  <DTD>row 1 col 2</DTD>
 </DTR>
 <DTR>
  <DTD>row 2 col 1</DTD>
  <DTD>row 2 col 2</DTD>
 </DTR>
</DTABLE>

你可以为每个“dtd”使用特定的CSS选择器,比如:

dtable dtd:nth-child(1) {
 width:300px;
 font-weight:bold;
}

dtable dtd:nth-child(2) {
 width:400px;
}

而“斑马”则像:

dtable dtr:nth-child(odd) {
 background-color:#ddd;
}

更多信息,请参见:https://css-tricks.com/useful-nth-child-recipies/

考虑到Grid-Css,我没有看到任何答案。我认为这是一种非常优雅的方法:grid-css甚至支持行跨度和列跨度。在这里你可以找到一篇非常好的文章:

https://medium.com/@js_tut/css-grid-tutorial-filling-in-the-gaps-c596c9534611

如果在<table>中有你不喜欢的东西,也许你可以使用重置文件?

or

如果你需要这样的页面布局,请查看cssplay布局示例,以设计没有表格的网站。

使用正确的文档类型;这将解决问题。在HTML文件的顶部添加以下代码行:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">