目标最终在一组使用 CSS 的中
中
我有以下代码:
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
.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!).如果无法访问 HTML,您最好的选择是使用
last-child
CSS 选择器。这假设 div 是某个父元素中的最后一项,以便:
您可以这样做:
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:
You could do: