当前位置:Gxlcms > html代码 > 实例详解Html5的背景应用

实例详解Html5的背景应用

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

这篇文章主要介绍了浅谈Html5的背景属性,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

1、背景属性复习:
background-image
background-color
background-repeat
background-position
background-attachment

2、新增属性:
background-size:
background-size:x y; // 水平 垂直方向的尺寸,像素/百分比/auto/⋯
background-size:cover; //保持宽高比不变,保证占满盒子,但不保证能看到全部
background-size:contain; //保持宽高比不变,保证看清全图,但可能占不满盒子

多背景:
background-image:url(1.jpg),url(2.jpg);


background-origin 背景区域定位
border-box: 从border区域开始显示背景
padding-box: 从padding区域开始显示背景
content-box: 从content内容区域开始显示背景

background-clip 背景绘制区域
border-box: 从border区域开始绘制背景
padding-box: 从padding区域开始绘制背景
content-box: 从content内容区域开始显示背景

3、背景练习代码部分:

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>your title name</title>
  5. <meta charset="utf-8">
  6. <meta name="Author" content="Wilson Xu">
  7. <style type="text/css">
  8. *{margin: 0;padding: 0;font-family: "Microsoft yahei";}
  9. a{text-decoration: none;}
  10. a img{display: block;border: none;}
  11. li{list-style: none;}
  12. .container{
  13. width: 1200px;
  14. padding: 20px;
  15. margin: 10px auto;
  16. border: 1px dashed #ccc;
  17. }
  18. .container h4{padding-bottom: 5px;}
  19. .container ul{
  20. width: 1200px;
  21. overflow: hidden;
  22. }
  23. .container ul li{
  24. float: left;
  25. width: 331px;
  26. padding: 20px;
  27. height: 240px;
  28. margin-right: 10px;
  29. border: 10px solid rgba(10,10,10,.3);
  30. background: url('images/1.jpg') no-repeat;
  31. background-size: 371px auto;
  32. }
  33. .container ul li:last-child{margin-right: 0;}
  34. .container ul.origin li:nth-child(1){
  35. background-origin: border-box;
  36. }
  37. .container ul.origin li:nth-child(2){
  38. background-origin: padding-box;
  39. }
  40. .container ul.origin li:nth-child(3){
  41. background-origin: content-box;
  42. }
  43. .container ul.clip li:nth-child(1){
  44. background-clip: border-box;
  45. }
  46. .container ul.clip li:nth-child(2){
  47. background-clip: padding-box;
  48. }
  49. .container ul.clip li:nth-child(3){
  50. background-clip: content-box;
  51. }
  52. section .pic{
  53. width: 600px;
  54. height: 400px;
  55. margin: 20px auto;
  56. border: 1px dashed #ddd;
  57. background: url('images/3.jpg') no-repeat center center/auto 200px, url('images/2.jpg') no-repeat center center/auto 300px, url('images/1.jpg') no-repeat center center/auto 400px;
  58. }
  59. section p{
  60. font-size: 14px;
  61. color: #f01010;
  62. }
  63. </style>
  64. </head>
  65. <body>
  66. <p class="container">
  67. <section>
  68. <h4>1、background-origin: border-box | padding-box | content-box</h4>
  69. <ul class="origin">
  70. <li></li>
  71. <li></li>
  72. <li></li>
  73. </ul>
  74. </section>
  75. <section>
  76. <h4 style="margin-top: 20px;border-top: 1px dashed #ccc;">2、background-clip: border-box | padding-box | content-box</h4>
  77. <ul class="clip">
  78. <li></li>
  79. <li></li>
  80. <li></li>
  81. </ul>
  82. </section>
  83. <section>
  84. <h4 style="margin-top: 20px;border-top: 1px dashed #ccc;">3、多背景:background: url('images/3.jpg') no-repeat center center/auto 200px, url('images/2.jpg') no-repeat center center/auto 300px, url('images/1.jpg') no-repeat center center/auto 400px;
  85. </h4>
  86. <p class="pic"></p>
  87. <p>注释:复合写background-size的时候,一定要用/与其他值隔开。</p>
  88. </section>
  89. </p>
  90. </body>
  91. </html>

