Home  >  Article  >  Web Front-end  >  Introduction to properties related to borders in background properties in css3

Introduction to properties related to borders in background properties in css3

不言
不言Original
2018-08-24 10:53:521998browse

This article brings you an introduction to the properties related to the border in the background properties of CSS3. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1 New attributes related to background

background-clip: Specify the display range of the background.

background-origin: Specify the starting point when drawing the background image.

background-size: Specify the size of the image in the background.

background-break: Specifies the looping method when the background image of an inline element is tiled.

Note:

Firefox browser: supports three other attributes besides the background-size attribute. You need to add "-moz-" text before the attribute. . When using background-break, you need to write "-moz-background-inline-policy".

Safari, Google Chorme, Opera: support three other attributes except background-break. You need to add ""-webkit-" text before the attribute.

2 Specify the display range of the background-background-clip attribute

(1) The display range of the background

CSS2: The display range of the background refers to The range within the internal padding, does not include the border .

CSS2.1 and even CSS3: The display range of the background refers to the range including the border.

(2) Attribute value

border: The background range includes the border area.

padding: The background range does not include the border area.

div.div1{
          -moz-background-clip:border;
         -webkit-background-clip:border;
}
div.div2{
          -moz-background-clip:padding;
         -webkit-background-clip:padding;
}

3 Specify the starting point when drawing the background image - background-origin attribute

(1) When drawing the background image, The default is from The content padding area is drawn starting from the upper left corner.

(2) Value

border: start drawing from the upper left corner of the border

padding: start drawing from the upper left corner of the content padding area

content: Start drawing from the upper left corner of the content

(3) Different browsers

Firefox: "-moz-background-origin";

Safari, Chorme: "-webkit- background-origin”

(4) Although when the background-clip attribute is specified as padding, the image between the midpoints of the border dotted line will not be displayed, but it can still be displayed by setting the background-origin attribute. Specify the border method to start drawing from the upper left corner of the border.

4 Specify the size of the image in the background - background-size attribute

(1) Usage method

background-size: the width and height of the image element ;

(2) Browser support:

So far: Safari3, Chrome8, Firefox4, and Opera10 browsers support this attribute.

(3) If you want to maintain the aspect ratio of the image, you can set another parameter to auto

# when setting the width and height of the image. ##(4)

Specify only one parameter

The browser will treat this value as the width value and auto as the height value.

(5) Use the value of

percentage as a parameter

The browser treats the specified percentage as the percentage of the image size divided by the size of the entire border area To process

(6)

contain keyword as parameter

This will make the original image larger or larger automatically while

maintaining the aspect ratio Shrink so that the width or height of the original image is completely equal to the width or height of the element (ensure that the image can be completely displayed in the element)

(7)

cover keywordAs a parameter

This will make the original image

maintain the aspect ratio Automatically scale the background image to fill the interior of the element, if the length and width of the element If the ratio is inconsistent with the length-to-width ratio of the original image, the excess part will be cut.

5 New options for tiled background images----space and round

(1) space

in the horizontal direction Or when tiling the background image vertically, it does not cut off the part of the image that exceeds the background, nor does it adjust the size of the background image, but automatically adjusts the

spacing between images.

(2) round

When tiling the background image horizontally or vertically, it does not cut off the part of the image that exceeds the background, but

automatically adjusts the background image size of.

(3) Browser support

So far: IE9 or above, Chrome, and Opera browsers support this attribute.

div{
     background-repeat:space;//round
}

6 Display multiple background images in one element

In CSS3, you can display multiple background images in one element, and you can also combine multiple background images. Overlay displays make it easier to adjust the material used in the background image.

div{
     background-image:url(flowe-red.png),url(flower-green.png),url(sky.png);
     background-repeat:no-repeat,repeat-x,no-repeat;
     background-position:3% 98%,85%,center center,top;
}

(1)使用background-image属性来指定图像文件的时候,指定的时候是按在浏览器中显示时图像叠放的顺序从上往下指定的,第一个图像文件是放在最上面的,最后指定的文件是放在最下面的。

(2)通过多个background-repeat属性与background-position属性的指定,可以单独指定背景图像中某个图像文件的平铺方式与放置位置。

(3)允许被多重指定并配合多个图像文件一起利用的属性如下:

background-image、background-repeat、background-position、background-size、background-clip、background-origin。

7 使用渐变色背景

