Blogger Information
Blog 18
fans 1
comment 0
visits 16087
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1215 作业
空瓶子
Original
591 people have browsed it

1215 作业

作业内容:1. 实例演示 css 规则的三种引入到 html 文档中的方式; 2. 实例演示标签选择器,class 选择器,id 选择器,上下文选择器, 结构伪类选择器,重点是结构伪类选择器

  • css 规则三种引入 html 文档的方式

1.内部样式:通过<style>标签引入 css 规则,仅适用于当前 html 页面
测试代码

  1. <style>
  2. /* 1.标签选择器 */
  3. li {
  4. background-color: red;
  5. }
  6. /* 2.类选择器 */
  7. .on {
  8. color: blue;
  9. }
  10. /* 3.id选择器 */
  11. /* 浏览器不检查id的唯一性,必须有开发者自行保证 */
  12. #foo {
  13. color: chartreuse;
  14. }
  15. </style>

2.外部样式(公共样式或共享样)
演示代码:
外部样式:

  1. header {
  2. background-color: aqua;
  3. height: 2em;
  4. }
  5. main {
  6. background-color: burlywood;
  7. min-height: 18em;
  8. }
  9. footer {
  10. background-color: #555;
  11. color: white;
  12. height: 2e;
  13. }

引入方式

2-1. @import url(文件路径)

  1. <style>
  2. @import url(css/style2.css);
  3. </style>

2-2. <link rel="stylesheet" href="文件路径" />
<link rel="stylesheet" href="css/style.css" />

3.内联样式:style 属性设置样式
<li class="on" style="color: green">item6</li>

HTML 代码:

  1. <div>
  2. <ul>
  3. <li id="foo">item1</li>
  4. <li class="on">item2</li>
  5. <li id="foo">item3</li>
  6. <li class="on">item4</li>
  7. <li>item5</li>
  8. <li class="on" style="color: red">item6</li>
  9. </ul>
  10. </div>

效果演示

标签选择器,class 选择器,id 选择器,上下文选择器, 结构伪类选择器

  • 标签选择器
  1. /* 标签选择器 */
  2. li {
  3. background-color: red;
  4. }
  • class 选择器
  1. /* 类选择器 */
  2. .on {
  3. color: blue;
  4. }
  • id 选择器
  1. /* id选择器 */
  2. /* 流浪器不检查id的唯一性,必须有开发者自行保证 */
  3. #foo {
  4. color: chartreuse;
  5. }
  • 上下文选择器
  1. /* 1.后代选择器,选中所有层级 */
  2. ul li {
  3. background-color: cyan;
  4. }
  5. /* 2.子元素选择器,仅选中父子层级 */
  6. div > ul > li {
  7. color: rgb(236, 14, 225);
  8. }
  9. /* 3.同级相邻选择器,仅选中相邻的兄弟元素 */
  10. .start + li {
  11. background-color: yellowgreen;
  12. }
  13. /* 4.同级所有选择器,与之相邻后面所有的元素 */
  14. .start2 ~ li {
  15. background-color: violet;
  16. }

效果演示

  • 结构为类选择器
  1. /* 1.匹配任意位置的元素: :nth-of-type(an+b) */
  2. /* an代表查询起点;b代偏移量 n从0开始取 */
  3. /* 匹配第三个li */
  4. ul li:nth-of-type(3) {
  5. background-color: red;
  6. }
  7. /* 选中所有元素 */
  8. /* ul li:nth-of-type(1n) {
  9. color: blue;
  10. } */
  11. /* 选中从第三个开始后面的所有元素 */
  12. ul li:nth-of-type(n + 3) {
  13. color: blue;
  14. }
  15. /* 反向获取任意位置的元素:nth-last-of-type() */
  16. /* 选中列表最后三个元素 */
  17. ul li:nth-last-of-type(-n + 3) {
  18. font-weight: bolder;
  19. }
  20. /* 选中倒数第三个元素 */
  21. ul li:nth-last-of-type(3) {
  22. font-size: larger;
  23. }
  24. /* 选中偶数的元素 偶数行evev*/
  25. ul li:nth-of-type(2n) {
  26. background-color: chartreuse;
  27. }
  28. /* 选中奇数的元素 奇数行odd*/
  29. ul li:nth-of-type(2n-1) {
  30. background-color: violet;
  31. }
  32. /* 语法糖 */
  33. /* 获取第一个元素 :first-of-type*/
  34. ul li:first-of-type {
  35. }
  36. /* 获取最后一个元素 :first-of-type*/
  37. ul li:last-of-type {
  38. }
  39. /* 匹配父元素中的唯一子元素 */
  40. ul li:only-of-type {
  41. background-color: red;
  42. }

效果演示

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!