css pseudo-classes: last-child etc don't work

【Foreword】

    Today, students encountered many problems in the practice project. Here is one: CSS pseudo-class: last-child, etc. do not work?

 

【Details】

     First, let me emphasize that the pseudo-class selectors `:first-child` and `:last-child` filter based on the parent. So if you want to style a tail element, you must add the parent element before it. That is, the parent element of the child element: last-child child element.

<style>
#icons{
    border: 1px solid bisque;
    height: 250px;
}
#icons .column{//Each column
    width: 25%;
    display: inline-block;
    float: left;
    text-align: center;
}
#icons .column .txt{//Every column of text
    width: 100%;
    height: 52px;
    margin-top: 134px;
    border-right: 2px solid #787b83;//Add spacer here
    
}
#icons .column .txt:last-child{
    border-right: none;
}
#icons .column .txt p{
    width: 160px;
    height: 52px;
    font-size: 14px;
    line-height: 18px;
    color: #767777;
    margin-left: 25%;
}

 

<div id="icons">
            <div class="column column1">
                <div class="txt">
                    <p>Create a new worldview and make you love your life more</p>
                </div>
            </div>
            <div class="column column2">
                <div class="txt">
                    <p>A variety of public welfare activities to develop the consciousness of the protagonist of the new world</p>
                </div>
            </div>
            <div class="column column3">
                <div class="txt">
                    <p>Fashion new ideas, experience the unknown life ahead of time</p>
                </div>
            </div>
            <div class="column column4">
                <div class="txt">
                    <p>Perfect training mechanism to cultivate your new world view</p>
                </div>
            </div>
        </div>
</div>

 The effect is as follows:

   Students generally think that after adding a spacer at `#icons .column .txt`, they want to set the last vertical spacer to `border: none;`, but as shown above, all of them are gone.

   The reason is that the pseudo-class selectors `:first-child` and `:last-child` are filtered according to the parent, the parent of `#icons .column .txt:last-child` is '.txt`, in There is only one child here, and the last child is of course itself, so to achieve the effect, you should add a pseudo-class to the parent of '.txt`.

#icons .column:last-child .txt{
    border-right: none;
}

 The effect is as follows:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326122726&siteId=291194637