html - 无法选择最后一个类(class)

标签 html css

我正在尝试选择我创建的这 3 个类中的最后一个子类。我认为这是一个非常简单的修复。

我创建了一个片段以准确显示我遇到问题的地方。

.homeimage 				{ width: 30%; position: relative; display: inline-block; vertical-align: top; text-align: center; border: 1px solid blue; margin-right: 2%;}
.homeimage:last-class	{  margin-right: 0; border: 1px soild yellow; }
<div class="homeimage">[image|2]<br />
&nbsp;
<h4>Product 1</h4>
</div>
<!-- homeimage -->

<div class="homeimage">[image|2]<br />
&nbsp;
<h4>Product 2</h4>
</div>
<!-- homeimage -->

<div class="homeimage">[image|2]<br />
&nbsp;
<h4>Product 3</h4>
</div>
<!-- homeimage -->

最佳答案

问题是 last-class 不存在。

我建议使用 last-child 来选择父节点的最后一个元素。

这是使用 last-child 的代码段的修改版本。 请注意,我必须创建一个包装器 div 来触发 last-child 功能。

.homeimage 				{ width: 30%; position: relative; display: inline-block; vertical-align: top; text-align: center; border: 1px solid blue; margin-right: 2%;}
.homeimage:last-child	{  margin-right: 0; border: 1px solid yellow; }
    <div>
    <div class="homeimage">[image|2]<br />
    &nbsp;
    <h4>Product 1</h4>
    </div>
    <!-- homeimage -->

    <div class="homeimage">[image|2]<br />
    &nbsp;
    <h4>Product 2</h4>
    </div>
    <!-- homeimage -->

    <div class="homeimage">[image|2]<br />
    &nbsp;
    <h4>Product 3</h4>
    </div>
    <!-- homeimage -->
    </div>

关于html - 无法选择最后一个类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34081480/

相关文章:

java - 创建 10.000 个连接的六边形页面?

jquery - 使用 JQuery 使用搜索框隐藏 div

javascript - 替换表格每一行的所有div标签

css - 使用背景尺寸封面时,将视频固定到背景位置

CSS悬停对旋转div的影响

html - 在 body 中垂直居中一个div?

html - 通过将鼠标悬停在标签上来更改图像源

html - Transform-origin 在负旋转时真的很难

html - 为什么不能在 <o> 或 <k> 标签中使用 href?

javascript - 在 <head> : Good idea or bad idea? 中使用 <noscript>