当前位置:Gxlcms > 前端框架 > CSS的属性选择器使用详解(css入门教程)

CSS的属性选择器使用详解(css入门教程)

时间:2021-07-01 10:21:17 帮助过:77人阅读

对带有指定属性的 HTML 元素设置样式。 ( 推荐学习:CSS入门教程 )

可以为拥有指定属性的 HTML 元素设置样式,而不仅限于 class 和 id 属性。

注:只有在规定了 !DOCTYPE 时,IE7 和 IE8 才支持属性选择器。在 IE6 及更低的版本中,不支持属性选择。

属性选择器

下面的例子为带有 title 属性的所有元素设置样式:

  1. [title]
  2. {
  3. color:red;
  4. }

属性和值选择器

下面的例子为 title="W3School" 的所有元素设置样式:

  1. [title=hello]
  2. {
  3. border:5px solid blue;
  4. }

属性和值选择器 - 多个值

下面的例子为包含指定值的 title 属性的所有元素设置样式。适用于由空格分隔的属性值:

  1. [title~=hello] { color:red; }

下面的例子为带有包含指定值的 lang 属性的所有元素设置样式。适用于由连字符分隔的属性值:

  1. [lang|=en] { color:red; }

设置表单的样式

属性选择器在为不带有 class 或 id 的表单设置样式时特别有用:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4. <style>
  5. input[type="text"]
  6. {
  7. width:150px;
  8. display:block;
  9. margin-bottom:10px;
  10. background-color:yellow;
  11. font-family: Verdana, Arial;
  12. }
  13. input[type="button"]
  14. {
  15. width:120px;
  16. margin-left:35px;
  17. display:block;
  18. font-family: Verdana, Arial;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <form name="input" action="" method="get">
  24. <input type="text" name="Name" value="Bill" size="20">
  25. <input type="text" name="Name" value="Gates" size="20">
  26. <input type="button" value="Example Button">
  27. </form>
  28. </body>
  29. </html>

以上就是CSS的属性选择器使用详解(css入门教程)的详细内容,更多请关注Gxlcms其它相关文章!

人气教程排行