代码之家  ›  专栏  ›  技术社区  ›  Kyle Cronin

表中特定行的边框?

  •  124
  • Kyle Cronin  · 技术社区  · 16 年前

    <td>

    .top {
      border-top: thin solid;
      border-color: black;
    }
    
    .bottom {
      border-bottom: thin solid;
      border-color: black;
    }
    
    .left {
      border-left: thin solid;
      border-color: black;
    }
    
    .right {
      border-right: thin solid;
      border-color: black;
    }
    <html>
    
    <body>
    
      <table cellspacing="0">
        <tr>
          <td>no border</td>
          <td>no border here either</td>
        </tr>
        <tr>
          <td class="top left">one</td>
          <td class="top right">two</td>
        </tr>
        <tr>
          <td class="bottom left">three</td>
          <td class="bottom right">four</td>
        </tr>
        <tr>
          <td colspan="2">once again no borders</td>
        </tr>
        <tr>
          <td class="top bottom left right" colspan="2">hello</td>
        </tr>
        <tr>
          <td colspan="2">world</td>
        </tr>
      </table>
    
    </html>

    <tr>

    注:

    10 回复  |  直到 8 年前
        1
  •  110
  •   enigment    12 年前

    怎么样 tr {outline: thin solid black;} appears

        2
  •  51
  •   Legends    8 年前

    tr.top td {
      border-top: thin solid black;
    }
    
    tr.bottom td {
      border-bottom: thin solid black;
    }
    
    tr.row td:first-child {
      border-left: thin solid black;
    }
    
    tr.row td:last-child {
      border-right: thin solid black;
    }
    <html>
    
    <head>
    </head>
    
    <body>
    
      <table cellspacing="0">
        <tr>
          <td>no border</td>
          <td>no border here either</td>
        </tr>
        <tr class="top row">
          <td>one</td>
          <td>two</td>
        </tr>
        <tr class="bottom row">
          <td>three</td>
          <td>four</td>
        </tr>
        <tr>
          <td colspan="2">once again no borders</td>
        </tr>
        <tr class="top bottom row">
          <td colspan="2">hello</td>
        </tr>
        <tr>
          <td colspan="2">world</td>
        </tr>
      </table>
    
    </body>
    
    </html>

    enter image description here

    top , bottom , left ,以及 right 每个班级 <td> ,我所要做的就是添加 top row <tr> , bottom row <tr> ,以及 row <tr>

        3
  •  34
  •   Stefan van den Akker Raymond Hettinger    11 年前

    border-collapse 风格到 collapse tr :

    <table style="border-collapse: collapse;">
      <tr>
        <td>No Border</td>
      </tr>
      <tr style="border:2px solid #f00;">
        <td>Border</td>
      </tr>
      <tr>
        <td>No Border</td>
      </tr>
    </table>
    

    HTML output

        4
  •  8
  •   Simon E. pbs    11 年前

    <style>
        tr { 
            display: table;            /* this makes borders/margins work */
            border: 1px solid black;
            margin: 5px;
        }
    </style>
    

    这将阻止使用流体/自动列宽 ,因为单元格将不再与其他行中的单元格对齐,但边框/颜色格式仍然可以正常工作。解决方案是 为TR和TD指定宽度 (像素或百分比)。

    tr.myClass 如果你只想将它应用于某些行。显然 display: table 然而,它不适用于IE 6/7,但可能还有其他黑客(hasLayout?)可以适用于这些。 :-(

        5
  •  3
  •   sipsorcery    16 年前

    <table cellspacing="0">  
        <tbody>
            <tr>    
                <td>no border</td>    
                <td>no border here either</td>  
            </tr>  
        <tbody bgcolor="gray">
            <tr>    
                <td>one</td>    
                <td>two</td>  
            </tr>  
            <tr>    
                <td>three</td>    
                <td>four</td>  
            </tr>  
        <tbody>
            <tr>    
                 <td colspan="2">once again no borders</td>  
            </tr>  
        <tbody bgcolor="gray">
            <tr>    
                 <td colspan="2">hello</td>  
            </tr>
        <tbody>
        <tr>    
             <td colspan="2">world</td>  
        </tr>
    </table>
    
        6
  •  2
  •   csi Mark B    11 年前

    我能想到的唯一另一种方法是将需要边框的每一行都括在嵌套表中。这将使边框更容易操作,但可能会产生其他布局问题,您必须手动设置表格单元格的宽度等。

    根据您的其他布局要求,您的方法可能是最好的,这里建议的方法只是一种可能的替代方案。

    <table cellspacing="0">  
        <tr>    
            <td>no border</td>    
            <td>no border here either</td>  
        </tr>  
        <tr>
            <td>
                 <table style="border: thin solid black">
                      <tr>    
                            <td>one</td>    
                            <td>two</td>  
                      </tr>  
                      <tr>    
                          <td>three</td>    
                          <td>four</td>  
                      </tr>  
                 </table>
             </td>
        </tr>
        <tr>    
             <td colspan="2">once again no borders</td>  
        </tr>  
        <tr>
            <td>
                 <table style="border: thin solid black">
                      <tr>    
                            <td>hello</td>  
                       </tr>
                 </table>
             </td>
        </tr>
        <tr>    
             <td colspan="2">world</td>  
        </tr>
    </table>
    
        7
  •  1
  •   sipsorcery    16 年前

    根据您的要求,您想在任意MxN单元格块周围设置边框,如果不使用Javascript,真的没有更简单的方法。如果你的单元格是固定的,你可以使用浮点数,但由于其他原因,这是有问题的。你正在做的事情可能很乏味,但没关系。

    <html>
    <head>
    
    <style type="text/css">
    td.top { border-top: thin solid black; }
    td.bottom { border-bottom: thin solid black; }
    td.left { border-left: thin solid black; }
    td.right { border-right: thin solid black; }
    </style>
    <script type="text/javascript" src="jquery-1.3.1.js"></script>
    <script type="text/javascript">
    $(function() {
      box(2, 1, 2, 2);
    });
    
    function box(row, col, height, width) {
      if (typeof height == 'undefined') {
        height = 1;
      }
      if (typeof width == 'undefined') {
        width = 1;
      }
      $("table").each(function() {
        $("tr:nth-child(" + row + ")", this).children().slice(col - 1, col + width - 1).addClass("top");
        $("tr:nth-child(" + (row + height - 1) + ")", this).children().slice(col - 1, col + width - 1).addClass("bottom");
        $("tr", this).slice(row - 1, row + height - 1).each(function() {
          $(":nth-child(" + col + ")", this).addClass("left");
          $(":nth-child(" + (col + width - 1) + ")", this).addClass("right");
        });
      });
    }
    </script>
    </head>
    <body>
    
    <table cellspacing="0">
      <tr>
        <td>no border</td>
        <td>no border here either</td>
      </tr>
      <tr>
        <td>one</td>
        <td>two</td>
      </tr>
      <tr>
        <td>three</td>
        <td>four</td>
      </tr>
      <tr>
        <td colspan="2">once again no borders</td>
      </tr>
    </tfoot>
    </table>
    </html>
    

        8
  •  1
  •   cletus    16 年前

    <tbody>

    <table>
      <tr><td>No Style here</td></tr>
      <tbody class="red-outline">
        <tr><td>Style me</td></tr>
        <tr><td>And me</td></tr>
      </tbody>
      <tr><td>No Style here</td></tr>
    </table>  
    

    还有style.css中的css

    .red-outline {
      outline: 1px solid red;
    }
    
        9
  •  0
  •   Community CDub    8 年前

    诀窍在于 outline property enigment's answer

    .row-border{
        outline: thin solid black;
        outline-offset: -1px;
    }
    

    然后在HTML中

    <tr>....</tr>
    <tr class="row-border">
        <td>...</td>
        <td>...</td>
    </tr>
    

    enter image description here

        10
  •  -4
  •   ForceMagic chooselife    12 年前

    一种更简单的方法是将表设置为服务器端控件。你可以使用类似的东西:

    Dim x As Integer
    table1.Border = "1"
    
    'Change the first 10 rows to have a black border
     For x = 1 To 10
         table1.Rows(x).BorderColor = "Black"
     Next
    
    'Change the rest of the rows to white
     For x = 11 To 22
         table1.Rows(x).BorderColor = "White"
     Next