CSS 函数

我一直以为 css 标准规范中是没有函数的,可没想到还真有几个

函数 描述 CSS 版本
attr() 返回选择元素的属性值 2
calc() 允许计算 CSS 的属性值,比如动态计算长度值 3
linear-gradient() 创建一个线性渐变的图像 3
radial-gradient() 用径向渐变创建图像 3
repeating-linear-gradient() 用重复的线性渐变创建图像 3
repeating-radial-gradient() 类似 radial-gradient(),用重复的径向渐变创建图像 3

CSS attr() 函数

CSS attr() 函数返回选择元素的属性值

attr(attribute-name)
描述
attribute-name必须。html 元素的属性名

范例

下面的代码在每个 <a> 标签后插入链接的 URL

a:after {
    content: " (" attr(href) ")";
}


CSS calc() 函数

CSS calc() 函数用于动态计算长度值

calc(expression)
描述
expression必须,一个数学表达式,结果将采用运算后的返回值
  1. 运算符前后都需要保留一个空格,例如:width: calc(100% - 10px)
  2. 任何长度值都可以使用 calc() 函数进行计算
  3. calc() 函数支持 "+", "-", "*", "/" 运算
  4. calc() 函数使用标准的数学运算优先级规则

范例

下面的代码使用 calc() 函数计算 <div> 元素的宽度

#div1 {
    position: absolute;
    left: 50px;
    width: calc(100% - 100px);
    border: 1px solid black;
    background-color: yellow;
    padding: 5px;
    text-align: center;
}


CSS linear-gradient() 函数

CSS3 函数 linear-gradient() 用于创建一个线性渐变的 "图像"

background: linear-gradient(direction, color-stop1, color-stop2, ...);
描述
direction用角度值指定渐变的方向(或角度)
color-stop1,
color-stop2,
...
用于指定渐变的起止颜色

线性渐变

为了创建一个线性渐变,需要设置一个起始点和一个方向(指定为一个角度)的渐变效果,同时还要定义终止色

终止色就是想让 Gecko 去平滑的过渡,并且必须指定至少两种,当然也会可以指定更多的颜色去创建更复杂的渐变效果

范例

下面的代码演示了从头部开始的线性渐变,从红色开始,转为黄色,再到蓝色

#grad {
    background: -webkit-linear-gradient(red,yellow,blue); /* Safari 5.1-6.0 */
    background: -o-linear-gradient(red,yellow,blue); /* Opera 11.1-12.0 */ 
    background: -moz-linear-gradient(red,yellow,blue); /* Firefox 3.6-15 */
    background: linear-gradient(red,yellow,blue); /* 标准语法 */
}

范例 2

下面的代码演示了从左侧开始的线性渐变,从红色开始,转为蓝色

#grad {
  background: -webkit-linear-gradient(left, red , blue); /* Safari 5.1 to 6.0 */
  background: -o-linear-gradient(right, red, blue); /* Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(right, red, blue); /* Firefox 3.6 to 15 */
  background: linear-gradient(to right, red , blue); /* 标准语法 */
}

范例 3

下面的代码演示了从左上角到右下角的线性渐变

#grad {
  background: -webkit-linear-gradient(left top, red , blue); /* Safari 5.1 to 6.0 */
  background: -o-linear-gradient(bottom right, red, blue); /* Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(bottom right, red, blue); /* Firefox 3.6 to 15 */
  background: linear-gradient(to bottom right, red , blue); /* 标准语法 */
}

范例 4

下面的代码演示了线性渐变指定一个角度

#grad {
  background: -webkit-linear-gradient(180deg, red, blue); /* Safari 5.1 to 6.0 */
  background: -o-linear-gradient(180deg, red, blue); /* Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(180deg, red, blue); /* Firefox 3.6 to 15 */
  background: linear-gradient(180deg, red, blue); /* 标准语法 */
}

范例 5

下面的代码演示了多个终止色

#grad {
  /* For Safari 5.1 to 6.0 */
  background: -webkit-linear-gradient(left,red,orange,yellow,green,blue,indigo,violet);
  /* For Opera 11.1 to 12.0 */
  background: -o-linear-gradient(left,red,orange,yellow,green,blue,indigo,violet);
  /* For Fx 3.6 to 15 */
  background: -moz-linear-gradient(left,red,orange,yellow,green,blue,indigo,violet);
  /* 标准语法 */
  background: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet); 
}

范例 6

下面的代码使用了透明度

#grad {
  background: -webkit-linear-gradient(left,rgba(255,0,0,0),rgba(255,0,0,1)); /*Safari 5.1-6*/
  background: -o-linear-gradient(right,rgba(255,0,0,0),rgba(255,0,0,1)); /*Opera 11.1-12*/
  background: -moz-linear-gradient(right,rgba(255,0,0,0),rgba(255,0,0,1)); /*Fx 3.6-15*/
  background: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1)); /*Standard*/
}


CSS radial-gradient() 函数

CSS 3 linear-gradient() 函数可以用来创建径向渐变

background: radial-gradient(shape size at position, start-color, ..., last-color);
描述
shape确定圆的类型:
ellipse (默认): 指定椭圆形的径向渐变
circle :指定圆形的径向渐变
size定义渐变的大小,可能值:
farthest-corner (默认) : 指定径向渐变的半径长度为从圆心到离圆心最远的角
closest-side :指定径向渐变的半径长度为从圆心到离圆心最近的边
closest-corner : 指定径向渐变的半径长度为从圆心到离圆心最近的角
farthest-side :指定径向渐变的半径长度为从圆心到离圆心最远的边
position定义渐变的位置。默认值为 center

可能的值有:
center (默认):设置中间为径向渐变圆心的纵坐标值
top:设置顶部为径向渐变圆心的纵坐标值
bottom:设置底部为径向渐变圆心的纵坐标值
start-color,
...,
last-color
用于指定渐变的起止颜色

CSS 3 repeating-linear-gradient() 函数

CSS 3 repeating-linear-gradient() 函数用于创建重复的线性渐变 "图像"

background: repeating-linear-gradient(angle | to side-or-corner, color-stop1, color-stop2, ...);
Value描述
angle定义渐变的角度方向。从 0deg 到 360deg,默认为 180deg
side-or-corner指定线性渐变的起始位置。由两个关键字组成:
第一个为指定水平位置(left 或 right)
第二个为指定垂直位置 ( top 或 bottom )
顺序是随意的,每个关键字都是可选的
color-stop1,
color-stop2,
...
指定渐变的起止颜色,由颜色值、停止位置(可选,使用百分比指定)组成




本文内容仅供个人学习/研究/参考使用,不构成任何决策建议或专业指导。分享/转载时请标明原文来源,同时请勿将内容用于商业售卖、虚假宣传等非学习用途哦~感谢您的理解与支持!

链接: https://fly63.com/course/6_261

目录选择