Blogger Information
Blog 26
fans 0
comment 3
visits 17763
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
css的引用和css选择器
后网络时代
Original
656 people have browsed it

1. 实例演示css规则的三种引入到html文档中的方式;

答:
行内样式:通过选择器的style属性来设置:

  1. <h3 style="color:red;background-color:cyan;">通过选择器的style属性来设置</h3>

内部样式(嵌入样式):写在head标签间的style标签中,仅对当前页面有效:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style type="text/css">
  8. h3{
  9. background-color:linen;
  10. color:navy;
  11. font-size: 24px;
  12. line-height: 35px;
  13. text-decoration:line-through;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <h3>此价格已经取消</h3>
  19. </body>
  20. </html>

效果:

引入外部样式:这样样式可以共享;
css.css:

  1. *{
  2. margin:0;
  3. padding:0;
  4. box-sizing: border-box;
  5. }
  6. body{
  7. background-color:steelblue
  8. }
  9. .login{
  10. position:absolute;
  11. width:300px;
  12. border:1px solid #ccc;
  13. top:50%;
  14. margin-top:-71px;
  15. left:50%;
  16. margin-left:-150px;
  17. font-size: 14px;
  18. text-align: left;
  19. text-indent:40px;
  20. height: 142px;
  21. border-radius: 6px;
  22. box-shadow:1px 1px 15px #ccc;
  23. background-color: #fff;
  24. }
  25. .login form p{
  26. line-height:35px;
  27. word-spacing:10px;
  28. }
  29. .login form p input{
  30. width:150px;
  31. }
  32. .login form p input[name="useryzm"]{
  33. width:50px;
  34. }
  35. .login form p:nth-of-type(4){
  36. text-indent:120px;
  37. }
  38. .login form p button{
  39. width:80px;
  40. }
  41. .login form p span{
  42. line-height:25px;
  43. background-color: #ccc;
  44. margin-left: 3px;
  45. vertical-align:1px;
  46. word-spacing: 20px;
  47. }

outcss.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <link rel="stylesheet" href="css/css.css" />
  8. </head>
  9. <body>
  10. <div class="login">
  11. <form action="">
  12. <p><label for="username">用户名:</label><input type="text" name="username" id="username" value="" /></p>
  13. <p><label for="userpwd">密&nbsp;码:</label><input type="password" name="userpwd" id="userpwd" value="" /></p>
  14. <p><label for="useryzm">验证码:</label><input type="text" name="useryzm" id="useryzm" value="" /><span>123456</span></p>
  15. <p><button>登陆</button></p>
  16. </form>
  17. </div
  18. </body>
  19. </html>

效果:

在style标签或者css文件中使用inmport引用

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style type="text/css">
  8. @import url(css/css.css);
  9. </style>
  10. </head>
  11. <body>
  12. <div class="login">
  13. <form action="">
  14. <p><label for="username">用户名:</label><input type="text" name="username" id="username" value="" /></p>
  15. <p><label for="userpwd">密&nbsp;码:</label><input type="password" name="userpwd" id="userpwd" value="" /></p>
  16. <p><label for="useryzm">验证码:</label><input type="text" name="useryzm" id="useryzm" value="" /><span>123456</span></p>
  17. <p><button>登陆</button></p>
  18. </form>
  19. </div
  20. </body>
  21. </html>

效果:

2. 实例演示标签选择器,class选择器,id选择器,上下文选择器, 结构伪类选择器,重点是结构伪类选择器

答案:

标签选择器,class选择器,id选择器:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <style>
  9. /* 标签选择器: */
  10. p{
  11. font-family:Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;
  12. height:35px;
  13. line-height: 35px;
  14. border:1px solid red;
  15. box-sizing: border-box;
  16. }
  17. a{
  18. color:red;
  19. }
  20. a:link{
  21. color:blueviolet;
  22. }
  23. a:visited{
  24. color:chartreuse;
  25. }
  26. a:hover{
  27. color:mediumblue;
  28. }
  29. a:active{
  30. color:teal;
  31. }
  32. .title{
  33. color:red;
  34. }
  35. /* id主要是为js服务的,故需要人为保持唯一性 */
  36. #id1{
  37. color:cyan
  38. }
  39. </style>
  40. <body>
  41. <P>p标签选择器</P>
  42. <a href="">百度</a>
  43. <h3 class="title">h3.title文章标题</h3>
  44. <p id="id1">id选择器</p>
  45. <script>
  46. document.querySelector("a").addEventListener('click',show);
  47. function show(ev){
  48. ev.preventDefault();
  49. }
  50. </script>
  51. </body>
  52. </html>

效果:

