DIV & CSS 如何与 Rowspan & Colspan 协同工作?



我已经研究了几天关于如何使用div创建表单元格和合并单元格的几天,实际上我可以使用表,但是不能在div中进行相同的屏幕,希望有人可以帮助我推动我更好的方法或修复代码。

实际上,我想在全屏模式下以相同的宽度和高度(合并区域除外)使所有单元格进行,但问题是中心的合并单元格。我尝试了许多方法无法像表格一样工作。

这是我想要的结果,但要使用表格:

<style>
html, body{
    width:100%;
    height:100%;
    margin:0;
    padding:0;
}
table {
    border-width: 1px 1px 0px 0px;
    border-spacing:0;
    margin:0;
    padding:0;
    width: 100%;
    height: 100%;
}
td { 
    border-width: 0px 0px 1px 1px;
}
table, td {
    border-style: solid;
    border-color: purple;
}
.w25 {
    width:25%;
    height:25%;
}
.w50 {
    width:50%;
    height:50%;
}
.w100 {
    width:100%;
    height:100%;
}
</style>
<table class='w100'>
    <tr>
        <td class='w25'>D</td>
        <td class='w25'>E</td>
        <td class='w25'>F</td>
        <td class='w25'>G</td>
    </tr>
    <tr>
        <td class='w25'>C</td>
        <td class='w50' colspan=2 rowspan=2 >MERGED AREA</td>
        <td class='w25'>H</td>
    </tr>
    <tr>
        <td class='w25'>B</td>
        <td class='w25'>I</td>
    </tr>
    <tr>
        <td class='w25'>A</td>
        <td class='w25'>L</td>
        <td class='w25'>K</td>
        <td class='w25'>J</td>
    </tr>
</table>

这是我目前为DIV版本制作的代码,但在全屏中无法平衡所有宽度和高度。

<style>
html, body{
    width:100%;
    height:100%;
    margin:0;
    padding:0;
}
.table {
    border-width: 1px 1px 0px 0px;
}
.intable {
    border-width: 0px 1px 0px 0px;
}
.table, .intable {
    display:table;
    width:100%;
    height:100%;
}
.cell {
    display:table-cell;
}
.row {
    display:table-row;
}
.cell {
    border-width: 0px 0px 1px 1px;
    width:25%;
}
.table, .intable, .cell {
    border-style: solid;
    border-color: purple;
}
</style>
<div class='table'>
    <div class='row'>
        <div class='cell' style="max-width:25%;">D</div>
        <div class='intable'>
            <div class='cell'>E</div>
            <div class='cell'>F</div>
        </div>
        <div class='cell'>G</div>
    </div>
    <div class='row'>
      <div class='intable'>
        <div class='row'>
          <div class='cell'>C</div>
        </div>
        <div class='row'>
          <div class='cell'>B</div>
        </div>
      </div>
      <div class='cell'>Merged Area</div>
      <div class='intable'>
        <div class='row'>
          <div class='cell'>H</div>
        </div>
        <div class='row'>
          <div class='cell'>I</div>
        </div>
      </div>
    </div>
    <div class='row'>
        <div class='cell'>A</div>
        <div class='intable'>
            <div class='cell'>L</div>
            <div class='cell'>K</div>
        </div>
        <div class='cell'>J</div>
    </div>
</div>

表版本JSFIDDLE

DIV版本JSFIDDLE

希望有人可以修复代码!

尝试在合并的列中添加另一个类,例如:

<div class='cell merged'>Merged Area</div>

和更改合并区域的CSS,例如:

.merged{
    width:50%;
    height:50%;
}

我这样做的原因是因为您在合并的区域上有相同的类,但是您希望大小可以占用普通单元的空间两倍。因此,我要做的就是添加一个额外的类更改合并区域的宽度和高度以获得所需的结果。

这是一个更新的小提琴:

https://jsfiddle.net/6hx2uooz/4/

最新更新