对带有指定属性的 HTML 元素设置样式。
可以为拥有指定属性的 HTML 元素设置样式,而不仅限于 class 和 id 属性。
只有在规定了 !DOCTYPE 时,IE7 和 IE8 才支持属性选择器。
在 IE6 及更低的版本中,不支持属性选择。
实例:属性选择器
<html> <head> <style type="text/css"> [title]{color:red;} </style> </head> <body> <h1>带有 title 属性,可以应用样式:</h1> <h2 title="Hello world">Hello world</h2> <a title="demo" href="demo.html">Hello world</a> <hr /> <h1>没有 title 属性,无法应用样式:</h1> <h2>Hello world</h2> <a href="demo.html">Hello world</a> </body> </html>注释:上面的例子为带有 title 属性的所有元素设置样式。
<html> <head> <style type="text/css"> [title=Hello]{border:5px solid blue;} </style> </head> <body> <h1>可以应用样式:</h1> <img title="Hello" src="hello.gif" /> <br /> <a title="Hello" href="hello.html">Hello world</a> <hr /> <h1>无法应用样式:</h1> <p title="greeting">Hello world</p> <a class="Hello" href="hello.html">Hello world</a> </body> </html>注释:下面的例子为 title="Hello" 的所有元素设置样式。
<!DOCTYPE html> <html> <head> <style> input[type="text"] { width:150px; display:block; margin-bottom:10px; background-color:yellow; font-family: Verdana, Arial; } input[type="button"] { width:120px; margin-left:35px; display:block; font-family: Verdana, Arial; } </style> </head> <body> <form name="input" action="" method="get"> <input type="text" name="Name" value="Bill" size="20"> <input type="text" name="Name" value="Gates" size="20"> <input type="button" value="Example Button"> </form> </body> </html>
评论