Resolver o css do canto arredondado do quadro

tenho esta regra do CSS para canto arredondado:

th, td { padding: 8px;
         background: #E8ECE0;
         text-align: center;
         border: 1px solid #444;
         border-bottom-width: 0px;
}
thead { background-color: #446bb3  ; color :#fff; padding:4px; line-height:30px }
tbody tr:nth-child(even) {background: #F6F6EC;}
tbody tr:nth-child(odd) {background: #FFF}

tr:first-child td, tr:first-child th {
    border-top-left-radius: 12px; border-top-right-radius: 12px;
}
tr:last-child td {
    border-bottom: 1px solid #444;
    border-bottom-left-radius: 12px; border-bottom-right-radius: 12px;
}
table { border-spacing: 0; border: 0; margin:0px; width:100%; padding:5px}
td.pd {border-bottom-left-radius: 12px; border-bottom-right-radius: 12px;}
td.pu {border-top-left-radius: 12px; border-top-right-radius: 12px;}

a minha tabela html é:

<table >
    <tbody>
      <tr >
        <td >Hello</td><td >Hello</td>
      </tr>
      <tr >
         <td >Hello</td><td >Hello</td>
      </tr>
      <tr >
         <td >Hello</td><td >Hello</td>
      </tr>
      <tr >
      <td >Hello</td><td >Hello</td>
      </tr>
    </tbody>
  </table>
O que me dá isto.

table with rounded corner

Como é que resolvo este problema, uma vez que os elementos td dentro da mesa e no meio da mesa também têm cantos arredondados? Só preciso que a primeira fila e a última fila tenham cantos arredondados.

Author: Brian Tompsett - 汤莱恩, 2013-05-19

7 answers

Assumindo que o html do seu table se assemelha ao seguinte:

<table>
    <thead>
        <tr>
            <th>First column</th>
            <th>Second column</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Row one, cell one</td>
            <td>Row one, cell two</td>
        </tr>
        <tr>
            <td>Row two, cell one</td>
            <td>Row two, cell two</td>
        </tr>
        <tr>
            <td>Row three, cell one</td>
            <td>Row four, cell two</td>
        </tr>
    </tbody>
</table>

Os seguintes CSS devem funcionar:

table {
    border-spacing: 0;
}
th, td {
    border: 1px solid #000;
    padding: 0.5em 1em;
}
/* the first 'th' within the first 'tr' of the 'thead': */
thead tr:first-child th:first-child {
    border-radius: 0.6em 0 0 0;
}
/* the last 'th' within the first 'tr' of the 'thead': */
thead tr:first-child th:last-child {
    border-radius: 0 0.6em 0 0;
}
/* the first 'td' within the last 'tr' of the 'tbody': */
tbody tr:last-child td:first-child {
    border-radius: 0 0 0 0.6em;
}
/* the last 'td' within the last 'tr' of the 'tbody': */
tbody tr:last-child td:last-child {
    border-radius: 0 0 0.6em 0;
}

Js Fiddle demo .

Editado em resposta a uma pergunta do OP:

A fronteira dentro da mesa é um pouco pensar, Como faço para que seja 1px

As fronteiras entre as células são um pouco grossas, porque o contorno esquerdo de uma célula é contra o contorno direito das células anteriores (e o mesmo para cima/baixo borda).

Uma maneira de remover esse efeito é especificar border-collapse: collapse; no elemento table. Infelizmente o efeito disto é Também remover/desactivar / anular as declarações border-radius: demo.

A forma mais complicada é remover manualmente os contornos superiores das linhas com uma linha anterior, e o contorno esquerdo de uma célula que segue uma célula, adicionando o seguinte ao CSS anterior:

thead + tbody tr td,
tr + tr td {
    border-top: 0;
}

tr th + th,
tr td + td {
    border-left: 0;
}

Js Fiddle demo .

Edited to reduce make the CSS mais durável (para linhas unicelulares, para a adição de um tfoot ou a remoção do thead):

table {
    border-spacing: 0;
}
th, td {
    border: 1px solid #000;
    padding: 0.5em 1em;
}
thead tr:first-child th:first-child,
tbody:first-child tr:first-child td:first-child {
    border-top-left-radius: 0.6em;
}
thead tr:first-child th:last-child,
tbody:first-child tr:first-child td:last-child {
    border-top-right-radius: 0.6em;
}

thead + tbody tr:last-child td:first-child,
tfoot tr:last-child td:first-child {
    border-bottom-left-radius: 0.6em;
}
thead + tbody tr:last-child td:last-child,
tfoot tr:last-child td:last-child {
    border-bottom-right-radius: 0.6em;
}

thead + tbody tr td,
tfoot + tbody tr td,
tfoot tr td,
tbody + tbody tr td,
tr + tr td {
    border-top: 0;
}

tr th + th,
tr td + td {
    border-left: 0;
}

Js Fiddle demo .

Há um problema com múltiplos elementos tbody, na ausência de um elemento tfoot, actualmente no qual o primeiro tbody irá manter o raio-fronteira nas suas fronteiras inferiores.

 7
Author: David Thomas, 2013-05-19 14:44:21
Podes pôr a mesa no div. Estilos do div (exemplo):
div {
  border-radius: 4px;
  -moz-border-radius: 4px
  -webkit-border-radius: 4px;
  overflow: hidden; /*notice*/
}
Então os cantos da mesa estarão escondidos.
 6
Author: Dimitry, 2014-10-17 15:39:00
Esta resposta não responde directamente à sua pergunta, mas sim a uma variante. Se não quiser que as colunas médias tenham cantos arredondados, esta é uma solução possível:

Ilustração:

enter image description here

Propriedades da linha da tabela (tr): actualizar os dados da tabela (td) para a coluna mais à esquerda:

tbody tr td:first-child
{
    border-radius: 0.6em 0 0 0.6em;
}

Propriedades da linha do quadro (tr): actualizar os dados da tabela (td) para a segunda coluna:

tbody td:nth-child(2)
{
    border-radius: 0 0.6em 0.6em 0;
}

Aqui está um exemplo: js Fiddle demo

Se tenha mais de uma coluna (td ou th) você simplesmente adiciona algo como isto:

tbody td:nth-child(2) /* This is now the middle element out of three */
{
    border-radius: 0 0 0 0;
}
tbody td:nth-child(3) /* This is now the right most column */
{
    boder-radius: 0 0.6em 0.6em 0;
}
 2
Author: rtc11, 2014-07-12 11:02:46
Podes reiniciar o raio de fronteira do elemento td. Isso deve resolver tudo.
 0
Author: marciano, 2013-05-19 13:36:53

Você pode dar id aos elementos td e usando os ids dos elementos td definir o raio de contorno para 0px.

 0
Author: Vivek Sadh, 2013-05-19 13:37:57

Devias tentar um clear:both; e será reiniciado. Também você pode tentar !important porque talvez você esqueça outras "regras CSS inline" em outros códigos html.

 0
Author: Mehmet Taha Meral, 2013-05-19 13:39:47
Embora esta seja uma resposta antiga, gostaria de a realçar adicionando as minhas descobertas. Além da resposta super-inteligente de David Thomas, encontrei uma caixa de ponta onde não se encaixa exactamente: uma fila de uma única célula! por exemplo:
<table>
   <tr><th colspan="3">My header</th></tr>
   <tr><td>row1-cell1</td><td>row1-cell2</td><td>row1-cell3</td></tr>
   <tr><td>row2-cell1</td><td>row2-cell2</td><td>row2-cell3</td></tr>
   <tr><th colspan="3">My footer</th></tr>
</table>

A regra para O canto superior direito substituiria a regra para o canto superior esquerdo (porque ela vem depois dela), e a regra para o canto inferior direito substituiria a regra para o canto inferior esquerdo (pelo mesmo motivo). Ver ecrã shot below:

Sobrepor os cantos http://i57.tinypic.com/v41091.jpg

O remédio para isso é o css abaixo (adicionei mais selectores para várias combinações table-tr, tbody-tr, thead-tr conforme necessário, de modo que você também pode expandi-lo para caber na sua marcação):

        table td,
        table th{
            border: 1px solid #666;
        }

        table{
            width: 98%;
            border-spacing: 0px;
        }

         /* alternating table row colors*/
        tr:nth-child(even)  { background-color:#f6f6f6; }
        tr:nth-child(odd)   { background-color:#ffffff; }

        /* set all corner radii initially to zero */
        th, td {
            border-radius: 0;
        }

        /*set only specific radii per corner (don't use the border-radius shorthand)*/
        thead tr:first-child th:first-child, 
        table tr:first-child td:first-child,
        tbody tr:first-child th:first-child {
            border-top-left-radius: 0.6em;
        }
        thead tr:first-child th:last-child, 
        table tr:first-child td:last-child,
        tbody tr:first-child th:last-child {
            border-top-right-radius: 0.6em;
        }
        tbody tr:last-child td:first-child, 
        table tr:last-child td:first-child,
        tbody tr:last-child th:first-child {
            border-bottom-left-radius: 0.6em;
        }
        tbody tr:last-child td:last-child, 
        table tr:last-child td:last-child,
        tbody tr:last-child th:last-child {
            border-bottom-right-radius: 0.6em;
        }

        thead + tbody tr td,
        tr + tr td ,
        tr + tr th {
            border-top: 0;
        }

        tr th + th,
        tr td + td {
            border-left: 0;
        }

        /* shade th cells */
        table th {
            background-color: #888;
            color: #FFF;
        }

Isto resulta na imagem abaixo, como desejado:

Cantos fixos http://i57.tinypic.com/5bqwqq.jpg

Todo o crédito por esta solução ainda vai para David Thomas , especialmente para o truque da fronteira das celas adjacentes!

 0
Author: kennasoft, 2017-05-23 12:10:09