CSS背景

CSS yana iya amfani da gaskiya mai yawa don rarraba abin, kuma yana iya amfani da keka bakwai don tsara kuma yana iya zama gaskiya da yawa.

CSS yana iya rarraba abin da ke a yau a fagen sa da HTML.

Kawun bakwai

A rani zai iya amfaniwa Tattakiya background-colorA rani kawun bakwai wa abin. Tattakiya ta amince da kowane gaskiya da ake yiwa.

Tattakiya ta kawu keka a matsayin gaskiya:

p {background-color: gray;}

Idan kwarin zai nufin kawun bakwai ya faru daga tekun abin zuwa baya, zai iya zartar da kwanan kaiwai:

p {background-color: gray; padding: 20px;}

亲自试一试

A rani kawun bakwai wa kowane abin, wanda ya haɗa da body har zuwa em da a da sauran abin da yana tsakiyar abin.

background-color yana rana kama a koyiwar, kuma a kawu da gaskiya. Gaskiya yana nufin 'gaskiya'. Idan yauwa ce yankin ba'a sananci wuri da keka, to da wa wanda ke tsakiyar keka yana zama gaskiya, kuma kawun wanda ke a baya zai kiyu zama kuma yana zama gaskiya.

keka bakwai

A rani kawun zai iwar da keka, yana soke kama a yiwa background-image 属性。background-image 属性的默认值是 none,表示背景上没有放置任何图像。

如果需要设置一个背景图像,必须为这个属性设置一个 URL 值:

body {background-image: url(/i/eg_bg_04.gif);}

大多数背景都应用到 body 元素,不过并不仅限于此。

下面例子为一个段落应用了一个背景,而不会对文档的其他部分应用背景:

p.flower {background-image: url(/i/eg_bg_03.gif);}

您甚至可以为行内元素设置背景图像,下面的例子为一个链接设置了背景图像:

a.radio {background-image: url(/i/eg_bg_07.gif);}

亲自试一试

理论上讲,甚至可以向 textareas 和 select 等替换元素的背景应用图像,不过并不是所有用户代理都能很好地处理这种情况。

另外还要补充一点,background-image 也不能继承。事实上,所有背景属性都不能继承。

背景重复

如果需要在页面上对背景图像进行平铺,可以使用 background-repeat 属性

属性值 repeat 导致图像在水平垂直方向上都平铺,就像以往背景图像的通常做法一样。repeat-x 和 repeat-y 分别导致图像只在水平或垂直方向上重复,no-repeat 则不允许图像在任何方向上平铺。

Dukansu, taswira ta background za a iya baya daga cikin gaba na cikin abin sha'awar. Ya kamancewa kamar shaidar a cikin tushen.

body
  { 
  background-image: url(/i/eg_bg_03.gif);
  background-repeat: repeat-y;
  }

亲自试一试

rarrabu background

A za a iya amfani da 属性 background-positionSake sa taswira ta background ta yi taswira a cikin background.

Tushen da yake sa taswira ta background ta yi tsaki a cikin elementin body:

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

Wannan akwai yawa da nuna sabon kiyayi don rarrabu alama background-position. Dukansu, za a iya amfani da kalmomi daban-daban: top, bottom, left, right da center. Dukansu, kalmomi daban-daban su yi kiyayi a matsayin kuma ba zai iya kuma. A cikin hakan, za a iya amfani da tsawon hanyar, kamar 100px ko 5cm, kuma a cikin hakan kuma za a iya amfani da hanyar niswari. An yi farin ciki na daban-daban na kiyayi na wuri na taswira na gidan yanar gizo.

Keywords

Image placement keywords are the easiest to understand, their function is as indicated by their name. For example, top right places the image at the top right corner of the element's padding area.

According to the specification, position keywords can appear in any order, as long as they do not exceed two keywords - one corresponding to the horizontal direction, and the other corresponding to the vertical direction.

If only one keyword appears, the other keyword is considered to be center.

So, if you want an image to appear in the middle of each paragraph, just declare as follows:

p
  { 
    background-image:url('bgimg.gif');
    background-repeat:no-repeat;
    background-position:top;
  }

The following are equivalent position keywords:

single keyword equivalent keywords
center center center
top top center or center top
bottom bottom center or center bottom
right right center or center right
left left center or center left

Percentage values

The representation of percentage values is more complex. Suppose you want to use percentage values to center an image within its element, it's easy:

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

This will result in the image being properly positioned, with its center aligned with the center of the element.In other words, the percentage values are applied to both the element and the image.That is to say, the point described as 50% 50% in the image (the center point) aligns with the point described as 50% 50% in the element (the center point).

If the image is at 0% 0%, its top left corner will be placed at the top left corner of the element's padding area. If the image position is 100% 100%, it will place the bottom right corner of the image at the bottom right corner of the right margin.

Therefore, if you want to place an image at 2/3 of the horizontal direction and 1/3 of the vertical direction, you can declare it like this:

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

If only a percentage value is provided, the value provided will be used as the horizontal value, and the vertical value will be assumed to be 50%. This is similar to the keyword.

default value of background-position is 0% 0%, which is equivalent to top left in function. This explains why the background image always starts tiling from the top left corner of the element's padding area, unless you set a different position value.

长度值

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

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

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

注意,这一点与百分数值不同,因为偏移只是从一个左上角到另一个左上角。也就是说,图像的左上角与 background-position 声明中的指定的点对齐。

背景关联

如果文档比较长,那么当文档向下滚动时,背景图像也会随之滚动。当文档滚动到超过图像的位置时,图像就会消失。

您可以通过 background-attachment 属性防止这种滚动。通过这个属性,可以声明图像相对于可视区是固定的(fixed),因此不会受到滚动的影响:

body 
  {
  background-image:url(/i/eg_bg_02.gif);
  background-repeat:no-repeat;
  background-attachment:fixed
  }

亲自试一试

background-attachment 属性的默认值是 scroll,也就是说,在默认的情况下,背景会随文档滚动。

CSS 背景实例

设置背景颜色
本例演示如何为元素设置背景颜色。
设置文本的背景颜色
本例颜色如何设置部分文本的背景颜色。
将图像设置为背景
本例演示如何将图像设置为背景。
将图像设置为背景 2
本例演示如何为多个元素同时设置背景图像。
如何重复背景图像
本例演示如何重复背景图像。
如何在垂直方向重复背景图像
本例演示如何垂直地重复背景图像。
如何在水平方向重复背景图像
本例演示如何水平地重复背景图像。
如何仅显示一次背景图像
本例演示如何仅显示一次背景图像。
如何放置背景图像
本例演示如何在页面上放置背景图像。
如何使用%来定位背景图像
本例演示如何使用百分比来在页面上定位背景图像。
如何使用像素来定位背景图像
本例演示如何使用像素来在页面上定位背景图像。
如何设置固定的背景图像
本例演示如何设置固定的背景图像。图像不会随着页面的其他部分滚动。
duba dakiya da ke yin aroku
This example demonstrates how to use shorthand properties to set all background properties in one declaration.

CSS Background Properties

Attribute Description
background A shorthand property that sets the background properties in one declaration.
background-attachment Whether the background image is fixed or scrolls with the rest of the page.
background-color Set the background color of the element.
background-image Set the image as the background.
background-position Set the starting position of the background image.
background-repeat Set whether the background image is repeated and how.