71

パズル グリンチ
(出典: sontag.ca )

パートI

このレイアウトは、2 つの HTML テーブル (一方が他方の内部にネストされている)、または 1 つのテーブルで非常に簡単に行うことができます。

CSS を使用することもできますが、もう少し考えなければならない場合があります。

これは実際のレイアウトではないかもしれませんが、似たようなページを見たことがあります。これはなぞなぞだと考えてください。CSS スキルを磨くための演習です。

物事をもう少し面白くするために、私はThe Challengeと呼ばれる小さな 2 部構成の Web ページに質問をまとめました。コードと質問を調べます:テーブルまたは CSS を使用したレイアウト? 、並んで、一撃一撃で、2人の対戦相手がコードの覇権をめぐって戦います。

パート I では、The Challengeがどのようにして誕生したかを説明します。楽しんでいただければ幸いです。

パート II は決定です。 あなたは驚くかもしれません


パート II

投稿してからわずか数で、本当に良い回答がすぐに表示されたことに驚きました。それは謙虚な経験でした。タイムトライアルであなたと競争する気はありません。

しかし、そうは言っても、提供されたソリューションを詳しく調べたところ、CSS ソリューション (当時の私自身のものも含む) のどれも、提供されたテーブル ソリューションほどうまく機能しないことに気付きました。課題は、どのようなレイアウト ソリューションにおいても、CSS がテーブルよりも優れているということでした。

そこで、3 つの新しいルールを追加しました (ルールの 1 つは、ルールを変更できるということです)。これは一部の人々を悩ませました。それで、ルールが変更された理由について、カラフルな説明を追加しました。これは彼らをさらに苛立たせたと思います。

  1. 私たちの庭はフェンスで囲まれています。それが自分自身を見つけるかもしれないどんな悲惨な環境からもそれを際立たせるための何か。高価ではありませんが、清潔に保つのは簡単です。だから私は庭の周りに1ピクセルの黒い境界線が欲しい
  2. 各庭の区画 (キャラクター) の住人は、庭で最も優れているかどうかに応じて、黒または白のいずれかでなければなりません。また、それらはすべて筆記体です。それらの間に斜体はありません。;-)
  3. 庭は再配置可能です。つまり、この庭をページのどこにでも置くことができます (絶対配置はありません)。

最終的な出力は次のようになります (背景色はオプションです)。

代替テキスト
(出典: sontag.ca )

気まぐれで土壇場でのルール変更についてお詫び申し上げます。間違えました。各庭園区画の住民は、職人、手作りの専門家です。彼らは筆記体の子孫であり、そのスタイルのセンスはイタリック体のおかげです。

両方の種類の庭 (テーブルと CSS) が同じページに共存する必要があるため、庭は再配置可能でなければなりません。position:absoluteルールは許されないと言うのは間違っているかもしれません。このコンテキストでそれらを機能させることができれば、より多くの力が得られます. 彼らは確かに受け入れられます。

私たちが育てているいくつかの花の色に非常に似たオレンジ色の背景を持つ田園地帯にそれぞれの庭のタイプが植えられるので、私はプロットの周りにフェンスを求めました.

私は今オランダに住んでいて、チューリップの季節が近づいています。今後数週間でオランダ上空を飛行し、晴れた日 (ここでは珍しいことです) の場合、下の風景はこのばかげた演習にかなり似ています。

私はオレンジ色に夢中というわけではありませんが、オランダ人が好きで、尊敬しています。:-)


パート III

この画像とともに、The Challenge からのTed のテーブルの回答を以下に掲載しました。

代替テキスト
(出典: sontag.ca )

CSS ルールに触れることなく、居住者をガーデン プロットに簡単に追加できるため、すべてが自動的に中央に配置されます。

CSS でこれを行うことはできますか?ニシンで森の中で一番強い木を切り倒すことができますか?


更新:チャーリーの答えはこちらです。

4

12 に答える 12

37

更新: 最終編集。STRICT DTD に切り替え、質問のイメージに合わせて斜体を削除し、質問に対する OP のコメントに従って意図を示すために ID のフルカラー名に戻し、CSS の ID 名のメイン列を表示順に並べ替えましたhtmlで。

また、外側の div を白い 7 の正方形として再利用しないことを選択しました (以前の編集では独自の divがありませんでした)。これは、レイアウトを使用したい場合に実用的ではなく、不正行為のように少し感じたためです。 (簡潔さ/ピクセルパーフェクトの観点からは、私はそれの生意気さが好きでしたが).

