CSS

@author:伏月廿柒

Cascading Style Sheet 层叠级联样式表

CSS:表现(美化)

字体,颜色,边距,高度,宽度,背景图片,网页定位,网页浮动……

CSS发展史

CSS 1.0

CSS 2.0 DIV(块) + CSS,HTML与CSS结构分离的思想,网页变得简单,利于SEO

CSS 2.1 浮动,定位

CSS 3.0 圆角,阴影,动画…… 浏览器兼容性

CSS的优势

1、内容和表现分离

2、网页结构表现统一,可以复用

3、样式十分丰富

4、建议使用独立于html的css文件

5、利于SEO,容易被搜索引擎收录!

style

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Demo01</title>
  8. <!--
  9. <style>语法:
  10. 选择器 {
  11. 声明1;
  12. 声明2;
  13. ……
  14. }
  15. -->
  16. <style>
  17. h1 {
  18. color: red;
  19. }
  20. </style>
  21. <!-- css外部引用,建议使用此方法 -->
  22. <link rel="stylesheet" href="../css/Demo01.css">
  23. </head>
  24. <body>
  25. <h1>标题1</h1>
  26. <h2>标题2</h2>
  27. </body>
  28. </html>
  1. /*Demo01.css*/
  2. h2 {
  3. color: blue;
  4. }

CSS导入方式

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Demo02</title>
  8. <!-- 内部样式 -->
  9. <style>
  10. h1 {
  11. color: green;
  12. }
  13. </style>
  14. <!-- 外部样式,链接引用 -->
  15. <link rel="stylesheet" href="../css/Demo02.css">
  16. <!-- 导入引用 -->
  17. <style>
  18. @import url("../css/Demo02.css");
  19. </style>
  20. </head>
  21. <body>
  22. <!-- 优先级:就近原则 -->
  23. <!-- 行内样式:在标签元素中,编写一个style属性,编写样式即可 -->
  24. <h1 style="color: red;">标题1</h1>
  25. </body>
  26. </html>
  1. /*Demo02.css*/
  2. /* 外部样式 */
  3. h1 {
  4. color: blue;
  5. }

拓展:外部样式两种写法

  • 链接式

    1. <link rel="stylesheet" href="XXX.css">
  • 导入式(css 2.1)

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

选择器

选择页面上的某一个或者某一类元素

基本选择器

  1. 标签选择器
  2. 类 选择器 class
  3. Id 选择器
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Demo03</title>
  8. <style>
  9. /* 标签选择器,会选择到页面上所有的这个标签的元素 */
  10. h1 {
  11. color: aqua;
  12. }
  13. /* 类选择器 */
  14. /*
  15. 格式:.XXX {}
  16. 特点:跨标签,可以多个标签归类
  17. */
  18. .h1_set_class {
  19. color: aquamarine;
  20. }
  21. /* id选择器 */
  22. /*
  23. 格式:#XXX {}
  24. 特点:id必须保证全局唯一
  25. */
  26. #h1_set_id {
  27. color: bisque;
  28. }
  29. #h2_set {
  30. color: cadetblue;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <h1>Java</h1>
  36. <h1 class="h1_set_class">html</h1>
  37. <h2 id="h2_set">python</h2>
  38. <!--
  39. 优先级:
  40. id选择器 > class选择器 > 标签选择器
  41. -->
  42. <h1 class="h1_set_class" id="h1_set_id">JS</h1>
  43. </body>
  44. </html>

