2010-02-19 6 views
270

RGBA est extrêmement amusant, tout comme -webkit-gradient, -moz-gradient et ... progid:DXImageTransform.Microsoft.gradient ... ouais. :)CSS3 Transparence + Dégradé

Existe-t-il un moyen de combiner les deux, RGBA et dégradés, de sorte qu'il y ait un dégradé de transparence alpha en utilisant les dernières spécifications CSS.

+1

@ commentaire de geo1701 devrait devenir l'accepter un, car il est plus pertinent pour les normes modernes. –

Répondre

320

Oui. Vous pouvez utiliser RGBA dans les deux webkit et déclarations de gradient moz:

/* webkit example */ 
background-image: -webkit-gradient(
    linear, left top, left bottom, from(rgba(50,50,50,0.8)), 
    to(rgba(80,80,80,0.2)), color-stop(.5,#333333) 
); 

(src)

/* mozilla example - FF3.6+ */ 
background-image: -moz-linear-gradient(
    rgba(255, 255, 255, 0.7) 0%, rgba(255, 255, 255, 0) 95% 
); 

(src)

Apparemment, vous pouvez même le faire dans IE, en utilisant un étrange « étendu hex "syntaxe. La première paire (dans l'exemple 55) fait référence au niveau d'opacité:

/* approximately a 33% opacity on blue */ 
filter: progid:DXImageTransform.Microsoft.gradient(
    startColorstr=#550000FF, endColorstr=#550000FF 
); 

/* IE8 uses -ms-filter for whatever reason... */ 
-ms-filter: progid:DXImageTransform.Microsoft.gradient(
    startColorstr=#550000FF, endColorstr=#550000FF 
); 

(src)

+19

FYI, "extended hex" est juste ARGB 32 bits au lieu des valeurs de couleur RVB 24 bits. – Macke

+2

je voudrais que ceux-ci fonctionnent en standard css, aussi, mais avec l'alpha à la fin (semble plus naturel): '# 0001' serait court hex pour" presque transparent noir "et' # ffcc00ff' serait le même comme '# ffcc00', c'est-à-dire" jaune mandarine complètement opaque " –

+55

regardez également le [CSS3 Gradient Generator sur Colorzilla] (http://www.colorzilla.com/gradient-editor/) qui a un tas de préréglages agréables et toutes les variations compatibles avec les navigateurs croisés pour atteindre le dégradé désiré – andrhamm

14

Ceci est quelque chose de vraiment cool! J'avais besoin à peu près de la même chose, mais avec un dégradé horizontal du blanc au transparent. Et ça marche très bien! Ici ist mon code:

.gradient{ 
     /* webkit example */ 
     background-image: -webkit-gradient(
      linear, right top, left top, from(rgba(255, 255, 255, 1.0)), 
      to(rgba(255, 255, 255, 0)) 
     ); 

     /* mozilla example - FF3.6+ */ 
     background-image: -moz-linear-gradient(
      right center, 
      rgba(255, 255, 255, 1.0) 20%, rgba(255, 255, 255, 0) 95% 
     ); 

     /* IE 5.5 - 7 */ 
     filter: progid:DXImageTransform.Microsoft.gradient(
      gradientType=1, startColor=0, endColorStr=#FFFFFF 
     ); 

     /* IE8 uses -ms-filter for whatever reason... */ 
     -ms-filter: progid:DXImageTransform.Microsoft.gradient(
      gradientType=1, startColor=0, endColoStr=#FFFFFF 
     ); 
    } 
+0

Les dégradés IE7 et IE8 ne spécifient aucune couleur alpha? Est-ce qu'ils deviennent vraiment transparents? – KajMagnus

+1

pour référence, sur l'implémentation de Microsoft, gradientType = 1 est horizontal, 0 est vertical. – Brooks

82

Nouvelle syntaxe a été pris en charge pour un certain temps par tous les navigateurs modernes (à partir de Chrome 26, Opera 12.1, IE 10 et Firefox 16): http://caniuse.com/#feat=css-gradients

background: linear-gradient(to bottom, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0)); 

Cela rend un dégradé, en partant du noir uni en haut, jusqu'à la transparence totale en bas.

Documentation on MDN.

+7

.. crée un noir plein en haut à complètement transparent en bas – commonpike

+0

Je suppose que c'est une suggestion et ne fonctionne pas réellement? – bart

+0

@bart L'avez-vous essayé? Fonctionne bien. – rybo111

3

Voici mon code:

background: #e8e3e3; /* Old browsers */ 
    background: -moz-linear-gradient(top, rgba(232, 227, 227, 0.95) 0%, rgba(246, 242, 242, 0.95) 100%); /* FF3.6+ */ 
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(232, 227, 227, 0.95)), color-stop(100%,rgba(246, 242, 242, 0.95))); /* Chrome,Safari4+ */ 
    background: -webkit-linear-gradient(top, rgba(232, 227, 227, 0.95) 0%,rgba(246, 242, 242, 0.95) 100%); /* Chrome10+,Safari5.1+ */ 
    background: -o-linear-gradient(top, rgba(232, 227, 227, 0.95) 0%,rgba(246, 242, 242, 0.95) 100%); /* Opera 11.10+ */ 
    background: -ms-linear-gradient(top, rgba(232, 227, 227, 0.95) 0%,rgba(246, 242, 242, 0.95) 100%); /* IE10+ */ 
    background: linear-gradient(to bottom, rgba(232, 227, 227, 0.95) 0%,rgba(246, 242, 242, 0.95) 100%); /* W3C */ 
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='rgba(232, 227, 227, 0.95)', endColorstr='rgba(246, 242, 242, 0.95)',GradientType=0); /* IE6-9 */ 
+1

Gradien ne fonctionne pas pour IE9 – Yarg

10

J'utilise http://enjoycss.com pour générer toute sorte de gradients simples et complexes thty ont beaucoup de modèles gradient dans la galerie et enter image description here

+0

ne sais pas pourquoi mais cette solution, ne fonctionne pas totalement pour moi. Cependant, colorzilla a fonctionné comme un charme, donc je n'ai pas essayé encore, beau regarder, cependant ... :) – mikus

2
#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*/ 
} 

I J'ai trouvé ça dans w3schools et j'ai répondu à mes besoins pendant que je cherchais le dégradé et la transparence. Je fournis le lien pour faire référence à w3schools. J'espère que cela aide si quelqu'un est à la recherche de gradient et de transparence.

http://www.w3schools.com/css/css3_gradients.asp

Aussi je l'ai essayé dans W3Schools de changer l'opacité coller le lien pour CONTROLEZ

http://www.w3schools.com/css/tryit.asp?filename=trycss3_gradient-linear_trans

it helps.

0

Je viens de rencontrer cet exemple plus récent.Pour simplifier et utiliser les exemples les plus récents, ce qui donne le css une classe de sélection de « grad », (je l'ai inclus rétrocompatibilité)

.grad { 
    background-color: #F07575; /* fallback color if gradients are not supported */ 
    background-image: -webkit-linear-gradient(top left, red, rgba(255,0,0,0));/* For Chrome 25 and Safari 6, iOS 6.1, Android 4.3 */ 
    background-image: -moz-linear-gradient(top left, red, rgba(255,0,0,0));/* For Firefox (3.6 to 15) */ 
    background-image: -o-linear-gradient(top left, red, rgba(255,0,0,0));/* For old Opera (11.1 to 12.0) */ 
    background-image: linear-gradient(to bottom right, red, rgba(255,0,0,0)); /* Standard syntax; must be last */ 
} 

de https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient