Border around specific rows in a table?

后端 未结 10 1551
轮回少年
轮回少年 2020-12-02 08:10

I\'m trying to design some HTML/CSS that can put a border around specific rows in a table. Yes, I know I\'m not really supposed to use tables for layout but I don\'t know en

相关标签:
10条回答
  • 2020-12-02 08:56

    the trick is with outline property thanks to enigment's answer with little modification

    use this class

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

    then in the HTML

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

    and the result is hope this helps you

    0 讨论(0)
  • 2020-12-02 08:58

    Here's an approach using tbody elements that could be the way to do it. You can't set the border on a tbody (same as you can't on a tr) but you can set the background colour. If the effect you're wanting to acheive can be obtained with a background colour on the groups of rows instead of a border this will work.

    <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>
    
    0 讨论(0)
  • 2020-12-02 09:00

    If you set the border-collapse style to collapse on the parent table you should be able to style the tr: (styles are inline for demo)

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

    Output:

    HTML output

    0 讨论(0)
  • 2020-12-02 09:01

    How about tr {outline: thin solid black;}? Works for me on tr or tbody elements, and appears to be compatible with the most browsers, including IE 8+ but not before.

    0 讨论(0)
提交回复
热议问题