CSS背景

落日映苍穹つ 2021-12-10 23:51 557阅读 0赞

1.设置页面的背景颜色

  1. body{
  2. margin:0px;
  3. background-color:lightskyblue;
  4. }
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="UTF-8">
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  11. <title>我的CSS</title>
  12. </head>
  13. <body>
  14. <h1>我的CSS web页!</h1>
  15. <p>Hello World!</p>
  16. </body>
  17. </html>

2.设置不同元素的背景颜色

  1. 1 h1{
  2. 2 background-color:royalblue;
  3. 3 }
  4. 4 div{
  5. 5 background-color:silver;
  6. 6 }
  7. 7 p{
  8. 8 background-color:honeydew;
  9. 9 }
  10. <!DOCTYPE html>
  11. <html lang="en">
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  16. <title>我的CSS</title>
  17. </head>
  18. <body>
  19. <h1>CSS background-color实例!</h1>
  20. <div>
  21. 该本文插入在div元素中。
  22. <p class="green">该段落有自己的背景颜色。</p>
  23. 我们任然在同一个div中。
  24. </div>
  25. </body>
  26. </html>

3.设置一个图像最为页面的背景

  1. div {
  2. margin: 0px;
  3. width: 200px;
  4. height: 200px;
  5. background-image: url('fun.jpg');
  6. }
  7. div h1 {
  8. text-align: center;
  9. }
  10. <!DOCTYPE html>
  11. <html lang="en">
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  16. <title>我的CSS</title>
  17. </head>
  18. <body>
  19. <div>
  20. <h1>Hello World!</h1>
  21. </div>
  22. </body>
  23. </html>

4.如何在水平方向重复背景图像

  1. div {
  2. background-image: url('fun.jpg');
  3. background-repeat: no-repeat;
  4. }
  5. <body>
  6. <div>
  7. <h1>Hello World!</h1>
  8. </div>
  9. </body>

5.如何定位背景图像

  1. div {
  2. background-image: url('fun.jpg');
  3. background-repeat: no-repeat;
  4. margin-right: 200px;
  5. }
  6. <body>
  7. <div>
  8. <h1>Hello World!</h1>
  9. </div>
  10. </body>

6.一个固定的背景图片(这个图片不会随着页面的其余部分滚动)

  1. div {
  2. background-image: url("fun.jpg");
  3. background-repeat: no-repeat;
  4. background-attachment: fixed;
  5. right: 2000px;
  6. }
  7. <body>
  8. <div>
  9. <p>背景图片是固定的。尝试向下滚动页面</p>
  10. <p>背景图片是固定的。尝试向下滚动页面</p>
  11. <p>背景图片是固定的。尝试向下滚动页面</p>
  12. <p>背景图片是固定的。尝试向下滚动页面</p>
  13. <p>背景图片是固定的。尝试向下滚动页面</p>
  14. <p>背景图片是固定的。尝试向下滚动页面</p>
  15. <p>背景图片是固定的。尝试向下滚动页面</p>
  16. <p>背景图片是固定的。尝试向下滚动页面</p>
  17. <p>背景图片是固定的。尝试向下滚动页面</p>
  18. <p>背景图片是固定的。尝试向下滚动页面</p>
  19. </div>
  20. </body>

7.声明背景属性

  1. body{
  2. background: url("fun.jpg") no-repeat right top;
  3. margin-right: 200px;
  4. }
  5. <body>
  6. <h1>Hello World!</h1>
  7. <p>背景图片只显示一次但它位置离文本比较远</p>
  8. <p>在这个例子中我们添加了一个右边距,所以背景图像</p>
  9. </body>

8.高级的背景例子

  1. <body>
  2. <div class="contains">
  3. <div class="content">
  4. <h1>Hello World!</h1>
  5. <p>This example contains some advanced CSS methods you may not have learned yet. But, we will explain these methods in a later chapter in the tutorial.</p>
  6. </div>
  7. </div>
  8. </body>
  9. body {
  10. margin-left: 200px;
  11. background-image: url("fun.jpg");
  12. }
  13. div.contains {
  14. text-align: center;
  15. }
  16. div.contains div.content {
  17. border: 1px solide skyblue;
  18. margin-right: auto;
  19. margin-top: auto;
  20. width: 75%;
  21. text-align: left;
  22. background-color: darkorange;
  23. padding: 8px;
  24. }

转载于:https://www.cnblogs.com/Tony98/p/10958001.html

发表评论

表情:
评论列表 (有 0 条评论,557人围观)

还没有评论,来说两句吧...

相关阅读

    相关 css设置背景

    css2设置背景相关属性有如下: background : 简写属性,作用是将背景属性设置在一个声明中。 background-color : 设置元素的背景颜色。