简体   繁体   English

:最后一个孩子不工作

[英]:last-child is not working

This is my first post here, so please tell me, if I'm doing something wrong. 这是我在这里的第一篇文章,所以如果我做错了,请告诉我。 I have a problem with the following code: 我的以下代码有问题:

That's my HTML: 那是我的HTML:

            <div class="sideSection">
            <div class="sidebar">
            <div class="mainContainer">
            <div class="articleHeader">
            <h3>Sapien lorem tempus</h3>
            <h4>Pharetra sed bibendum lorem</h4>
            </div>
            <p>Nunc cursus quam vitae ipsum viverra luctus. Nam nullam feugiat massa lacinia lectus vitae. Sed eu turpis at adipiscing.</p>
            <div class="sidebarBox">
            <img src="images/pic04.jpg" alt="ernster Blick">
            <div class="sidebarBoxText">
            <a href="#">Cursus quam vitae</a>
            <p>Nunc cursus quam vitae ipsum viverra luctus sapien.</p>
            </div>
            </div>
            <div class="sidebarBox">
            <img src="images/pic05.jpg" alt="Straße">
            <div class="sidebarBoxText">
            <a href="#">Etiam at orci ut nibh</a>
            <p>Nunc cursus quam vitae ipsum viverra luctus sapien.</p>
            </div>
            </div>
            <div class="sidebarBox">
            <img src="images/pic06.jpg" alt="telefonierender Blick">
            <div class="sidebarBoxText">
            <a href="#">Mauris non tellus</a>
            <p>Nunc cursus quam vitae ipsum viverra luctus sapien.</p>
            </div>
            </div>
            <div class="sidebarBox">
            <img src="images/pic07.jpg" alt="Skyscraper">
            <div class="sidebarBoxText">
            <a href="#">Duis id ipsum</a>
            <p>Nunc cursus quam vitae ipsum viverra luctus sapien.</p>
            </div>
            </div>
            <div class="moreInfoButton"><a href="#">More Info</a></div>
            </div>
            </div>
            </div>

And here is my CSS: 这是我的CSS:

.sideSection .sidebar,
.sideSection .sidebar .sidebarBox {
float: left;
}
.sideSection .sidebar .sidebarBox {
padding: 25px 0px 20px;
border-bottom-style: solid;
border-width: 1px;
border-color: #ccc;
}
.sideSection .sidebar .sidebarBox:last-child {
border: none;
}

Now, what I'm trying to do, is to remove the border from the last "sidebarBox" class. 现在,我想做的是从最后一个“ sidebarBox”类中删除边框。 This isn't quite working out yet, because the code isn't affecting the border at all. 这还没有完全解决,因为代码根本不影响边框。 I don't really know what I'm doing wrong, and I hope that someone can help me here, as it is for work :S 我真的不知道我在做什么错,我希望有人可以在这里为我提供帮助,因为它适合工作:S

EDIT: Okay, as far as I figured it out by now, it's a problem with the s. 编辑:好的,据我目前所知,这是s的问题。 When I use "".sidebarBox:nth-child(6)"" it works just fine.. Does the ":last-child" attribute actually count all s that I used in that section? 当我使用“” .sidebarBox:nth-​​child(6)“”时,它就可以正常工作。“ :: last-child”属性实际上计算该部分中使用的所有s吗?

:last-child only matches the actual last child of the parent div. :last-child仅匹配父div的实际最后一个孩子。

You have the .moreinfoButton following it. 您后面有.moreinfoButton If you can move the button to outside the .mainContainer , it will work as you expect it 如果您可以将button移到.mainContainer之外,它将按您期望的那样工作

The :last-child selector applies to the last element inside a parent div. :last-child选择器应用于父div中的最后一个元素。 In your case the parent div is .mainContainer and its last child is .moreInfoButton and not .sidebarBox . 在您的情况下,父div是.mainContainer ,其最后一个孩子是.moreInfoButton而不是.sidebarBox

I would recommend you to add last class to the sidebarBox you want to modify, ie <div class="sidebarBox last"> 我建议您将last类添加到要修改的sidebarBox中,即<div class="sidebarBox last">

and the corrspondent css code is 和相应的CSS代码是

.sideSection .sidebar .sidebarBox.last { border: none; }

Or another way to handle it is to wrap all your .sidebarBox elements in one parent <div> . 或另一种处理方法是将所有.sidebarBox元素包装在一个父<div> Then you would be able to use :last-child selector. 然后,您将可以使用:last-child选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM