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

如何创建具有双色背景的表格单元格?

  •  4
  • Chowlett  · 技术社区  · 15 年前

    我正试图创建一个带有双色调背景的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>
    

    我该怎么解决?

    7 回复  |  直到 7 年前
        1
  •  4
  •   Simon Lieschke    15 年前

    从视觉上看,每种颜色看起来都是相等的,因此理想情况下,您应该在代码中保持将背景颜色设置为相同级别的元素,而不是嵌套它们。根据亚伦的回答:

    <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>
    
        2
  •  6
  •   Aaron Digulla    15 年前

    你必须嵌套内容 DIV 黄色的 div :

    <div class="yellow"><div class="content">Hello</div></div>
    

    [编辑]这有一个缺陷:内部div将限于黄色 div (也就是说,它只能使用50%的页面宽度)。

    所以我们需要另一个 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。

        3
  •  1
  •   matpol    15 年前

    使用背景图像可能更容易些?你可以把这个应用到手机上。

        4
  •  0
  •   nickf    15 年前

    试试这个:

      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;
      }
    
        5
  •  0
  •   Svish    15 年前

    只需创建一个长而薄的图像,一种颜色在左边,另一种颜色在右边。然后给出如下样式:

    background: url(image) center center repeat-y;
    

    (未经测试,但应该是这样的:p

        6
  •  0
  •   rahul    15 年前

    如果td是固定宽度的,则可以生成一个尺寸等于 fixedWidth_in_px * 1px 并将此图像设置为背景,并将背景重复设置为repeat-y,以便它填充TD的整个高度。有点像

    td.bg
    {
        width: 100px;
        height: 100%;
        background: #fff url(imagepath) repeat-y;
    }
    
        7
  •  0
  •   Jonathon Batson    10 年前

    对于我的解决方案,我在单元格中没有其他元素或文本,因此可以使用单元格的边框使其看起来好像有两种背景色。因此,将这两种样式应用到td设置为50px时,会出现两种伪背景色。

    .halfleft_casual {    
      border-right:25px solid blue;
    }
    .halfleft_member {    
      border-right:25px solid green;  
    }