如何创建具有双色背景的表格单元格?
-
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将局限于黄色DIV
(即,它只能使用页面宽度的50%)
因此,我们需要另一个div
,绝对定位和的z-index:
<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为固定宽度的话可以使用尺寸的图像等于fixedWidth_in_px * 1px
和设置该图像作为背景,并设置背景重复重复-γ,以便它填充了TD的整个高度。类似
td.bg
{
width: 100px;
height: 100%;
background: #fff url(imagepath) repeat-y;
}
有关我的解决方案我没有其他元素或文本的单元格内,从而可以使用单元格的边框,使它看起来好像有2种背景颜色。因此应用这两种样式TD设定为50像素显示带有2伪背景色。
.halfleft_casual {
border-right:25px solid blue;
}
.halfleft_member {
border-right:25px solid green;
}
不隶属于 StackOverflow