A
A
Amir2018-02-21 12:55:50
css
Amir, 2018-02-21 12:55:50

How to apply style to the last element with a class if there are other elements further on?

Hello. There is a table:

<table>
            <thead>
                <tr>
                    <th class="show">One</th>
                    <th class="show">Two</th>
                    <th class="show">Three</th>
                    <th class="show">Four</th>
                    <th class="show">Five</th>
                    <th class="show">Six</th>
                    <th class="hide">Seven</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td class="show">1</td>
                    <td class="show">2</td>
                    <td class="show">3</td>
                    <td class="show">4</td>
                    <td class="show">5</td>
                    <td class="show">6</td>
                    <td class="hide">7</td>
                </tr>
                <tr>
                    <td class="show">1</td>
                    <td class="show">2</td>
                    <td class="show">3</td>
                    <td class="show">4</td>
                    <td class="show">5</td>
                    <td class="show">6</td>
                    <td class="hide">7</td>
                </tr>
                <tr>
                    <td class="show">1</td>
                    <td class="show" class="show">2</td>
                    <td class="show">3</td>
                    <td class="show">4</td>
                    <td class="show">5</td>
                    <td class="show">6</td>
                    <td class="hide">7</td>
                </tr>
                <tr>
                    <td class="show">1</td>
                    <td class="show">2</td>
                    <td class="show">3</td>
                    <td class="show">4</td>
                    <td class="show">5</td>
                    <td class="show">6</td>
                    <td class="hide">7</td>
                </tr>
        </tbody>
    </table>

body {
    margin: 0;
    background: #eee;
}

table {
    width: calc(100% - 40px);
    margin: 40px 20px;
    border-collapse: separate;
    border-spacing: 0 10px;
}

table tbody tr {
    cursor: pointer;
    border-radius: 10px;
    z-index:0;
    position: relative;
}

table tbody tr:hover {
    box-shadow: 0 12px 10px 0 rgba(0,0,0,0.15);
    z-index: 100;
}

table tbody tr td {
    background-color: #fff;
    padding: 10px 20px;
    position: relative;
    z-index: -1;
}

table tbody tr > td:first-child {
    border-radius: 10px 0 0 10px;
}

/* Вот здесь загвоздка */
table tbody tr > td.show:last-child {
    border-radius: 0 10px 10px 0;
}

.hide {
  display: none;
}

I want the wrap (border-radius) to be on the edge element with the .show class. But for some reason it doesn't render.
https://jsfiddle.net/74qcu8gt/14/
What to do?
Thanks in advance for the replies.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton fon Faust, 2018-02-21
@bubandos

Because selectors are read from right to left.
td.show:last-child should literally read like this:
take the last child, and apply the style "border-radius: 0 10px 10px 0;" to it, if it also has a .show class and is a td and is directly nested in tr , which is nested at any level in tbody, which is nested at any level in table.
For this particular task, you can use, for example, the following selector:
table tbody tr > td.show:nth-last-of-type(2)

E
Egor Lavrenov, 2018-02-21
@EgorWeb

tr td .show:last-child

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question