English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
CSS linear-gradient()函数创建一个图像,该图像表示颜色的线性渐变。结果是类型为<gradient>的CSS对象,这是<image>的特殊形式。
以下示例演示了从头部开始的线性渐变,从红色开始,转为黄色,再到蓝色:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Tutorial básico(oldtoolbag.com)</title> <style> #grad1 { height: 200px; background-image: linear-gradient(red, yellow, blue); } </style> </head> <body> <h3>Gradiente lineal - 头部到底部</h3> <p>从头部开始的线性渐变,从红色开始,转为黄色,再到蓝色:</p> <div id="grad1></div> <p><strong>Nota:</strong> Internet Explorer 9 及更早版本 IE 浏览器不支持渐变。</p> </body> </html>Prueba aquí ‹/›
linear-gradient() 函数用于创建一个线性渐变的 "图像"。
为了创建一个线性渐变,你需要设置一个起始点和一个方向(指定为一个角度)的渐变效果。你还要定义终止色。终止色就是你想让Gecko去平滑的过渡,并且你必须指定至少两种,当然也会可以指定更多的颜色去创建更复杂的渐变效果。
支持版本:CSS3
表格中的数字表示支持该函数的第一个浏览器版本号。
"webkit" 或 "moz" 或 "o" 指定的数字为支持该函数的第一个版本号前缀。
函数 | |||||
---|---|---|---|---|---|
linear-gradient() | 26.0 10.0 -webkit- | 10.0 | 16.0 3.6 -moz- | 6.1 5.1 -webkit- | 12.1 11.1 -o- |
background-image: linear-gradient(direction, color-stop1, color-stop2, ...)
值 | 描述 |
---|---|
direction | 用角度值指定渐变的方向(或角度)。 |
color-stop1, color-stop2,... | 用于指定渐变的起止颜色。 |
以下示例演示了从左侧开始的线性渐变,从红色开始,转为黄色:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Tutorial básico(oldtoolbag.com)</title> <style> #grad1 { height: 200px; background-color: red; /* 不支持线性的时候显示 */ background-image: linear-gradient(to right, red , yellow); } </style> </head> <body> <h3>Gradiente lineal - 从左到右</h3> <p>从左边开始的线性渐变。起点是红色,慢慢过渡到黄色:</p> <div id="grad1></div> <p><strong>Nota:</strong> Internet Explorer 8 Las versiones anteriores a la 8 no soportan gradientes.</p> </body> </html>Prueba aquí ‹/›
以下示例演示了从左上角到右下角的线性渐变:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Tutorial básico(oldtoolbag.com)</title> <style> #grad1 { height: 200px; background-color: red; /* 不支持线性的时候显示 */ background-image: linear-gradient(to bottom right, red , yellow); } </style> </head> <body> <h3>Gradiente lineal - 对角</h3> <p>从左上角开始(到右下角)的线性渐变。起点是红色,慢慢过渡到黄色:</p> <div id="grad1></div> <p><strong>Nota:</strong> Internet Explorer 8 Las versiones anteriores a la 8 no soportan gradientes.</p> </body> </html>Prueba aquí ‹/›
以下示例演示了线性渐变指定一个角度:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Tutorial básico(oldtoolbag.com)</title> <style> #grad1 { height: 100px; background-color: red; /* Se muestra cuando el navegador no lo admite */ background-image: linear-gradient(0deg, red, yellow); } #grad2 { height: 100px; background-color: red; /* Se muestra cuando el navegador no lo admite */ background-image: linear-gradient(90deg, red, yellow); } #grad3 { height: 100px; background-color: red; /* Se muestra cuando el navegador no lo admite */ background-image: linear-gradient(180deg, red, yellow); } #grad4 { height: 100px; background-color: red; /* Se muestra cuando el navegador no lo admite */ background-image: linear-gradient(-90deg, red, yellow); } </style> </head> <body> <h3>Gradiente lineal - Usar diferentes ángulos</h3> <div id="grad1" style="text-align:center;">0deg</div><br> <div id="grad2" style="text-align:center;">90deg</div><br> <div id="grad3" style="text-align:center;">180deg</div><br> <div id="grad4" style="text-align:center;">-90deg</div> <p><strong>Nota:</strong> Internet Explorer 9 Las versiones anteriores a la 8 no soportan gradientes.</p> </body> </html>Prueba aquí ‹/›
El siguiente ejemplo muestra múltiples colores de terminación:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Tutorial básico(oldtoolbag.com)</title> <style> #grad1 { height: 55px; background-color: red; /* Se muestra cuando el navegador no lo admite */ background-image: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet); /* La sintaxis estándar (debe estar al final) */ } </style> </head> <body> <div id="grad1" style="text-align:center;margin:auto;color:#888888;font-size:40px;font-weight:bold"> Fondo de gradiente </div> <p><strong>Nota:</strong> Internet Explorer 8 Las versiones anteriores a la 8 no soportan gradientes.</p> </body> </html>Prueba aquí ‹/›
El siguiente ejemplo utiliza transparencia:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Tutorial básico(oldtoolbag.com)</title> <style> #grad1 { height: 200px; background-image: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1)); } </style> </head> <body> <h3>Gradiente lineal - Opacidad</h3> <p>Para agregar transparencia, usamos la función rgba() para definir los puntos de color. El último parámetro de la función rgba() puede ser de 0 a 1 El valor, que define la opacidad del color: 0 representa la transparencia completa,1 representa la opacidad completa.</p> <div id="grad1></div> <p><strong>Nota:</strong> Internet Explorer 8 Las versiones anteriores a la 8 no soportan gradientes.</p> </body> </html>Prueba aquí ‹/›
Tutorial CSS: CSS3 Gradiente