Alexander Sivashev
Alexander Sivashev

Reputation: 83

Column and row highlight on hover in striped table in CSS

I'm using trick for CSS only highlight of table column on hover from https://css-tricks.com/simple-css-row-column-highlighting/

It's work perfect, but not for striped table via

tr:nth-of-type(odd) {
    background-color: #f0f0f0;
}

Highlighting not apply to upper and bottom cells in rows with background.

See example here: http://jsfiddle.net/615avo4v/

Please, help me fix this issue without JavaScript.

Thanks in advance!

Upvotes: 2

Views: 6397

Answers (1)

vals
vals

Reputation: 64174

Change the way you set the stripped background.

Use another pseudo element, this time on the first td of the even rows, and aligned horizontally. (and with a lower z-index).

* {
    margin: 0;
    padding: 0;
}

table {
    width: 100%;
    border-spacing: 0;
    color: #212121;
    text-align: left;
    overflow: hidden;
}

table>tbody>tr>td {
    padding: 10px;
    font-size: 14px;
    position: relative;
}


table>tbody>tr:hover {
    padding: 20px;
    background-color: #ffa !important;
}

tr:nth-child(even) td:first-child::before {
    content: "";
    position: absolute;
    background-color: lightgreen;
    top: 0;
    left: -5000px;
    width: 10000px;
    height: 100%;
    z-index: -10;
}
td:hover::after {
    content: "";
    position: absolute;
    background-color: #ffa;
    left: 0;
    top: -5000px;
    height: 10000px;
    width: 100%;
    z-index: -1;
}
 <table>
        <tr>
            <td>col1</td>
            <td>col2</td>
            <td>col3</td>
            <td>col4</td>
            <td>col5</td>
            <td>col6</td>
            <td>col7</td>
            <td>col8</td>
        </tr>
        <tr>
            <td>col1</td>
            <td>col2</td>
            <td>col3</td>
            <td>col4</td>
            <td>col5</td>
            <td>col6</td>
            <td>col7</td>
            <td>col8</td>
        </tr>
        <tr>
            <td>col1</td>
            <td>col2</td>
            <td>col3</td>
            <td>col4</td>
            <td>col5</td>
            <td>col6</td>
            <td>col7</td>
            <td>col8</td>
        </tr>
        <tr>
            <td>col1</td>
            <td>col2</td>
            <td>col3</td>
            <td>col4</td>
            <td>col5</td>
            <td>col6</td>
            <td>col7</td>
            <td>col8</td>
        </tr>
        <tr>
            <td>col1</td>
            <td>col2</td>
            <td>col3</td>
            <td>col4</td>
            <td>col5</td>
            <td>col6</td>
            <td>col7</td>
            <td>col8</td>
        </tr>
        <tr>
            <td>col1</td>
            <td>col2</td>
            <td>col3</td>
            <td>col4</td>
            <td>col5</td>
            <td>col6</td>
            <td>col7</td>
            <td>col8</td>
        </tr>
    </table>

Upvotes: 5

Related Questions