ここで表示: http://jsbin.com/efidiここ
編集: http://jsbin.com/efidi/edit

<!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 のコードブロックがスクロールバーを取得し始める前にこれが限界になり、すべてを画面に表示することを選択しました。

注: Tysonline-heightから修正を借りました(正しくレンダリングされた回答を最初に得たのは彼です)。

于 2009-03-11T23:36:06.170 に答える
18

ここに3つの解決策があります。

マークアップ:

<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%;
    }
    
于 2009-03-11T23:28:30.807 に答える
17

ここに行きます-テーブルタグの誤用が提供できるよりも少ない行:

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

:P

于 2009-03-12T00:46:19.497 に答える
13

これは、垂直方向および水平方向の中央揃えのテキスト (これまで誰も行っていない) を含め、テーブルの例と完全に一致します。

<!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>
于 2009-03-11T23:35:24.193 に答える
4

ここではまだ誰も表の解決策を示していません。The Challenge は、制御された (そして非常に偏りのある) シナリオで、CSS レイアウトと表ベースのレイアウトを比較することに関するものです。

Ted のテーブル レイアウト ソリューションと彼の課題は次のとおりです...

「私のテーブル ベースのソリューションでは、HTML マークアップに非常に簡単に追加するだけで、庭の区画に新しい住人を簡単に追加できます。すべての住人は、快適なスタイルで自動的に中央に配置され、間隔が空けられます。例:」

代替テキスト
(出典: sontag.ca ) (出典: sontag.ca )
代替テキスト

「私が知る限り、ここの CSS ベースのソリューションは、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>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>
于 2009-03-13T09:41:34.590 に答える
4

幅と高さが一定である限り、常に絶対配置を使用して同じ効果を得ることができます。これは十分に明白なはずなので、タイプする必要はありません (ここでは遅く、私は怠け者です:P)

于 2009-03-11T23:35:43.927 に答える
4

単一テーブル ソリューション。

今、私に反対票を投じないでください。

これは元の質問に対する答えではないことはわかっています。OPが元の質問へのコメントで要求し
たため、この回答を投稿しました。

<?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、宣言<?xmlmeta-tagsおよびsummary属性を削除することで、929 文字まで削減でき、有効な XHTML 1.0 Transitional のままにすることができます。

編集

要求どおり、XHTML 1.0 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">
            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>

970 個の非空白文字、オレンジ色の背景、Dr. Suess のグリンチが削除されました。

于 2009-03-13T09:36:06.037 に答える
4

これまで見てきた「すべてをidにする」ソリューションとは少し異なるアプローチを取りました。これは、テーブルベースのソリューションよりも 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>
于 2009-03-12T01:17:00.843 に答える
2

マークアップの簡潔さ....

<!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>

リンク

于 2009-03-13T00:55:58.487 に答える
1

代替テキスト
(ソース: sontag.ca )
私が最初にこの演習を行ったのは、HTML と CSS を初めて学習した 2 年前のことです。私の最初の解決策は、匿名コンテナー DIV がないことを除いて、ここに表示されているものと似ていました。次に、CSS と Table を並べて比較し、CSS の方が優れていることを証明する Web ページのアイデアを思いつきました。そこで私はThe Challengeページに取り組み、それを公開してから、この質問を投稿しました。

Sam Haslerが数分以内に回答を投稿しました。彼は私が持っていたものよりも優れた解決策に向かっていることがわかりました. 彼の div はすべて正常でしたが、私の div は正常ではありませんでした。 Jaccoは、ネストされたテーブルを 1 つ使用するのに、なぜ 2 つのネストされたテーブルを使用したのかを尋ねるコメントを投稿しました。もちろん、彼も正しかった。

それで、ホーマー・シンプソンの 「Doh!」が 2 つありました。すぐに。テーブルとCSSに関する他の質問と回答を読みました。誰かが、テーブルが垂直方向に中央に配置されていると言いました。私の答えも垂直方向の中央にはありませんでしたが、可能かもしれないと思いました。結局のところ、要点は、テーブルができることすべてをより良くすることです。馬鹿みたいに自分を追い詰めてしまった僕は、答えを見つけなければならなかった。

最終的に(それがどれくらいだったかを言うのは恥ずかしいです)、私は以下の解決策を思いつきました. その後、並べて比較する Web ページという当初のコンセプトを実現することができました。

これがどのように機能するのか、なぜ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>
于 2009-03-14T03:31:02.817 に答える
0

これは、絶対配置を使用せず、テーブルセルを使用せず、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>
于 2010-01-14T07:22:23.610 に答える