CSS3中,支持对于元素指定渐变色背景。所谓渐变是指从一种颜色慢慢过渡到另一种颜色。渐变分为线性渐变与放射性渐变。

绘制线性渐变

background:linear-gradient(to bottom,orange,black)

使用linear-gradient函数实现线性渐变,函数中使用三个参数。

(1)第一个参数值:

to bottom:指定从上往下的渐变,默认渐变起点为元素顶端,渐变重点为元素底端。

to bottom right:指定从左上往右下的渐变,默认渐变起点为元素左上角,渐变终点为元素右下角。

to right:指定从左往右的渐变,默认渐变起点为元素左边,渐变重点为元素右边。

to up right:指定从左下往右上的渐变,默认渐变起点为元素左下角,渐变终点为元素右上角。

to up:指定从下往上的渐变,默认渐变起点为元素底端,渐变终点为元素顶端。

to up left:指定从右下往左上的渐变,默认渐变起点为元素右下角,渐变终点为元素左上角。

to left:指定从右往左的渐变,默认渐变起点为元素右边,渐变终点为原左边。

to bottom left:指定从右上往左下的渐变,默认渐变起点为元素右上角,渐变终点为元素左下角。

可指定一个角度,用于指定渐变线的旋转角度

(2)第二个参数值代表渐变的起点色,第三个参数代表渐变的终点色。

div{
    width:300px;
    height:300px;
    background:linear-gradient(to bottom,orange,black)//从顶端到底端、从桔色到黑色的线性渐变
}

(3)将第一个参数指定为一个角度,其作用为修改渐变线的角度

background:linear-gradient(30deg,orange,black);

如果角度为0,则渐变线的方向为从下往上,当角度增加时渐变线顺时针方向旋转。

(4)可以在起点色或终点色后边指定离渐变色起点或渐变色终点的偏离位置(不指定时默认值分别为0%及100%)

background:linear-gradient(to bottom,orange 20%,black 70%);

表示从p元素的顶端往下20%,即离元素顶端300(元素高度)*20%=60像素处开始渐变,一直渐变到离元素底端30%(100%-70%),即离元素底端300*30%=90像素处停止渐变。

(5)可以添加多个渐变的中间点

background:linear-gradient(to bottom,orange 0%,red 25%,yellow 50%,green 75%,black 100%);

绘制放射性渐变

background:radial-gradient(orange,black);

使用radial-gradient函数实现放射性渐变,函数使用两个参数,分别为渐变起点色与渐变终点色。

div{
     background:radial-gradient(orange,black);//从中心向外扩散,从桔色到黑色的放射性渐变;
}

(1)可以通过circle关键字ellipse关键字指定绘制渐变呈圆形向外扩散方式还是呈椭圆形向外扩散方式。

div{
     background:radial-gradient(circle,orange,black);//指定圆形渐变方式
}

(2)可以通过at关键字指定渐变起点位置

background:radial-gradient(
at 
left top,orange,black);

可指定如下选项值:

center center:从元素中心点向外扩散(默认选项值)

left top:从元素左上角向外扩散

center top:从元素顶部中央向外扩散

right top:从元素右上角向外扩散

right center:从元素右端中央向外扩散

right bottom:从元素右下角向外扩散

center bottom:从元素底部中央向外扩散

left bottom:从元素左上角向外扩散

坐标值:例如(30,50),从指定坐标点处向外扩散

background:radial-gradient(circle at center top,orange,black);
background:radial-gradient( at 130px 50px,orange,black);

(3)指定渐变尺寸

closest-side:可渐变到离渐变起点最的一条

farthest-side:可渐变到离渐变起点最的一条

closest-corner:可渐变到离渐变起点最的一个

farthest-corner:可渐变到离渐变起点最的一个

background:radial-gradient( ellipse closest-side at 130px 50px,orange,black);

(4)可通过对圆形渐变指定半径的方法指定渐变尺寸

background:radial-gradient( circle 95px at 130px 50px,orange,black);

(5)可通过对椭圆形渐变指定横向半径及纵向半径的方法指定渐变尺寸

background:radial-gradient( ellipse 
235px 95px at 130px 50px,orange,black);

(6)可通过添加多个渐变色并指定偏离百分比的方法在渐变起点与渐变终点中添加多个渐变中间点

background:radial-gradient( circle 130px at130px 50px,orange 0%,red 25%,yellow 50%,green 75%,black);

