2色の背景を持つテーブルセルを作成する方法?
-
20-09-2019 - |
質問
私は、ツートンカラーの背景を持つHTMLテーブルセルを作成しようとしています。私は右に左に黄色、緑のある背景の上に通常のテキストを持っています。
次のように私がこれまで持っている最も近いです。背景は正確に半分ずつですが、コンテンツのテキストは、その下に変位されます。
<html>
<head>
<style type='text/css'>
td.green
{
background-color: green;
padding: 0px;
margin: 0px;
height:100%;
text-align:center
}
div.yellow
{
position:relative;
width: 50%;
height: 100%;
background-color:yellow
}
</style>
</head>
<body style="width: 100%">
<table style="width: 25%">
<tr style="padding: 0px; margin: 0px">
<td class="green">
<div class="yellow"></div>
<div class="content">Hello</div>
</td>
</tr>
</table>
</body>
</html>
どのように私はこれを修正することができますか?
解決
視覚的に各色がとても理想的にあなたがそれらを入れ子にするのではなく、コード内で同じレベルに背景色を設定する要素を維持したいと等しいとして表示されます。アーロンの答えをオフに構築ます:
<html>
<head>
<style type='text/css'>
td {
padding: 0;
margin: 0;
text-align: center;
}
.container {
position: relative;
}
.bg {
position: absolute;
top: 0;
bottom: 0;
width: 50%;
}
.content {
position: relative;
z-index: 1;
}
.yellow {
left: 0;
background-color: yellow;
}
.green {
right: 0;
background-color: green;
}
</style>
</head>
<body style="width: 100%">
<table style="width: 25%">
<tr style="padding: 0; margin: 0">
<td>
<div class="container">
<div class="content">Hello</div>
<div class="bg yellow"></div>
<div class="bg green"></div>
</div>
</td>
</tr>
</table>
</body>
</html>
他のヒント
あなたが必要巣黄色DIV
のコンテンツDIV
ます:
<div class="yellow"><div class="content">Hello</div></div>
[編集]これは欠陥を有している:内側DIV(すなわち、それが唯一ページ幅の50%を使用する)黄色DIV
に限定される
だから我々は別のdiv
、絶対位置とzインデックスを必要とします:
<html>
<head>
<style type='text/css'>
td.green
{
background-color: green;
padding: 0px;
margin: 0px;
height:100%;
text-align:center
}
div.yellow
{
position:absolute; left:0px; top:0px;
width: 50%;
height: 100%;
background-color:yellow
}
div.container { position:relative; height: 100%; }
div.content { position:relative; z-index:1; }
</style>
</head>
<body style="width: 100%">
<table style="width: 25%; height: 150px;">
<tr style="padding: 0px; margin: 0px">
<td class="green">
<div class="container">
<div class="content">Hello</div>
<div class="yellow"></div>
</div>
</td>
</tr>
</table>
</body>
</html>
FF 3.6で動作します。
背景画像を使用する方が簡単かもしれませんか?あなたは、そしてちょうどセルにこれを適用することができます。
これを試してください:
td.green
{
background-color: green;
padding: 0px;
margin: 0px;
height:1em;
text-align:center
}
div.yellow
{
width: 50%;
height: 1em;
background-color:yellow
}
.content {
margin-top: -1em;
}
ちょうど右の左側にある色と相互に長く細いイメージを作成します。そして、次のようにそれにスタイルを与えます:
background: url(image) center center repeat-y;
(未テストが、これらの線に沿って何かする必要があります:P
Tdは一定の幅である場合、あなたはそれがTDの高さ全体を埋めるように、fixedWidth_in_px * 1px
と背景と設定された背景リピートを繰り返し-Yとして画像を設定する寸法の画像を等しくすることができます。
td.bg
{
width: 100px;
height: 100%;
background: #fff url(imagepath) repeat-y;
}
私の解決策のために私は2つの背景色があるかのように見せかけるために、セルの境界線を使用することができ、細胞の内部には、他の要素やテキストがありませんでした。そうは50pxに設定TDために、これらの両方のスタイルを適用すると、2擬似背景色で表示されます。
.halfleft_casual {
border-right:25px solid blue;
}
.halfleft_member {
border-right:25px solid green;
}