search
HomeWeb Front-endHTML TutorialCSS 背景_html/css_WEB-ITnose

概览

CSS 背景属性用于定义HTML元素的背景。

相关的属性

  • background-color: 设置背景颜色

  • background-image: 设置背景图片

  • background-repeat: 设置背景图片是否重复及如何重复

  • background-position: 设置背景图像的位置

  • background-attachment: 背景图像是否固定或者随着页面的其余部分滚动

  • background: 简写属性,作用是将背景属性设置在一个声明中

元素背景的范围

背景会填充元素的内容、内边距和边框区域,扩展到元素边框的外边界(但不包括外边距)。如果边框有透明部分(如虚线边框),会透过这些透明部分显示出背景色。

浏览器支持

所有主要浏览器都支持背景属性。

注意: IE7和更早的版本不支持"继承"的值(inherit)。 IE8需要定义!DOCTYPE。 IE9支持"继承"。

背景颜色

background-color 属性用来定义元素的背景颜色。

body {background-color: #b0c4de;}

CSS中,颜色值通常以以下方式定义:

  • 十六进制 - 如:#ff0000

  • RGB - 如:rgb(255,0,0)

  • 颜色名称 - 如:red

  • 默认 - transparent,透明之意。如果一个元素没有指定背景色,那么背景就是透明的。

  • inherit 从父元素继承背景颜色,这个在IE上存在兼容问题。

背景图像

background-image 属性用来定义元素的背景图像。

默认地,背景图像位于元素的左上角,并在水平和垂直方向上重复。

body {background-image: url('paper.gif');}

提示:背景颜色和背景图像是可以共存的,请设置一种可用的背景颜色,这样的话,假如背景图像不可用,页面也可获得良好的视觉效果。

body {background-image: url('paper.gif');background-color:#fff;}    

属性值:

  • none - 默认无背景图像

  • url('image path') - 图像的可访问路径,可以使用网络地址,相对路径地址,绝对路径地址

  • inherit - 从父元素继承

背景重复

如果需要对背景图像平铺,可以使用background-repeat属性。

body {    background-image: url('gradient2.png');    background-repeat: repeat-x;}

属性值:

  • repeat 导致图像在水平垂直方向上都平铺,默认。

  • repeat-x 和 repeat-y 分别使背景图像只在水平或垂直方向上重复。

  • no-repeat 则不允许图像在任何方向上平铺。

  • inherit 从父元素继承。

背景定位

background-position 属性用来控制背景图片的位置,一般配合 background-repeat: no-repeat; 使用。

body {    background-image: url('img_tree.png');    background-repeat: no-repeat;    background-position: right top;}

为 background-position 属性提供值有很多方法。首先,可以使用一些关键字:top、bottom、left、right 和 center。通常,这些关键字会成对出现,不过也不总是这样。还可以使用长度值,如 100px,最后也可以使用百分数值。

关键词

图像放置关键字最容易理解,其作用如其名称所表明的。例如,top right 使图像放置在元素内边距区的右上角。

根据规范,位置关键字可以按任何顺序出现,只要保证不超过两个关键字,一个对应水平方向,另一个对应垂直方向,top right 和 right top 是等效的。

如果只出现一个关键字,则认为另一个关键字是 center。

百分比

百分数值的表现方式更为复杂。假设你希望用百分数值将图像在其元素中居中:

body {     background-image: url('/eg_bg_03.gif');    background-repeat: no-repeat;    background-position: 50% 50%;}

如果图像位于 0% 0%,其左上角将放在元素内边距区的左上角。如果图像位置是 100% 100%,会使图像的右下角放在右边距的右下角。

把一个图像放在水平方向 2/3、垂直方向 1/3 处:

body {     background-image: url('/eg_bg_03.gif');    background-repeat: no-repeat;    background-position: 66% 33%;}

如果只提供一个百分数值,所提供的这个值将用作水平值,垂直值将假设为 50%。

长度值

长度值解释的是元素内边距区左上角的偏移。偏移点是图像的左上角。

比如,如果设置值为 50px 100px,图像的左上角将在元素内边距区左上角向右 50 像素、向下 100 像素的位置上。

背景固定

background-attachment 属性设置背景图像是否固定或随页面的其余部分滚动。

body {    background-image:url('image.png');    background-repeat:no-repeat;    background-attachment:fixed;}

属性值:

  • scroll 默认。背景图像会随着页面其余部分的滚动而移动。

  • fixed 当页面的其余部分滚动时,背景图像不会移动。

  • inherit 从父元素继承。

简写属性

background 属性在一个声明中设置所有背景属性。

body {     background: #00FF00 url('bgimage.gif') no-repeat fixed top;}

如果不设置其中的某个值,也不会出问题,比如 background:#ff0000 url('smiley.gif'); 也是允许的。

通常建议使用这个属性,而不是分别使用单个属性,因为这个属性在较老的浏览器中能够得到更好的支持,而且需要键入的字母也更少。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

How to implement multi-project carousel in Bootstrap 4?How to implement multi-project carousel in Bootstrap 4?Apr 30, 2025 pm 03:24 PM

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How does deepseek official website achieve the effect of penetrating mouse scroll event?How does deepseek official website achieve the effect of penetrating mouse scroll event?Apr 30, 2025 pm 03:21 PM

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

How to modify the playback control style of HTML videoHow to modify the playback control style of HTML videoApr 30, 2025 pm 03:18 PM

The default playback control style of HTML video cannot be modified directly through CSS. 1. Create custom controls using JavaScript. 2. Beautify these controls through CSS. 3. Consider compatibility, user experience and performance, using libraries such as Video.js or Plyr can simplify the process.

What problems will be caused by using native select on your phone?What problems will be caused by using native select on your phone?Apr 30, 2025 pm 03:15 PM

Potential problems with using native select on mobile phones When developing mobile applications, we often encounter the need for selecting boxes. Normally, developers...

What are the disadvantages of using native select on your phone?What are the disadvantages of using native select on your phone?Apr 30, 2025 pm 03:12 PM

What are the disadvantages of using native select on your phone? When developing applications on mobile devices, it is very important to choose the right UI components. Many developers...

How to optimize collision handling of third-person roaming in a room using Three.js and Octree?How to optimize collision handling of third-person roaming in a room using Three.js and Octree?Apr 30, 2025 pm 03:09 PM

Use Three.js and Octree to optimize collision handling of third-person roaming in the room. Use Octree in Three.js to implement third-person roaming in the room and add collisions...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.