2016-06-19 15 views
5

Bir tablom var ve her satırın ilk td numaralı satırına satır numarası eklemek için sözlü öğeden önce CSS :: komutunu kullanmak istiyorum. Benim sorunum, ilk td sararsa ikinci satırın numarası altına girmesi ve bunun üstündeki metinle satır içi olarak başlamasını istiyorum. Bu sadece CSS ile yapılabilir mi?Sahte elemandan önce :: sonra bir girinti nasıl?

Örnek:

Image example of what I would like to accomplish.

Düzenleme

https://jsfiddle.net/3yL19zde/

HTML:

<table> 
    <tr> 
    <td>Some really long text that wraps to a second line...</td> 
    <td>Some other column</td> 
    <td>A third column</td> 
    </tr> 
    <tr> 
    <td>Some really long text that wraps to a second line...</td> 
    <td>Some other column</td> 
    <td>A third column</td> 
    </tr> 
</table> 

CSS:

table { 
    width: 480px; 
} 

table td { 
    vertical-align: text-top; 
} 

table tr { 
    counter-increment: rowNumber; 
} 

table tr td:first-child::before { 
    content: counter(rowNumber); 
    min-width: 1em; 
    margin-right: 0.5em; 
    font-weight: bold; 
} 
bağımsız bir table-cell olarak görüntülenebilir böylece, satırın başında yerine ilk hücre içinde sahte elemanı sokmak

cevap

5

:

table { 
 
    width: 500px; 
 
} 
 
tr { 
 
    counter-increment: row; 
 
} 
 
tr::before { 
 
    content: counter(row); 
 
    display: table-cell; 
 
    font-weight: bold; 
 
} 
 
tr::before, td { 
 
    border: 1px dotted #888; 
 
    padding: 5px; 
 
}
<table> 
 
    <tr> 
 
    <td>Some really long text that wraps to a second line...</td> 
 
    <td>Some other column</td> 
 
    <td>A third column</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Some really long text that wraps to a second line...</td> 
 
    <td>Some other column</td> 
 
    <td>A third column</td> 
 
    </tr> 
 
</table>

+0

Ben ekranda 'bilmiyordum: masa cell', bu tam olarak ne gerekli olduğunu. –

+1

Not 'ekranı: tablo hücresi' tamamen vazgeçilmez değildir. Bunu kullanmazsanız, sözde öğe, [anonim tablo nesneleri] 'ne göre anonim bir tablo hücresine sarılır (https://www.w3.org/TR/CSS21/tables.html#anonymous-boxes), böylece metin hala istediğiniz gibi "girintili" olacaktır. Ancak stil amaçları için sözde elemanın hücre kutusu olması daha kolay. – Oriol

0

İşte aynı hücre içinde yapmanın bir yolu (text-indent ve bir miktar dolgu).

table { 
 
    counter-reset: rowNumber; 
 
    width: 480px; 
 
} 
 
td { 
 
    border: 1px dotted #888; 
 
} 
 
td:first-child { 
 
    padding-left: 2em; 
 
    text-indent: -2em; 
 
} 
 
td:first-child:before { 
 
    counter-increment: rowNumber; 
 
    content: counter(rowNumber); 
 
    font-weight: bold; 
 
    display: inline-block; 
 
    text-indent: 0; 
 
    width: 2em; 
 
}
<table> 
 
    <tr> 
 
     <td>Some really long text that wraps to a second line...</td> 
 
     <td>Some other column</td> 
 
     <td>A third column</td> 
 
    </tr> 
 
    <tr> 
 
     <td>Some really long text that wraps to a second line...</td> 
 
     <td>Some other column</td> 
 
     <td>A third column</td> 
 
    </tr> 
 
</table>

İlgili konular