目标最终
在一组使用 CSS 的

发布于 2024-11-29 18:33:45 字数 331 浏览 1 评论 0原文

我有以下代码:

<div class="sidebar">1</div>

<div class="sidebar">2</div>

<div class="sidebar">3</div>

如何使用 CSS 定位最后一个 div 元素?如果可以的话我想改变一下造型。

<div class="sidebar">3</div>

遗憾的是,我无权访问生成的 HTML,那么这可能吗?

I have the following code:

<div class="sidebar">1</div>

<div class="sidebar">2</div>

<div class="sidebar">3</div>

How do I use CSS to target the last div element? I'd like to change the styling if possible.

<div class="sidebar">3</div>

Sadly, I don't have access to the HTML generated, so is this even possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界和平 2024-12-06 18:33:45

您可以使用 .sidebar:last-child,请参阅:http://jsfiddle.net /thirtydot/JJw4w/

如果您的

是其父元素的最后一个元素,则会选择它。

所有现代浏览器和 IE9+ 均支持 :last-child不是 IE8!)。

You can use .sidebar:last-child, see: http://jsfiddle.net/thirtydot/JJw4w/

That will select your <div class="sidebar">3</div> if it's the last element of its parent element.

:last-child is supported in all modern browsers and IE9+ (not IE8!).

意中人 2024-12-06 18:33:45

如果无法访问 HTML,您最好的选择是使用 last-child CSS 选择器。

这假设 div 是某个父元素中的最后一项,以便:

<div id="container">
<div></div>
<div></div>
<div></div>
</div>

您可以这样做:

#container div:last-child{}

Without having access to the HTML your best bet is to go with the last-child CSS Selector.

This assumes that the div is the last item in some parent element so that:

<div id="container">
<div></div>
<div></div>
<div></div>
</div>

You could do:

#container div:last-child{}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文