5、背景练习preview:



6、渐变:

线性渐变:linear-gradient(方位(left/left top/60deg),起始颜色 | 百分比30%,终止颜色);使用时加内核前缀eg:-webkit-linear-gradient,IE9不支持
径向渐变:radial-gradient(中心点位置,扩散程度,颜色域 | 百分比);由中心向四周发散,eg:-webkit-radial-gradient(50px 50px,起始颜色,终止颜色);-webkit-radial-gradient(center,起始颜色,终止颜色);
IE低版本兼容:filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff',endColorstr='#ff0000',GradientType='1');1表示从左到右,0是从上到下,并且颜色值只能是6位哈希值

7、渐变练习代码部分:

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>your title name</title>
  5. <meta charset="utf-8">
  6. <meta name="Author" content="Wilson Xu">
  7. <style type="text/css">
  8. *{margin: 0;padding: 0;font-family: "Microsoft yahei";}
  9. a{text-decoration: none;}
  10. a img{display: block;border: none;}
  11. li{list-style: none;}
  12. .container{
  13. width: 1200px;
  14. padding: 20px;
  15. margin: 20px auto;
  16. border: 1px dashed #ccc;
  17. }
  18. .container h4{padding-bottom: 5px;}
  19. .container ul{
  20. width: 1200px;
  21. overflow: hidden;
  22. }
  23. .container ul.linear li,
  24. .container ul.filter li{
  25. width: 600px;
  26. height: 40px;
  27. margin: 10px 0;
  28. }
  29. .container ul.linear li:first-child{
  30. background: -webkit-linear-gradient(60deg,#fff 10%, #f00 30%, #0f0 50%, #00f 70%, #000);
  31. }
  32. .container ul.linear li:last-child{
  33. background: -webkit-linear-gradient(left top, rgba(122,156,233,.6) 30%, rgb(255,12,222) 60%, green 80%, #fff);
  34. }
  35. .container ul.filter li:first-child{
  36. background: filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#abcdef',endColorstr='#f44add',GradientType='0');
  37. }
  38. .container ul.filter li:last-child{
  39. background: filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff',endColorstr='#000000',GradientType='1');
  40. }
  41. .container ul.radial li{
  42. width: 200px;
  43. height: 200px;
  44. margin-right: 20px;
  45. float: left;
  46. border-radius: 100%;
  47. }
  48. .container ul.radial li:nth-child(1){
  49. background: -webkit-radial-gradient(center, #fff, #000);
  50. }
  51. .container ul.radial li:nth-child(2){
  52. background: -webkit-radial-gradient(left 50px, #fff, #000);
  53. }
  54. .container ul.radial li:nth-child(3){
  55. background: -webkit-radial-gradient(50px 100px,100px 100px, #fff 80%, #000);
  56. }
  57. .container ul.radial li:nth-child(4){
  58. background: -webkit-radial-gradient(left, #fff 20%, #f00 40%, #0f0 60%, #00f 80%, #000);
  59. }
  60. </style>
  61. </head>
  62. <body>
  63. <p class="container">
  64. <section>
  65. <h4>1、线性渐变:-webkit-linear-gradient(方位,颜色域 | 范围百分比)</h4>
  66. <ul class="linear">
  67. <li></li>
  68. <li></li>
  69. </ul>
  70. </section>
  71. <section>
  72. <h4>2、线性渐变-兼容IE低版本:filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='6位哈希值',endColorstr='6位哈希值',GradientType='1/0');</h4>
  73. <ul class="filter">
  74. <li></li>
  75. <li></li>
  76. </ul>
  77. </section>
  78. <section>
  79. <h4>3、径向渐变:radial-gradient(中心点位置,扩散程度,颜色域 | 百分比);</h4>
  80. <ul class="radial">
  81. <li></li>
  82. <li></li>
  83. <li></li>
  84. <li></li>
  85. </ul>
  86. </section>
  87. </p>
  88. </body>
  89. </html>

8、渐变preview:

以上就是实例详解Html5的背景应用的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行