代码之家  ›  专栏  ›  技术社区  ›  Diogenes

你会怎么做:表格还是CSS?[关闭]

  •  71
  • Diogenes  · 技术社区  · 17 年前

    Puzzle Grinch
    (来源: sontag.ca )

    第一部分

    它也可以用CSS完成,尽管它可能涉及 .

    The Challenge

    The Decision . .


    但是,尽管如此,在仔细研究了所提供的解决方案后,我得出了以下结论 要意识到,没有一个CSS解决方案(包括当时我自己的)能像所提供的任何一个表解决方案那样工作。挑战在于CSS比表格更好

    1. 我们的花园要用篱笆围起来;让它与周围沉闷的环境区别开来的东西;而且不太贵,但易于保持清洁。

    alt text
    (来源: sontag.ca )

    .

    position:absolute 规则是不允许的。如果你能让他们在这种情况下工作,那么你就有更多的权力。他们肯定会被接受的。

    我要求在地块周围设置围栏,因为每种花园类型都将种植在橙色背景的乡村,和我们种植的一些花的颜色非常相似。

    我并不喜欢橙色,但我确实喜欢和钦佩荷兰人,这就是为什么我们有一个橙色的背景,向我的东道国致敬。 :-)


    table answer

    alt text
    (来源: sontag.ca )

    你能用 你能砍倒森林里最高大的树吗。一条鲱鱼?


    更新

    12 回复  |  直到 17 年前
        1
  •  37
  •   Community Mohan Dere    9 年前

    更新:

    我还选择不将外部div重用为白色7方形(它在中没有自己的div) previous edits ),因为如果你想使用布局,这是不切实际的,感觉有点像作弊(尽管从简洁/像素完美的角度来看,我喜欢它的厚颜无耻)。

    在这里查看: http://jsbin.com/efidi
    在此处编辑: http://jsbin.com/efidi/edit
    Validates as XHTML strict

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head><title>The Challenge</title>
    <style type="text/css">
    div     { text-align: center; width:175px; height:175px; line-height: 35px;}
    div div {         float:left; width: 35px; height: 35px;}
    #orange, #maroon,
    #blue  , #green  {float:right;}
    
    #orange, #silver {background-color:silver;  width:140px;}
    #navy  , #maroon {background-color:maroon; height:140px; line-height:140px;}
             #navy   {background-color:navy  ;}
    #green , #red    {background-color:red   ;  width: 70px;}
    #yellow, #blue   {background-color:blue  ; height: 70px; line-height: 70px;}
             #yellow {background-color:yellow;}
             #white  {background-color:white ;}
             #green  {background-color:green ;}
             #orange {background-color:orange;}
    </style> 
    </head> 
    <body> 
      <div> 
        <div id="silver">1</div> 
        <div id="maroon">2</div> 
        <div id="navy"  >3</div> 
        <div id="red"   >4</div> 
        <div id="blue"  >5</div> 
        <div id="yellow">6</div> 
        <div id="white" >7</div>
        <div id="green" >8</div> 
        <div id="orange">9</div> 
      </div>
    </body></html>
    

    旁白:如果可以的话,我可能会多加一点空白,但在SO上的代码块开始显示滚动条之前,这已经达到了极限,我选择让所有内容都显示在屏幕上。

    注:我借了 line-height 修复自 Tyson (谁是第一个得到正确答案的人)。

        2
  •  18
  •   Gumbo    17 年前

    这里有三个解决方案。

    标记:

    <div id="outer">
        <div id="a1">1</div>
        <div id="a2">2</div>
        <div id="a3">3</div>
        <div id="a4">4</div>
        <div id="a5">5</div>
        <div id="a6">6</div>
        <div id="a7">7</div>
        <div id="a8">8</div>
        <div id="a9">9</div>
    </div>
    

    基本样式表(尺寸和颜色):

    #outer {
        width: 20em;
        height: 20em;
    }
    #a1 {
        background-color: #C0C0C0;
        width: 80%;
        height: 20%;
    }
    #a2 {
        background-color: #800000;
        width: 20%;
        height: 80%;
    }
    #a3 {
        background-color: #000080;
        width: 20%;
        height: 80%;
    }
    #a4 {
        background-color: #FF0000;
        width: 40%;
        height: 20%;
    }
    #a5 {
        background-color: #0000FF;
        width: 20%;
        height: 40%;
    }
    #a6 {
        background-color: #FFFF00;
        width: 20%;
        height: 40%;
    }
    #a7 {
        background-color: #FFFFFF;
        width: 20%;
        height: 20%;
    }
    #a8 {
        background-color: #008000;
        width: 40%;
        height: 20%;
    }
    #a9 {
        background-color: #FFA500;
        height: 20%;
        width: 80%;
    }
    

    现在定位:

    • 使用 float :

      #a1 {
          float: left;
      }
      #a2 {
          float: right;
      }
      #a3 {
          float: left;
      }
      #a4 {
          float: left;
      }
      #a5 {
          float: right;
      }
      #a6 {
          float: left;
      }
      #a7 {
          float: left;
      }
      #a8 {
          float: right;
      }
      #a9 {
          float: right;
      }
      
    • 使用 position :

      #outer {
          position: relative;
      }
      #outer div {
          position: absolute;
      }
      #a1 {
          top: 0;
          left: 0;
      }
      #a2 {
          top: 0;
          right: 0;
      }
      #a3 {
          top: 20%;
          left: 0;
      }
      #a4 {
          top: 20%;
          left: 20%;
      }
      #a5 {
          top: 20%;
          right: 20%;
      }
      #a6 {
          top: 40%;
          left: 20%;
      }
      #a7 {
          top: 40%;
          left: 40%;
      }
      #a8 {
          bottom: 20%;
          right: 20%;
      }
      #a9 {
          bottom: 0;
          right: 0;
      }
      
    • 使用 margin :

      #a1 {
      }
      #a2 {
          margin: -20% -80% 0 80%;
      }
      #a3 {
          margin: -60% 0 0 0;
      }
      #a4 {
          margin: -80% -20% 0 20%;
      }
      #a5 {
          margin: -20% -60% 0 60%;
      }
      #a6 {
          margin: -20% -20% 0 20%;
      }
      #a7 {
          margin: -40% -40% 0 40%;
      }
      #a8 {
          margin: 0 -40% 0 40%;
      }
      #a9 {
          margin: 0 -20% 0 20%;
      }
      
        3
  •  17
  •   Peter Boughton    17 年前

    <img
        src="http://sontag.ca/TheChallenge/tiles.gif"
        alt="nine assorted coloured rectangles"
    />
    

    P

        4
  •  13
  •   Tyson    17 年前

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    
        <title>Boxy Boxes in a Box</title>
    
        <style type="text/css" media="screen">
            #container {position: relative; margin: 100px auto; height: 175px; width: 175px; font-style: italic; }
    
            .box {width: 35px; height: 35px; position: absolute; text-align: center; line-height: 35px;}
    
            #box_1 {top: 0; left: 0; width: 140px; background-color: silver;}
            #box_2 {top: 0; right: 0; height: 140px; background-color: maroon; line-height: 140px;}
            #box_3 {top: 35px; left: 0; height: 140px; background-color: navy; line-height: 140px;}
            #box_4 {top: 35px; left: 35px; width: 70px; background-color: red;}
            #box_5 {top: 35px; right: 35px; height: 70px; background-color: blue; line-height: 70px;}
            #box_6 {top: 70px; left: 35px; height: 70px; background-color: yellow; line-height: 70px;}
            #box_7 {top: 70px; left: 70px; background-color: white;}
            #box_8 {bottom: 35px; right: 35px; width: 70px; background-color: green;}
            #box_9 {bottom: 0; right: 0; width: 140px; background-color: orange;}
        </style>
    </head>
    
    <body>
        <div id="container">
            <div id="box_1" class="box">1</div>
            <div id="box_2" class="box">2</div>
            <div id="box_3" class="box">3</div>
            <div id="box_4" class="box">4</div>
            <div id="box_5" class="box">5</div>
            <div id="box_6" class="box">6</div>
            <div id="box_7" class="box">7</div>
            <div id="box_8" class="box">8</div>
            <div id="box_9" class="box">9</div>
        </div>
    </body>
    </html>
    
        5
  •  4
  •   Vilx-    17 年前

    只要宽度和高度保持不变,人们总是可以使用绝对定位来获得相同的效果。这应该足够明显,这样我就不必把它打出来了(这里很晚了,我很懒:P)

        6
  •  4
  •   Prestaul    17 年前

    我采取了一种与我迄今为止看到的“id everything”解决方案略有不同的方法。这比基于表的解决方案多不到100个字符。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>The Challenge</title>
    <style type="text/css">
    div {
        position:absolute;
        width:35px;
        height:35px;
        text-align:center;
        line-height:35px
    }
    
    .spiral { width:175px; height:175px }
    
    .t { top:0 }
    .l { left:0 }
    .r { right:0 }
    .b { bottom:0 }
    .w { width:140px }
    .h { height:140px; line-height:140px }
    .c {
        top:35px;
        left:35px;
        width:105px;
        height:105px
    }
    
    .c .w { width:70px }
    .c .h { height:70px; line-height: 70px }
    .c .c { width:35px; height: 35px }
    </style>
    </head>
    <body>
    <div class="spiral">
        <div class="t l w" style="background-color:silver">1</div>
        <div class="t r h" style="background-color:maroon">2</div>
        <div class="b l h" style="background-color:navy">3</div>
        <div class="c">
            <div class="t l w" style="background-color:red">4</div>
            <div class="t r h" style="background-color:blue">5</div>
            <div class="b l h" style="background-color:yellow">6</div>
            <div class="c">7</div>
            <div class="b r w" style="background-color:green">8</div>
        </div>
        <div class="b r w" style="background-color:orange">9</div>
    </div>
    </body>
    </html>
    

    编辑: 根据您的修改,我发布了一个稍微冗长但希望更清晰的解决方案,添加了一个黑色边框,将一些文本设置为白色,并且没有绝对定位“花园”。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>The Challenge</title>
    <style type="text/css">
    div {
        position:absolute;
        width:35px;
        height:35px;
        text-align:center;
        line-height:35px
    }
    
    div.spiral {
        position:relative;
        width:175px;
        height:175px;
        border: 1px solid #000
    }
    
    .top { top:0 }
    .left { left:0 }
    .right { right:0 }
    .bottom { bottom:0 }
    .wide { width:140px }
    .tall { height:140px; line-height:140px }
    .center {
        top:35px;
        left:35px;
        width:105px;
        height:105px
    }
    
    .center .wide { width:70px }
    .center .tall { height:70px; line-height: 70px }
    .center .center { width:35px; height: 35px }
    </style>
    </head>
    <body>
    <div class="spiral">
        <div class="top left wide" style="background-color:silver">1</div>
        <div class="top right tall" style="background-color:maroon">2</div>
        <div class="bottom left tall" style="background-color:navy;color:#fff">3</div>
        <div class="center">
            <div class="top left wide" style="background-color:red">4</div>
            <div class="top right tall" style="background-color:blue">5</div>
            <div class="bottom left tall" style="background-color:yellow">6</div>
            <div class="center">7</div>
            <div class="bottom right wide" style="background-color:green">8</div>
        </div>
        <div class="bottom right wide" style="background-color:orange">9</div>
    </div>
    </body>
    </html>
    
        7
  •  4
  •   Jacco    17 年前

    这是Ted的表格布局解决方案和他的挑战。..

    只需在HTML标记中添加非常简单的内容!所有居民都以令人愉悦的风格自动居中和间隔。例如:“

    alt text
    (来源: sontag.ca )
    alt text
    (来源: sontag.ca )

    “孩子们,最好带很多钱,我现在真的又饿又渴。”

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Terrible Ted's Table Layout</title>
    <style type="text/css">
    
    #master TD { text-align: center }
    #master { 
      border: 1px solid black;
      font: italic 100%/200% 'Comic Sans MS', cursive; 
    }
    #silver { background-color:silver }
    #maroon { background-color: maroon;  color:white }
    #navy { background-color:navy;  color:white }
    #red { background-color: red }
    #blue { background-color:blue;  color:white }
    #yellow { background-color: yellow }
    #green { background-color:green;  color:white }
    #orange { background-color:orange }
    #white { background-color:white }
    
    #silver, #red, #green, #orange, #white { height: 35px }
    #maroon, #navy, #blue, #yellow, #white { width: 35px }
    
    </style>
    </head>
    <body style="background-color:#ffb600">
    
    <table id="master" border="0" cellpadding="0" cellspacing="0" 
      summary="layoutByTable"><tr>
        <td id="silver" colspan="2" > 1 </td> 
        <td id="maroon" rowspan="2" > 2 </td>
      </tr><tr>
      <td id="navy" rowspan="2" > 3 </td>
      <td>
        <table border="0" cellpadding="0" cellspacing="0" 
          summary="inner"><tr>
            <td id="red" colspan="2" > 4 </td>
            <td id="blue" rowspan="2" > 5 </td>
          </tr><tr>
            <td id="yellow" rowspan="2" > 6 </td>
            <td id="white"> 7 </td>
          </tr><tr>
            <td id="green" colspan="2" > 8 </td>
          </tr>
        </table>
      </td>
    </tr><tr>
     <td id="orange" colspan="2"> 9 </td>
    </tr>
    </table>
    
    </body>
    </html>
    
        8
  •  4
  •   Glorfindel Doug L.    7 年前

    单桌解决方案。

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <meta http-equiv="Content-language" content="en" />
            <title>The Challenge</title>
        </head>
        <body>
    
            <table cellspacing="0" cellpadding="0" border="0" summary="">
                <tr>
                    <td colspan="4" height="35" align="center" bgcolor="silver"><i>1</i></td>
                    <td rowspan="4" width="35" align="center" bgcolor="maroon"><i>2</i></td>
                    <td rowspan="5" valign="bottom"><img src="http://sontag.ca/gif/grinch.gif" width="41" height="122" alt="Dr. Suess's Grinch"/></td>
                </tr><tr>
                    <td rowspan="4" width="35" align="center" bgcolor="navy"><i>3</i></td>
                    <td colspan="2" height="35" align="center" bgcolor="red"><i>4</i></td>
                    <td rowspan="2" width="35" align="center" bgcolor="blue"><i>5</i></td>
                </tr><tr>
                    <td rowspan="2" width="35" align="center" bgcolor="yellow"><i>6</i></td>
                    <td width="35" height="35" align="center"><i>7</i></td>
                </tr><tr>
                    <td colspan="2" height="35" align="center" bgcolor="green"><i>8</i></td>
                </tr><tr>
                    <td colspan="4" height="35" align="center" bgcolor="orange"><i>9</i></td>
                </tr>
            </table>
    
        </body>
    </html>
    

    它是有效的XHTML 1.0 Transitional,我包含了Dr.Suess字符:)

    Dr. Suess character <?xml 声明 meta-tags summary

    编辑

    根据要求,XHTML 1.0严格版

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
            <title>The Challenge</title>
            <style type="text/css">
                BODY {background: orange}
                #garden {border: 1px solid black; color: black}
                #garden TD {
                    font: italic 100% 'Comic Sans MS', cursive;
                    height: 35px;
                    padding: 0;
                    text-align: center;
                    width: 35px
                }
                #c1 {background: silver}
                #c2 {background: maroon; color: white}
                #c3 {background: navy;   color: white}
                #c4 {background: red}
                #c5 {background: blue;   color: white}
                #c6 {background: yellow}
                #c7 {background: white}
                #c8 {background: green;  color: white}
                #c9 {background: orange}
            </style>
        </head>
        <body>
    
            <table id="garden" cellspacing="0">
                <tr>
                    <td id="c1" colspan="4">1</td>
                    <td id="c2" rowspan="4">2</td>
                </tr><tr>
                    <td id="c3" rowspan="4">3</td>
                    <td id="c4" colspan="2">4</td>
                    <td id="c5" rowspan="2">5</td>
                </tr><tr>
                    <td id="c6" rowspan="2">6</td>
                    <td id="c7">7</td>
                </tr><tr>
                    <td id="c8" colspan="2">8</td>
                </tr><tr>
                    <td id="c9" colspan="4">9</td>
                </tr>
            </table>
    
        </body>
    </html>
    

        9
  •  2
  •   Andy Ford    17 年前

    标记的简洁性。...

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>The Challenge</title>
    <style type="text/css">
        .garden {
            position: relative;
            width: 175px;
            height: 175px;
            font-family: 'Comic Sans MS', cursive;
            border: 1px solid;
            color: #000;
            }
        .garden div {
            position: absolute;
            width: 35px;
            height: 35px;
            line-height: 35px;
            text-align: center;
            }
        .garden div:first-child {
            width: 140px;
            background: silver;
            }
        .garden div:first-child + div {
            right: 0;
            height: 140px;
            line-height: 140px;
            color: #fff;
            background: maroon;
            }
        .garden div:first-child + div + div {
            top: 35px;
            height: 140px;
            line-height: 140px;
            color: #fff;
            background: navy;
            }
        .garden div:first-child + div + div + div {
            top: 35px;
            left: 35px;
            width: 70px;
            background: red;
            }
        .garden div:first-child + div + div + div + div {
            top: 35px;
            right: 35px;
            height: 70px;
            line-height: 70px;
            background: blue;
            }
        .garden div:first-child + div + div + div + div + div {
            top: 70px;
            left: 35px;
            height: 70px;
            line-height: 70px;
            background: yellow;
            }
        .garden div:first-child + div + div + div + div + div + div {
            top: 70px;
            left: 70px;
            background: white;
            }
        .garden div:first-child + div + div + div + div + div + div + div {
            top: 105px;
            left: 70px;
            width: 70px;
            background: green;
            }
        .garden div:first-child + div + div + div + div + div + div + div + div{
            bottom: 0;
            right: 0;
            width: 140px;
            background: orange;
            }
    
        </style> 
    </head> 
    <body> 
    <div class="garden"> 
    <div>1</div> 
    <div>2</div> 
    <div>3</div> 
    <div>4</div> 
    <div>5</div> 
    <div>6</div> 
    <div>7</div>
    <div>8</div> 
    <div>9</div> 
    </div>
    </body>
    </html>
    

    link

        10
  •  1
  •   Travis    17 年前

    我认为我们已经证明,有不止一种方法可以做到这一点。这 table 标签和CSS都是可行的选择。

    HTML中的表格应用于显示表格数据。

    • 制造
    • CSS是 制造 用于造型/展示。
        11
  •  1
  •   Glorfindel Doug L.    7 年前

    alt text
    (来源: sontag.ca )
    我第一次做这个练习是在两年多前,当时我第一次学习HTML和CSS。我的第一个解决方案就像你在这里看到的那样,除了没有匿名容器DIV。然后我想到了一个网页,它将CSS与Table进行并排比较,以证明CSS更好。因此,我致力于 The Challenge 页面,发布了它,然后发布了这个问题。

    在几分钟内发布了一个答案,似乎真的很接近。我看得出来,他正在寻找比我更好的解决方案。他所有的潜水器都井然有序,而我的则不然。 杰科 发布了一条评论,问我为什么使用两个嵌套表,而一个嵌套表就可以了。当然,他也是对的。

    所以我有两个 荷马·辛普森 瞬间。我阅读了关于表格与CSS的其他问题和答案。有人提到桌子是垂直居中的。我的答案也没有垂直居中,但我认为这是可能的。毕竟,关键在于做桌子能做的一切,甚至做得更好。我现在已经把自己画成了一个角落,看起来像个傻瓜,所以我 寻找答案。

    最终(不好意思说有多长时间),我想出了下面的解决方案。

    you should use CSS


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Charlie's CSS layout</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <style type="text/css">
    
    #outer { 
      width:175px; height:175px; 
      text-align:center; 
      font: italic 100%/200% 'Comic Sans MS', cursive;  
      border: 1px solid black;
    }
    #inner { width: 105px }
    #outer>DIV, #inner>DIV { float:left }
    #outer>DIV>DIV, #inner>DIV>DIV 
    { display: table-cell;  vertical-align: middle }
    #c2 { clear: right }
    #c3, #c6 { clear: left }
    
    #c1>DIV, #c4>DIV, #c7>DIV, #c8>DIV, #c9>DIV { height: 35px }
    #c2>DIV, #c3>DIV, #c5>DIV, #c6>DIV, #c7>DIV { width:  35px }
    #c2>DIV, #c3>DIV { height: 140px } 
    #c1>DIV, #c9>DIV { width:  140px } 
    #c5>DIV, #c6>DIV { height:  70px } 
    #c4>DIV, #c8>DIV { width:   70px } 
    #c2, #c6, #c7, #c8, #c9 { position:relative; top:-35px }
    #c9 { left: 35px }
    
    #c1 { background-color: silver }
    #c2 { background-color: maroon; color: white }
    #c3 { background-color: navy; color: white }
    #c4 { background-color: red }
    #c5 { background-color: blue; color: white }
    #c6 { background-color: yellow }
    #c7 { background-color: white }
    #c8 { background-color: green; color: white }
    #c9 { background-color: orange }
    
    /* these rules are a HACK to center vertically in IE7 */
    #outer>DIV>DIV, #inner>DIV>DIV { position:relative; }
    #c1>DIV, #c4>DIV, #c7>DIV, #c8>DIV, #c9>DIV { top: 10% }
    #c5>DIV { top: 0% } 
    #c6>DIV { top: 30% }
    #c2>DIV { top: 0% }
    #c3>DIV { top: 15% }
    
    </style>
    </head>
    <body>
    
    <div id="outer">
      <div id="c1"><div> 1 </div></div>
      <div id="c3"><div>3<br/>3<br/>3</div></div>
      <div id="inner">
        <div id="c4"><div> 4 </div></div>
        <div id="c5"><div> 5<br/>5 </div></div>
        <div id="c6"><div> 6 </div></div>
        <div id="c7"><div> 7 </div></div>
        <div id="c8"><div> 8 </div></div>
      </div>
      <div id="c2"><div> 2<br/>2<br/>2<br/>2 </div></div>
      <div id="c9"><div> 9 9 9</div></div>
    </div>
    
    </body>
    </html>
    
        12
  •  0
  •   pinxi    16 年前

    这是一个不使用绝对定位、不使用表单元格、在IE6-8、FF等中有效的示例。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    
    <title>Terrible Ted's Table Layout</title>
    <style type="text/css">
    #box{border:1px solid #000; width:175px; height:175px; color:navy; font-family:"Comic Sans MS"; font-size:13px; font-style:italic; text-align:center;}
    
    div {float:left}
    
    #c1, #c3, #c4, #c7, #c8, #c9{height:35px; line-height:35px}
    #c2, #c3{height:140px;line-height:140px}
    #c5, #c6{height:70px; line-height:70px}
    
    #c1, #c9{width:140px}
    #c2, #c3, #c5, #c6, #c7{width:35px}
    #c4, #c8{width:70px}
    
    #c6, #c7 {margin-top:-35px}
    
    #c1{background-color:silver}
    #c2{background-color:maroon; float:right}
    #c3{background-color:navy}
    #c4{background-color:red}
    #c5{background-color:blue}
    #c6{background-color:yellow}
    #c7{background-color:white}
    #c8{background-color:green}
    #c9{background-color:orange}
    
    </style>
    </head>
    <body>
    <div id="box">
    <div id="c1">1</div>
    <div id="c2">2</div>
    <div id="c3">3</div>
    <div id="c4">4</div>
    <div id="c5">5</div>
    <div id="c6">6</div>
    <div id="c7">7</div>
    <div id="c8">8</div>
    <div id="c9">9</div>
    </div>
    </body>
    </html>