8 圆角边框的绘制

到目前为止:IE、Safari、Firefox、Opera、Chorme浏览器都支持这种绘制圆角边框的样式。

border-radius属性

在CSS3中,使用该属性指定好圆角的半径就可以绘制圆角边框了。

div{
   border:soild 5px blue;
    border-radius:20px;
}

(1)指定两个半径

在浏览器中,将第一个半径作为边框左上角与右下角圆半径来绘制,第二个半径作为边框右上角与左下角的圆半径来绘制

border-radius:40px 20px;

(2)不显示边框的时候

在CSS3中,如果使用了border-radius属性但是把边框设定为不显示的时候,浏览器将把背景的4个角绘制为圆角

div{
border:none;
border-radius:20px;
}

(3)修改边框种类的时候

使用border-radius属性后,不管边框是什么种类,都会将边框沿着圆角曲线进行绘制。

div{
border:dashed 5px blue;
border-radius:20px;
}

(4)绘制四个角不同半径的圆角边框

border-top-left-radius属性:指定左上角半径

border-top-right-radius属性:指定右上角半径

border-bottom-right-radius属性:指定右下角半径

border-bottom-left-radius属性:指定左下角半径

div{
border:dashed 5px blue;
border-top-left-radius:10px;//指定左上角半径
border-top-right-radius:20px;//指定右上角半径
border-bottom-right-radius:30px;//指定右下角半径
border-bottom-left-radius:40px;//指定左下角半径
}

9 使用图像边框

border-image属性

CSS3中增加了一个border-image属性,可以让元素的长度或宽度处于随时变化状态的边框统一使用一个图像文件进行绘制。。使用border-image属性,会让浏览器在显示图像边框时,自动将所使用到的图像分割为9部分进行处理,这样就不需要页面制作者另外进行人工处理了。另外,页面中也不需要因此而使用较多的元素。

(1)浏览器支持

到目前为止:IE、Safari、Firefox、Opera、Chorme浏览器都支持border-image属性的使用。

(2)使用方法

border-image:url(图像文件的路径) A B C D(该属性中至少必须指定5个参数)

A、B、C、 D:表示当浏览器自动把边框所使用到的图像进行分割时的上边距、右边距、下边距以及左边距。

div{
border:solid 5px;
border-image:url(borderimage.png) 18 18 18 18;
width:300px;
}

(3)使用border-image属性来指定边框宽度

CSS3中,除了可以使用border属性或border-width属性来指定边框的宽度外,还可以使用border-image属性来指定边框宽度。

border-image:url(图像文件的路径) A B C D/border-width

div{
border:solid;
border-image:url(borderimage.png) 18 18 18 18/10px;(指定为相同宽度)
//border-image:url(borderimage.png) 10/5px 10px 15px 20px;(四条边的边框指定为不同的宽度)
width:300px;
}

注意:在CSS3中,如果4个参数完全相同,可以只写一个参数,将其他三个参数省略。

(4)指定4条边中图像的显示方式

可以在border-image属性中指定元素4条边中的图像是以拉伸的方式进行显示,还是以平铺的方式进行显示,

border-image:url(图像文件的路径) A B C D/border-width topbottom leftright

      topbottom:表示元素的上下两条边中图像的显示方式

      leftright:表示元素的左右两条边中图像的显示方式

显示方式的值:

repeat:图像将以平铺的方式进行显示

stretch:图像将以拉伸的方式进行显示

round

div{
border-image:url(borderimage.png) 10/5px repeat strerch;
width:300px;
height:200px;
}

(5)使用背景图像

在使用border-image属性的时候,仍然可以正常使用背景图片,但是为了不让边框图像挡住背景图像,需要使用中间为透明的边框图像,否则背景图像就有可能被边框图像的中央挡住部分或全体。

div{
background-image:url(bk.png);
background-repeat:no-repeat;
border-image:url("borderimage.png") 20 20 20 20 /5px;
background-origin:border;
border-radius:18px;
width:711px;
height:404px;
}

相关推荐:

Свойства, связанные с фоном, в HTML и CSS

Краткий обзор новых возможностей Css3: границы и фон

Атрибут границы CSS3 border_html/css_WEB-ITnose

The above is the detailed content of Introduction to properties related to borders in background properties in css3. For more information, please follow other related articles on the PHP Chinese website!

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