层次选择器

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Demo04</title>
  8. <style>
  9. /* 后代选择器,作用与后面所有代 */
  10. body p {
  11. background: #57b4e6;
  12. }
  13. /* 子选择器,只作用于下一代 */
  14. body > h1 {
  15. background: #219143;
  16. }
  17. /* 相邻兄弟选择器,只作用于下一个 */
  18. .h1_set + h1 {
  19. background: #3f56d6;
  20. }
  21. /* 通用选择器,作用于向下的所有同级标签 */
  22. .h4_set ~ h1 {
  23. background: #cce755;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <p>p1</p>
  29. <p>p2</p>
  30. <h1 class="h1_set">h1</h1>
  31. <h1>h2</h1>
  32. <h1>h3</h1>
  33. <h1 class="h4_set">h4</h1>
  34. <h1>h5</h1>
  35. <h1>h6</h1>
  36. <p>p3</p>
  37. <ul>
  38. <li>
  39. <h1>h7</h1>
  40. <p>p4</p>
  41. </li>
  42. <li>
  43. <p>p5</p>
  44. </li>
  45. <li>
  46. <p>p6</p>
  47. </li>
  48. </ul>
  49. <h1>h8</h1>
  50. </body>
  51. </html>

结构伪类选择器

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Doemo05</title>
  8. <style>
  9. /* ul的第一个子元素 */
  10. ul li:first-child {
  11. background: #63c0f1;
  12. }
  13. /* ul的最后一个子元素 */
  14. ul li:last-child {
  15. background: #63f1aa;
  16. }
  17. /* 选中p1:定位到父元素,选择当前第一个元素 */
  18. /* :nth-child 选中当前元素的父级元素的第一个子元素,并且是相同标签才生效! */
  19. p:nth-child(1) {
  20. background: #6378f1;
  21. }
  22. /* 选择第三个 */
  23. p:nth-child(3) {
  24. background: #f1e863;
  25. }
  26. /* :nth-of-type 选中当前元素的父级元素下的相同标签的第二个子元素 */
  27. p:nth-of-type(2) {
  28. background: #f17f63;
  29. }
  30. a:hover {
  31. background: #b1d2e4;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <a href="">链接</a>
  37. <p>p1</p>
  38. <p>p2</p>
  39. <p>p3</p>
  40. <ul>
  41. <li>li1</li>
  42. <li>li2</li>
  43. <li>li3</li>
  44. </ul>
  45. </body>
  46. </html>

属性选择器

id 与 class 的结合

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Demo06</title>
  8. <style>
  9. .demo a {
  10. float: left;
  11. display: block;
  12. height: 50px;
  13. width: 50px;
  14. border-radius: 10px;
  15. background: #1296db;
  16. text-align: center;
  17. color: #ffffff;
  18. text-decoration: none; /*去除下划线*/
  19. margin-right: 5px;
  20. font: bold 20px/50px Arial; /* 20px:字体大小,50px:行高 */
  21. }
  22. /* 属性选择器 */
  23. /*
  24. 标签名[属性名=属性值] {} (支持正则)
  25. = 绝对等于这个值
  26. *= 包含这个值
  27. ^= 以这个值开头
  28. $= 以这个值结尾
  29. */
  30. /* 改变存在class属性且class含有links的元素 */
  31. a[class*="links"] {
  32. background: #12db66;
  33. }
  34. /* 改变存在id属性的元素 a[] {} */
  35. a[id] {
  36. background: #12dbca;
  37. }
  38. /* 改变存在id属性且id为last的元素 a[] {} */
  39. a[id=last] {
  40. background: #9b12db;
  41. }
  42. /* 改变href中以https开头的元素 */
  43. a[href^=https]{
  44. background: #db1281;
  45. }
  46. /*改变href中以pdf结尾的元素*/
  47. a[href$=pdf] {
  48. background-color: #db121c;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <p class="demo">
  54. <a href="abc" class="links item first" id="first">1</a>
  55. <a href="" class="links item active" target="_blank" title="test">2</a>
  56. <a href="../css/123.html" class="links item">3</a>
  57. <a href="../css/123.png" class="links item">4</a>
  58. <a href="../css/123.jpg" class="item">5</a>
  59. <a href="https://www.baidu.com" class="item">6</a>
  60. <a href="/a.xls" class="item">7</a>
  61. <a href="/abc.pdf" class="links item">8</a>
  62. <a href="abc.doc" class="links item">9</a>
  63. <a href="abcd.doc" class="links item last" id="last">10</a>
  64. </p>
  65. </body>
  66. </html>

美化网页元素

字体、文本样式

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Demo07</title>
  8. <style>
  9. body {
  10. font-family: Arial, 楷体; /* 文字字体 */
  11. }
  12. #title {
  13. font-size: 50px; /* 文字大小 */
  14. }
  15. h1 {
  16. font-weight: bold; /* 文字粗细 */
  17. /* 文字颜色 */
  18. /*
  19. 单词
  20. RGB 0~F
  21. RGBA (A-透明度[0~1])
  22. */
  23. color: aqua;
  24. font-style: oblique; /* 字体风格 */
  25. text-align: center; /* 文本排版 */
  26. }
  27. .p1 {
  28. text-indent: 2em; /* 首行缩进 1em等于一个字间隔 */
  29. }
  30. .p4 {
  31. background: #f1f1f1;
  32. height: 100px; /* 块高 */
  33. /* 行高等于块高,文本垂直居中 */
  34. line-height: 100px; /* 文本行高 */
  35. }
  36. .l1 {
  37. text-decoration: underline; /* 下划线 */
  38. }
  39. .l2 {
  40. text-decoration: line-through; /* 删除线 */
  41. }
  42. .l3 {
  43. text-decoration: overline; /* 上划线 */
  44. }
  45. span{
  46. vertical-align: middle;
  47. }
  48. img {
  49. height: 100px;
  50. width: 200px;
  51. vertical-align: middle; /* 垂直对齐 */
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <!-- span标签:重点要突出的元素,标题等(约定俗成) -->
  57. <span id="title">CSS</span>
  58. <h1>简介</h1>
  59. <p class="p1">CSS 指的是层叠样式表* (Cascading Style Sheets)</p>
  60. <p class="p2">CSS 描述了如何在屏幕、纸张或其他媒体上显示 HTML 元素</p>
  61. <p>CSS 节省了大量工作。它可以同时控制多张网页的布局</p>
  62. <p class="p4">外部样式表存储在 CSS 文件中</p>
  63. <p class="l1">1111111111</p>
  64. <p class="l2">2222222222</p>
  65. <p class="l3">3333333333</p>
  66. <p class="img_1">
  67. <img src="../../image/1920x1080.jpg" alt="">
  68. Hello World!
  69. </p>
  70. </body>
  71. </html>

超链接伪类

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Demo08</title>
  8. <style>
  9. /* 默认状态 */
  10. a {
  11. text-decoration: none;
  12. color: #000000;
  13. }
  14. /* 鼠标悬浮状态 */
  15. a:hover {
  16. color: orange;
  17. font-size: 20px;
  18. }
  19. /* 鼠标按住未释放状态 */
  20. a:active {
  21. color: green;
  22. }
  23. /* 鼠标点击后状态 */
  24. /* a:visited {
  25. color: red;
  26. } */
  27. #price {
  28. /* text-shadow: 水平偏移 垂直偏移 阴影半径 阴影颜色 */
  29. text-shadow: 5 px 5px 2px #41cffa;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <a href="#">
  35. <img src="../../image/mcgx.jpg" alt="">
  36. </a>
  37. <p>
  38. <a href="#">码出高效:Java开发手册</a>
  39. </p>
  40. <p>
  41. <a href="">作者:孤尽老师</a>
  42. </p>
  43. <p id="price">
  44. ¥99
  45. </p>
  46. </body>
  47. </html>

列表样式

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Demo09</title>
  8. <link rel="stylesheet" href="../css/Demo09.css" type="text/css">
  9. </head>
  10. <body>
  11. <div id="nav">
  12. <h2 class="title">全部商品分类</h2>
  13. <ul>
  14. <li><a href="#">图书</a>&nbsp;&nbsp;<a href="#">音像</a>&nbsp;&nbsp;<a href="#">数字商品</a></li>
  15. <li><a href="#">家用电器</a>&nbsp;&nbsp;<a href="#">手机</a>&nbsp;&nbsp;<a href="#">数码</a></li>
  16. <li><a href="#">电脑</a>&nbsp;&nbsp;<a href="#">办公</a></li>
  17. <li><a href="#">家居</a>&nbsp;&nbsp;<a href="#">家装</a>&nbsp;&nbsp;<a href="#">厨具</a></li>
  18. <li><a href="#">服饰鞋帽</a>&nbsp;&nbsp;<a href="#">个护化妆</a></li>
  19. <li><a href="#">礼品箱包</a>&nbsp;&nbsp;<a href="#">钟表</a>&nbsp;&nbsp;<a href="#">珠宝</a></li>
  20. <li><a href="#">食品饮料</a>&nbsp;&nbsp;<a href="#">保健食品</a></li>
  21. <li><a href="#">彩票</a>&nbsp;&nbsp;<a href="#">旅行</a>&nbsp;&nbsp;<a href="#">充值</a>&nbsp;&nbsp;<a href="#">票务</a></li>
  22. </ul>
  23. </div>
  24. </body>
  25. </html>
  1. /* Demo09.css */
  2. #nav {
  3. width: 300px;
  4. background: #f1f1f1;
  5. }
  6. .title {
  7. font-size: 18px;
  8. font-weight: bold;
  9. text-indent: 1em;
  10. line-height: 35px;
  11. color: #ffffff;
  12. /* 背景颜色,背景图片,图片位置,平铺方式 */
  13. background: #ffa3a3 url("../../image/down.png") 270px 8px no-repeat;
  14. }
  15. ul {
  16. background: #f1f1f1;
  17. }
  18. /*
  19. list-style:
  20. none 去掉圆点
  21. circle 空心圆
  22. decimal 数字
  23. square 正方形
  24. */
  25. ul li {
  26. line-height: 30px;
  27. list-style: none;
  28. text-indent: 1em;
  29. background-image: url("../../image/right.png");
  30. background-repeat: no-repeat;
  31. background-position: 240px 9px;
  32. }
  33. a {
  34. text-decoration: none;
  35. font-size: 14px;
  36. color: #999999;
  37. }
  38. a:hover {
  39. text-decoration: underline;
  40. color: #000000;
  41. }

背景

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Demo10</title>
  8. <style>
  9. div {
  10. width: 1000px;
  11. height: 500px;
  12. border: 2px solid #23ccff;
  13. /* 默认平铺 */
  14. background-image: url(../../image/mcgx.jpg);
  15. }
  16. /* 水平平铺 */
  17. .div1 {
  18. background-repeat: repeat-x;
  19. }
  20. /* 竖直平铺 */
  21. .div2 {
  22. background-repeat: repeat-y;
  23. }
  24. /* 不平铺 */
  25. .div3 {
  26. background-repeat: no-repeat;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div class="div1"></div>
  32. <div class="div2"></div>
  33. <div class="div3"></div>
  34. </body>
  35. </html>

渐变

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Demo11</title>
  8. <style>
  9. /* 径向渐变,圆形渐变 */
  10. body {
  11. background: linear-gradient(19deg, #21D4FD 0%, #B721FF 100%);
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. </body>
  17. </html>

盒子模型

盒子属性

margin 外边距

padding 内边距

border 边框

边框属性

边框粗细

边框样式

边框颜色

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Demo12</title>
  8. <style>
  9. h1,ul,li,a,body{
  10. /* body总有一个默认的外边距margin */
  11. margin: 0;
  12. padding: 0;
  13. text-decoration: none;
  14. }
  15. #box {
  16. width: 300px;
  17. /* border: 粗细 样式(solid 实线、dashed 虚线) 颜色 */
  18. border: 1px solid red;
  19. /* 外边距可以居中元素 margin: 0 auto (水平居中) */
  20. margin: 0 auto;
  21. }
  22. h2 {
  23. font-size: 16px;
  24. background-color: #3cbda6;
  25. line-height: 30px;
  26. }
  27. form {
  28. background: aquamarine;
  29. }
  30. div:nth-of-type(1) input {
  31. border: black solid 3px;
  32. }
  33. div:nth-of-type(2) input {
  34. border: #ad0b8c dashed 3px;
  35. }
  36. div:nth-of-type(3) input {
  37. border: #008c27 solid 3px;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <div id="box">
  43. <h2>会员登录</h2>
  44. <form action="#">
  45. <div>
  46. <span>用户名:</span>
  47. <input type="text">
  48. </div>
  49. <div>
  50. <span>密码:</span>
  51. <input type="text">
  52. </div>
  53. <div>
  54. <span>邮箱:</span>
  55. <input type="text">
  56. </div>
  57. </form>
  58. </div>
  59. </body>
  60. </html>

圆角边框、盒子阴影

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Demo13</title>
  8. <style>
  9. /* border-radius: 左上 右上 右下 左下 */
  10. /* 圆:圆角 = 半径 */
  11. .div1 {
  12. width: 100px;
  13. height: 100px;
  14. border: 10px solid #60f1b9;
  15. border-radius: 60px;
  16. }
  17. /* 盒子阴影 */
  18. .div2 {
  19. width: 100px;
  20. height: 100px;
  21. border: 10px solid #888888;
  22. box-shadow: 10px 10px 1px #d8d8d8;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div class="div1">
  28. </div>
  29. <div class="div2">
  30. </div>
  31. </body>
  32. </html>

display

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Demo14</title>
  8. <style>
  9. /*
  10. display:
  11. block 块元素
  12. inline 行内元素
  13. inline-block 是块元素,但是可以内联,在一行
  14. none 隐藏元素
  15. */
  16. div{
  17. width: 100px;
  18. height: 100px;
  19. border: 1px solid red;
  20. display: inline;
  21. }
  22. span {
  23. width: 100px;
  24. height: 100px;
  25. border: 1px solid red;
  26. display: inline-block;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div>div块级元素</div>
  32. <span>span行内元素</span>
  33. </body>
  34. </html>

float 浮动

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Demo15</title>
  8. <link rel="stylesheet" href="../css/Demo15.css">
  9. </head>
  10. <body>
  11. <div id="father">
  12. <div class="layer01"><img src="../../image/img1.jpg" alt=""></div>
  13. <div class="layer02"><img src="../../image/img2.jpg" alt=""></div>
  14. <div class="layer03"><img src="../../image/img3.jpg" alt=""></div>
  15. <div class="layer04">
  16. 浮动的盒子可以向左浮动,也可以向右浮动,直到它的外边缘碰到包含框或另一个浮动盒子为止。
  17. </div>
  18. </div>
  19. </body>
  20. </html>
  1. /* Demo15 */
  2. div {
  3. margin: 10px;
  4. padding: 5px;
  5. }
  6. #father {
  7. border: 1px #000 solid;
  8. }
  9. .layer01 {
  10. border: 1px #F00 dashed;
  11. display: block;
  12. float: right;
  13. }
  14. .layer02 {
  15. border: 1px #00F dashed;
  16. display: block;
  17. float: right;
  18. }
  19. .layer03 {
  20. border: 1px #060 dashed;
  21. display: block;
  22. float: right;
  23. }
  24. .layer04 {
  25. border: 1px #666 dashed;
  26. display: block;
  27. float: right;
  28. }

父级边框及塌陷问题

  1. clear: right; 右侧不允许有浮动元素
  2. clear: left; 左侧不允许有浮动元素
  3. clear: both; 两侧不允许有浮动元素
  4. clear: none; 允许有浮动元素

解决方案:

1、增高父级元素高度

2、增加一个空的div标签,清除浮动

  1. <body>
  2. <div id="father">
  3. <div class="layer01"><img src="../../image/img1.jpg" alt=""></div>
  4. <div class="layer02"><img src="../../image/img2.jpg" alt=""></div>
  5. <div class="layer03"><img src="../../image/img3.jpg" alt=""></div>
  6. <div class="layer04">
  7. 浮动的盒子可以向左浮动,也可以向右浮动,直到它的外边缘碰到包含框或另一个浮动盒子为止。
  8. </div>
  9. <div class="clear"></div>
  10. </div>
  11. </body>
  1. .clear {
  2. clear: both;
  3. margin: 0;
  4. padding: 0;
  5. }

3、overflow

  1. 在父级元素中增加一个 overflow: hidden
  1. #father {
  2. border: 1px #000000 solid
  3. }

4、在父类添加一个伪类:after

  1. #father {    
  2. border: 1px #000000 solid
  3. }
  4. #father:after {
  5. content: '';
  6. display: block;
  7. clearboth;
  8. }

定位

1、相对定位

2、绝对定位

3、固定定位

4、z-index

5、透明度

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Demo17</title>
  8. <style>
  9. body {
  10. padding: 20px;
  11. }
  12. div {
  13. margin: 10px;
  14. padding: 5px;
  15. font-size: 12px;
  16. line-height: 25px;
  17. }
  18. #father {
  19. border: 2px solid #666;
  20. padding: 0;
  21. height: 1000px;
  22. /* position: relative; */ /*父级元素的定位*/
  23. }
  24. /* 相对定位 */
  25. /* 相对于自己原来的位置进行偏移,原来位置会保留 */
  26. #first {
  27. background: rgb(56, 163, 70);
  28. border: 2px dashed rgb(89, 255, 111);
  29. position: relative; /* 相对定位 */
  30. top: -20px;
  31. left: 20px;
  32. }
  33. /* 绝对定位 */
  34. /*
  35. 没有父级元素定位的情况下,相对于浏览器偏移
  36. 有父级元素定位的情况下,相对于父级元素偏移
  37. 原来位置不会保留
  38. */
  39. #second {
  40. background: rgb(82, 167, 182);
  41. border: 2px dashed rgb(118, 234, 255);
  42. position: absolute; /* 绝对定位 */
  43. right: 50px;
  44. }
  45. #third {
  46. background: rgb(133, 93, 165);
  47. border: 2px dashed rgb(205, 143, 255);
  48. }
  49. /* 固定定位 */
  50. /* 相对于浏览器偏移,不保留原来位置,不随页面移动 */
  51. #fourth {
  52. width: 50px;
  53. height: 50px;
  54. background: rgb(207, 201, 114);
  55. border: 2px dashed rgb(255, 245, 98);
  56. position: fixed;
  57. right: 0;
  58. top: 0;
  59. }
  60. ul,li {
  61. padding: 0px;
  62. margin: 0px;
  63. list-style: none;
  64. }
  65. #content {
  66. width: 300px;
  67. padding: 0px;
  68. margin: 0px 0px 0px 10px;
  69. overflow: hidden;
  70. font-size: 12px;
  71. line-height: 25px;
  72. border: 1px solid #666;
  73. }
  74. #content ul {
  75. position: relative;
  76. }
  77. .tipText, .tipBg{
  78. position: absolute;
  79. width: 300px;
  80. height: 25px;
  81. top:175px;
  82. }
  83. /* z-index 图层 */
  84. .tipText {
  85. color: #FFFFFF;
  86. z-index: 999;
  87. }
  88. .tipBg {
  89. background: #8a8a8a;
  90. /* 透明度 */
  91. opacity: 0.5; /* (0~1) */
  92. }
  93. </style>
  94. </head>
  95. <body>
  96. <div id="father">
  97. <div id="first">第一个盒子</div>
  98. <div id="second">第二个盒子</div>
  99. <div id="third">第三个盒子</div>
  100. <div id="fourth">第四个盒子</div>
  101. <div id="content">
  102. <ul>
  103. <li><img src="../../image/img2.jpg" alt=""></li>
  104. <li class="tipText">文本文字</li>
  105. <li class="tipBg"></li>
  106. <li>时间:2020.01.01</li>
  107. <li>地点:XXX</li>
  108. </ul>
  109. </div>
  110. </div>
  111. </body>
  112. </html>

