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.
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.
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;
}
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;
}
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;
}
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.
div {
border-radius: 4px;
-moz-border-radius: 4px
-webkit-border-radius: 4px;
overflow: hidden; /*notice*/
}
Então os cantos da mesa estarão escondidos.
Ilustração:
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;
}
Você pode dar id aos elementos td e usando os ids dos elementos td definir o raio de contorno para 0px.
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.
<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!