:target是什么?
MDN是这样描述的::target
The :target pseudo-class represents the unique element, if any, with an id matching the fragment identifier of the URI of the document.
在document中,可以设置锚链接,举个例子:
上面存在两个锚链接:#tab和t#ab2。当点击锚链接时,就会跳到对应的div,则::target就是给这些div用的。添加一下CSS
点击锚链接,对应链接的div的文本变成红色,另外,给#tab后面插入一个文本。
效果猛戳:https://jsfiddle.net/dwqs/cL8rawov/
应该大致明白了:target的含义了吧~
:target可以做什么
最简单的用处:利用:target实现选项卡切换。
HTML:
CSS:
看看效果:Demo
当然,:target的功能不仅局限于此。隐藏元素、创建lightbox 等。MDN上给了很多个demo:more demo.
你自己也可以脑洞大开一下,哈哈。
浏览器支持
对于:target伪类,浏览器支持情况还是不错的。
新闻热点
疑难解答