动画

了解,略

CSS入门笔记的更多相关文章

  1. 【前端】CSS入门笔记

    教程 CSS 指层叠样式表 (Cascading Style Sheets) CSS 语法 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 选择器通常是您需要改变样式的 HTML 元素 ...

  2. Css技术入门笔记02

    第一篇见Css入门笔记01http://blog.csdn.net/qq_32059827/article/details/51406674 4.其他选择器 4.1.关联选择器 有时在页面上会出现我们 ...

  3. [HeadFist-HTMLCSS学习笔记][第七章CSS入门:加一点样式]

    CSS入门 style元素设置CSS 基本格式 <style type="text/css"> body { background-color: #eaf3da; } ...

  4. HTML&CSS精选笔记_CSS入门

    CSS入门 CSS核心基础 CSS样式规则 选择器{属性1:属性值1; 属性2:属性值2; 属性3:属性值3;} CSS代码结构中的特点 CSS样式中的选择器严格区分大小写,属性和值不区分大小写,按照 ...

  5. 【干货】Html与CSS入门学习笔记4-8

    四.web镇之旅,连接起来 找一家托管公司如阿里云,购买域名和空间,然后将网页文件上传到购买的空间的根目录下. 1.绝对路径url url:uniform resource locators 统一资源 ...

  6. 每天成长一点---WEB前端学习入门笔记

    WEB前端学习入门笔记 从今天开始,本人就要学习WEB前端了. 经过老师的建议,说到他每天都会记录下来新的知识点,每天都是在围绕着这些问题来度过,很有必要每天抽出半个小时来写一个知识总结,及时对一天工 ...

  7. React.js入门笔记

    # React.js入门笔记 核心提示 这是本人学习react.js的第一篇入门笔记,估计也会是该系列涵盖内容最多的笔记,主要内容来自英文官方文档的快速上手部分和阮一峰博客教程.当然,还有我自己尝试的 ...

  8. JavaScript基础——JavaScript入门(笔记)

    JavaScript入门(笔记) JavaScript是一种轻量级.解释型的Web开发语言,该语言系统不是很庞杂,简单易学.由于所有现代浏览器都已嵌入JavaScript引擎,JavaScript源代 ...

  9. Python爬虫 小白[3天]入门笔记

    笔记来源 Day-0 1.如果你还不了解Python的基础语法,可以移步|>>>Python 基础 小白 [7天] 入门笔记<<<|或自行学习. 简介 1.什么是爬 ...

随机推荐

  1. 图计算 on nLive:Nebula 的图计算实践

    本文首发于 Nebula Graph Community 公众号 在 #图计算 on nLive# 直播活动中,来自 Nebula 研发团队的 nebula-plato 维护者郝彤和 nebula-a ...

  2. HashTable源码学习

    一.介绍 1.HashMap和HashTable的区别 1.相同点 二者都实现了Map接口. 底层都是哈西表 2.不同点 Hashtable继承自Dictionary类,而HashMap继承自Abst ...

  3. zabbix主动上报mysql数据库内容

    zabbix_sender命令支持主动上报数据,web服务端添加对应机器和采集器即可. 2015年刚接触zabbix时候,用的上报sqlserver脚本是select数据后插入到临时表,bcp下载到本 ...

  4. 数据库delete from和truncate删除的区别详解

    一:区别 1.delete from 后面可以直接接条件,truncate不可以 2.delete from 记录是一条条删除的,所删除的每行记录都会进入日志,而truncate一次性删除整个页,因此 ...

  5. CMake 交叉编译

    CMake 交叉编译 交叉编译就是说在平台 A (宿主机)上编译出可以在平台 B (目标机) 上运行的程序,比如在 x86 上编译 ARM 程序 要交叉编译首先要去下载目标平台的工具链,比如要编译 A ...

  6. png图片隐写

  7. [Java]Java中的自动包装

    来源:https://www.cnblogs.com/cheapcrook/archive/2012/04/25/2470478.html 自动拆装箱(AutoBoxing) 是JDK1.5中新增加的 ...

  8. Smartbi实践:制作可视化分析报表的感悟

    ​估计看到这篇文章的朋友,都是有使用过Smartbi制作数据可视化图表的.但是不是制作过程跟制作效果并没有让自己那么满意.使用过程也经常遇到一些问题解决不了?那是因为你使用的方法不对.你是否在使用Sm ...

  9. SpringBoot 实现 excel 全自由导入导出,性能强的离谱,用起来还特优雅

    一.简介 在实际的业务系统开发过程中,操作 Excel 实现数据的导入导出基本上是个非常常见的需求. 之前,我们有介绍一款非常好用的工具:EasyPoi,有读者提出在数据量大的情况下,EasyPoi ...

  10. 【C# .Net GC】Windows 系统上的大型对象堆

    原文链接:https://docs.microsoft.com/zh-cn/dotnet/standard/garbage-collection/large-object-heap NET 垃圾回收器 ...