(CSS入门)css添加+背景图像+背景重复

布满荆棘的人生 2022-09-03 02:29 363阅读 0赞

css添加

当浏览器读到样式表时,它将根据样式表中的信息来格式化 HTML 文档。

三种使用 CSS 的方法

有三种插入样式表的方法:

  • 外部css
  • 内部css
  • 行内css

外部css

每张 HTML 页面必须在 head 部分的 元素内包含对外部样式表文件的引用。
eg:
外部样式在 HTML 页面 部分内的 元素中进行定义:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link rel="stylesheet" type="text/css" href="mystyle.css">
  5. </head>
  6. <body>
  7. <h1>This is a heading</h1>
  8. <p>This is a paragraph.</p>
  9. </body>
  10. </html>

外部样式表可以在任何文本编辑器中编写,并且必须以 .css 扩展名保存。
外部 .css 文件不应包含任何 HTML 标签。
“mystyle.css” 是这样的:

  1. body {
  2. background-color: lightblue;
  3. }
  4. h1 {
  5. color: navy;
  6. margin-left: 20px;
  7. }

请勿在属性值和单位之间添加空格

内部 CSS

如果一张 HTML 页面拥有唯一的样式,那么可以使用内部样式表。
内部样式是在 head 部分的 <style> 元素中进行定义。
eg:
内部样式在 HTML 页面的 <head> 部分内的 <style> 元素中进行定义:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. background-color: linen;
  7. }
  8. h1 {
  9. color: maroon;
  10. margin-left: 40px;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h1>This is a heading</h1>
  16. <p>This is a paragraph.</p>
  17. </body>
  18. </html>

行内 CSS

行内样式(也称内联样式)可用于为单个元素应用唯一的样式。
行内样式在相关元素的 “style” 属性中定义:

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h1 style="color:blue;text-align:center;">This is a heading</h1>
  5. <p style="color:red;">This is a paragraph.</p>
  6. </body>
  7. </html>

层叠顺序

页面中的所有样式将按照以下规则“层叠”为新的“虚拟”样式表,其中第一优先级最高:
1.行内样式(在 HTML 元素中)
2.外部和内部样式表(在 head 部分)
3.浏览器默认样式

css 注释

位于 <style> 元素内的 CSS 注释,以 /* 开始,以 */ 结束

css背景图像

background-image 属性指定用作元素背景的图像。
页面的背景图像可以像这样设置:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. background-image: url("/i/paper.jpg");
  7. }
  8. </style>
  9. </head>
  10. <body>
  11. <h1>Hello World!</h1>
  12. <p>此页面以图像为背景!</p>
  13. </body>
  14. </html>

在这里插入图片描述

还可以为特定元素设置背景图像,例如

元素:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. p {
  6. background-image: url("/i/paper.jpg");
  7. }
  8. </style>
  9. </head>
  10. <body>
  11. <h1>Hello World!</h1>
  12. <p>本段以一幅图像作为背景!</p>
  13. </body>
  14. </html>

在这里插入图片描述

CSS 背景重复

默认情况下,background-image 属性在水平和垂直方向上都重复图像。

CSS background-repeat

  1. !DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. background-image: url("/i/css/gradient_bg.png");
  7. }
  8. </style>
  9. </head>
  10. <body>
  11. <h1>Hello World!</h1>
  12. <p>奇怪的背景图片...</p>
  13. </body>
  14. </html>

在这里插入图片描述如果上面的图像仅在水平方向重复 (background-repeat: repeat-x;),则背景看起来会更好:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. background-image: url("/i/css/gradient_bg.png");
  7. background-repeat: repeat-x;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <h1>Hello World!</h1>
  13. <p>在这里,背景图像仅在水平方向上重复!</p>
  14. </body>
  15. </html>

在这里插入图片描述
background-repeat 属性还可指定只显示一次背景图像:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. background-image: url("/i/photo/tree.png");
  7. background-repeat: no-repeat;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <h1>Hello World!</h1>
  13. <p>W3School 背景图像实例。</p>
  14. <p>这幅背景图像仅显示一次,但它会打扰读者!</p>
  15. </body>
  16. </html>

在这里插入图片描述

CSS background-position

background-position 属性用于指定背景图像的位置。

发表评论

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

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

相关阅读