2016-06-19 15 views
5

Mam tabelę i chcę użyć CSS :: before pseudo element z licznikiem, aby dodać numer wiersza do pierwszego td każdego wiersza. Mój problem polega na tym, że jeśli pierwszy td zawija, druga linia zaczyna się pod tym numerem i chciałbym, żeby zaczął on wstawiać tekst nad nim. Czy można to zrobić za pomocą samego CSS?Jak uzyskać wcięcie po :: before pseudo element?

Przykład:

Image example of what I would like to accomplish.

Edit

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

Odpowiedz

5

będzie włożeniu Pseudoelement na początku wiersza a nie wewnątrz pierwszej komórki, tak, że może być wyświetlona w postaci niezależnego table-cell:

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

Nie wiedziałem o 'wyświetlaczu: stół-cell', to jest dokładnie to, co potrzebne. –

+1

Uwaga 'display: table-cell' nie jest całkowicie niezbędna. Jeśli go nie użyjesz, pseudoelement zostanie zawinięty wewnątrz anonimowej komórki tabeli zgodnie z [anonimowymi obiektami tabel] (https://www.w3.org/TR/CSS21/tables.html#anonymous-boxes), więc tekst będzie nadal "wcięty", jak chcesz. Ale do celów stylizacji łatwiej jest, jeśli pseudoelement jest samą komórką. – Oriol

0

Oto sposób, aby to zrobić w tej samej komórce (używając text-indent i niektórych dopełnień).

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>

Powiązane problemy