标签选择器,class选择器,id选择器:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>上下文选择器</title>
  7. </head>
  8. <style>
  9. /* 只是直系子元素 */
  10. div.ft div{
  11. width:300px;
  12. float: left;
  13. margin-left: 12px;
  14. background-color: oldlace;
  15. }
  16. div>ul>li{
  17. width:100%;
  18. border-bottom:1px solid red;
  19. list-style: none;
  20. font-size:14px;
  21. line-height: 30px;
  22. }
  23. /* 后台选择器: */
  24. div ul li{
  25. width:100%;
  26. border-bottom:1px solid red;
  27. list-style: none;
  28. font-size:14px;
  29. line-height: 30px;
  30. }
  31. div.add{
  32. width:300px;
  33. }
  34. div.add p+p{
  35. background-color: orangered;
  36. }
  37. div.ft{
  38. overflow:hidden;
  39. }
  40. div.test{
  41. width:500px;
  42. background-color:orangered;
  43. }
  44. div.test h3 ~ p{
  45. background-color:navy;
  46. color:orangered;
  47. }
  48. </style>
  49. <body>
  50. <div class="ft">
  51. <div>
  52. <h3>div>ul>li:</h3>
  53. <ul>
  54. <li>测试信息1子元素</li>
  55. <li>测试信息2子元素</li>
  56. <li>测试信息3子元素</li>
  57. <ul>
  58. <li>测试信息孙子元素</li>
  59. </ul>
  60. </ul>
  61. </div>
  62. <div>
  63. <h3>div ul li:</h3>
  64. <ul>
  65. <li>测试信息1子元素</li>
  66. <li>测试信息2子元素</li>
  67. <li>测试信息3子元素</li>
  68. <ul>
  69. <li>测试信息孙子元素</li>
  70. </ul>
  71. </ul>
  72. </div>
  73. <div class="add">
  74. <h3>相邻选择符号E+F:选择紧贴在E元素之后F元素</h3>
  75. <p>选择符号一</p><p>选择符号</p><h4>间隔一个</h4><p>间隔一个元素的p</p>
  76. </div>
  77. </div>
  78. <div class="test">
  79. <h3>兄弟选择符(E~F):选择E元素后面的所有兄弟元素F,元素E与F必须同属一个父级。</h3>
  80. <p>p兄弟选择符(E~F)</p>
  81. <h3>h4兄弟选择符(E~F)</h3>
  82. <p>p兄弟选择符(E~F)</p>
  83. <p>p兄弟选择符(E~F)</p>
  84. </div>
  85. </body>
  86. </html>

效果:

结构伪类选择器:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <style type="text/css">
  9. /* 常用伪类 */
  10. /* 所有的子元素li */
  11. ul li{
  12. list-style: none;
  13. }
  14. /* ul的第一个子元素li */
  15. ul li:first-child{
  16. color:red;
  17. }
  18. /* ul的最后一个子元素li */
  19. ul li:last-child{
  20. color:cyan;
  21. }
  22. /* ul的第二个子元素li */
  23. ul li:nth-child(2){
  24. color:chartreuse;
  25. }
  26. /* ul的倒数第二个子元素li */
  27. ul li:nth-last-child(2){
  28. color:chartreuse;
  29. }
  30. /* 匹配dl中第一个同级元素 n从1开始*/
  31. dl dd:first-of-type{
  32. color:red;
  33. }
  34. /* 匹配dl中最后一个同级元素dt */
  35. dl dt:last-of-type{
  36. color:red;
  37. }
  38. /* dl dt:last-of-type{
  39. color:red;
  40. } */
  41. /* 匹配dl中唯一的同级元素h3 */
  42. dl h3:only-of-type{
  43. color:red;
  44. }
  45. /* 匹配dl中第二个同级元素dd,如果不是dd则无效 */
  46. dl dd:nth-of-type(2) {
  47. color:darkblue;
  48. }
  49. /* 匹配dl中倒数第二个同级元素dd,如果不是dd则无效 */
  50. dl dd:nth-last-of-type(2){
  51. color:darkblue;
  52. }
  53. /* //n从1开始 偶数行*/
  54. div.n1 p:nth-of-type(2n){
  55. color:red;
  56. }
  57. /* //n从1开始 奇数行*/
  58. div.n2 p:nth-of-type(2n+1){
  59. color:darkblue;
  60. }
  61. /* //从零开始 */
  62. div.n3 p:nth-of-type(odd){
  63. color:red;
  64. }
  65. div.n4 p:nth-of-type(even){
  66. color:darkgreen;
  67. }
  68. </style>
  69. <body>
  70. <ul>
  71. <li>111</li>
  72. <li>2</li>
  73. <li>3</li>
  74. <li>4</li>
  75. <li>5</li>
  76. </ul>
  77. <dl>
  78. <dt>今晚今晚要唱到爆炸</dt>
  79. <dd>今晚今晚要唱到爆炸</dd>
  80. <dt>今晚今晚要唱到爆炸</dt>
  81. <dd>今晚今晚要唱到爆炸</dd>
  82. <dt>今晚今晚要唱到爆炸</dt>
  83. <dd>今晚今晚要唱到爆炸</dd>
  84. <h3>唯一同级元素</h3>
  85. </dl>
  86. <h3>2n(n=1,2,3,...)</h3>
  87. <div class="n1">
  88. <p>奇偶数选择</p>
  89. <p>奇偶数选择</p>
  90. <p>奇偶数选择</p>
  91. </div>
  92. <h3>2n+1(n=1,2,3,...)</h3>
  93. <div class="n2">
  94. <p>奇偶数选择</p>
  95. <p>奇偶数选择</p>
  96. <p>奇偶数选择</p>
  97. </div>
  98. <h3>odd</h3>
  99. <div class="n3">
  100. <p>奇偶数选择</p>
  101. <p>奇偶数选择</p>
  102. <p>奇偶数选择</p>
  103. </div>
  104. <h3>even</h3>
  105. <div class="n4">
  106. <p>奇偶数选择</p>
  107. <p>奇偶数选择</p>
  108. <p>奇偶数选择</p>
  109. </div>
  110. </body>
  111. </html>

效果:

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post