CSS3 Gradient Borders

Próbuję zastosować gradient do granicy, myślałem, że to takie proste:

border-color: -moz-linear-gradient(top, #555555, #111111);

To nie działa, czy ktoś wie, jaki jest prawidłowy sposób wykonywania gradientów granicznych.

Author: Zaffy, 2010-04-27

13 answers

Teraz WebKit (a przynajmniej Chrome 12) obsługuje gradienty jako obraz obramowania:

-webkit-border-image: -webkit-gradient(linear, left top, left bottom, from(#00abeb), to(#fff), color-stop(0.5, #fff), color-stop(0.5, #66cc00)) 21 30 30 21 repeat repeat;

Prooflink -- http://www.webkit.org/blog/1424/css3-gradients/
Obsługa przeglądarki: http://caniuse.com/#search=border-image

 168
Author: Tony,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-26 20:56:23

Zamiast obramowań, użyłbym gradientów tła i padding. ten sam wygląd, ale o wiele łatwiejszy, bardziej wspierany.

Prosty przykład:

<div class="g">
    <div>bla</div>
</div>

CSS:

.g {
background-image: -webkit-gradient(linear, left bottom, left top, color-stop(0.33, rgb(14,173,173)), color-stop(0.67, rgb(0,255,255)));
background-image: -moz-linear-gradient(center bottom, rgb(14,173,173) 33%, rgb(0,255,255) 67% );
padding: 2px;
}

.g > div { background: #fff; }

JsFiddle

EDIT: możesz również użyć selektora :before, Jak zauważył @WalterSchwarz w tym jsFiddle

 99
Author: szajmon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-20 17:58:34

Mozilla obsługuje obecnie tylko gradienty CSS jako wartości właściwości background-image, jak również w skróconym tle.

- https://developer.mozilla.org/en/CSS/-moz-linear-gradient

Example 3 - Gradient Borders

border: 8px solid #000;
-moz-border-bottom-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc;
-moz-border-top-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc;
-moz-border-left-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc;
-moz-border-right-colors: #555 #666 #777 #888 #999 #aaa #bbb #ccc;
padding: 5px 5px 5px 15px; 

- http://www.cssportal.com/css3-preview/borders.htm

 47
Author: Quentin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-04-26 21:53:42

border-image-slice rozszerzy gradient CSS border-image

To (jak rozumiem) zapobiega domyślnemu krojeniu "obrazu" na sekcje - bez niego nic nie pojawia się, jeśli obramowanie jest tylko po jednej stronie, a jeśli jest wokół całego elementu, cztery małe gradienty pojawiają się w każdym rogu.

  border-bottom: 6px solid transparent;
  border-image: linear-gradient(to right, red , yellow);
  border-image-slice: 1;
 34
Author: Dave Everitt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-09 21:58:56

Spróbuj tego, działa dobrze na web-kit

Http://jsfiddle.net/284sa/

.border { 
    width: 400px;
    padding: 20px;
    border-top: 10px solid #FFFF00;
    border-bottom:10px solid #FF0000;
    background-image: 
        linear-gradient(#FFFF00, #FF0000),
        linear-gradient(#FFFF00, #FF0000)
    ;
    background-size:10px 100%;
    background-position:0 0, 100% 0;
    background-repeat:no-repeat;
}

<div class="border">Hello!</div>
 23
Author: GibboK,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-30 09:33:14

Jest to hack, ale można osiągnąć ten efekt w niektórych przypadkach za pomocą obrazu tła, aby określić gradient, a następnie maskowanie rzeczywistego tła za pomocą cienia pola. Na przykład:

p {
  display: inline-block;
  width: 50px;
  height: 50px;
  /* The background is used to specify the border background */
  background: -moz-linear-gradient(45deg, #f00, #ff0);
  background: -webkit-linear-gradient(45deg, #f00, #ff0);
  /* Background origin is the padding box by default.
  Override to make the background cover the border as well. */
  -moz-background-origin: border;
  background-origin: border-box;
  /* A transparent border determines the width */
  border: 4px solid transparent;
  border-radius: 8px;
  box-shadow:
    inset 0 0 12px #0cc, /* Inset shadow */
    0 0 12px #0cc, /* Outset shadow */
    inset -999px 0 0 #fff; /* The background color */
}

From: http://blog.nateps.com/the-elusive-css-border-gradient

 9
Author: Nate Smith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-09-21 01:04:03

Zgadzam się z szajmonem. Jedynym problemem jego i Quentina jest kompatybilność między przeglądarkami.

HTML:

<div class="g">
    <div>bla</div>
</div>

CSS:

.g {
background-image: -webkit-linear-gradient(300deg, white, black, white); /* webkit browsers (Chrome & Safari) */
background-image: -moz-linear-gradient(300deg, white, black, white); /* Mozilla browsers (Firefox) */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#000000', gradientType='1'); /* Internet Explorer */
background-image: -o-linear-gradient(300deg,rgb(255,255,255),rgb(0,0,0) 50%,rgb(255,255,255) 100%); /* Opera */
}

.g > div { background: #fff; }
 3
Author: Scotty,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-20 23:10:53

Webkit obsługuje gradienty w borders , a teraz akceptuje gradient w formacie Mozilla.

Firefox twierdzi, że obsługuje gradienty na dwa sposoby:

  1. używanie border-image z border-image-source
  2. użycie border-right-colors (right/left/top/bottom)

IE9 nie ma wsparcia.

 2
Author: SamGoody,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-11 21:50:03

Innym włamaniem do osiągnięcia tego samego efektu jest wykorzystanie wielu obrazów tła, funkcja, która jest obsługiwana w IE9+, newish Firefox i większość przeglądarek opartych na WebKit: http://caniuse.com/#feat=multibackgrounds

W IE6-8 można również użyć kilku tła: http://www.beyondhyper.com/css3-multiple-backgrounds-in-non-supportive-browsers/

Na przykład, załóżmy, że chcesz lewej krawędzi szerokości 5px, która jest liniowym gradientem od niebieski do białego. Utwórz gradient jako obraz i wyeksportuj go do formatu PNG. Lista pozostałych tła CSS po dla gradientu lewej krawędzi:

#theBox {
    background:
        url(/images/theBox-leftBorderGradient.png) left no-repeat,
        ...;
}

Można dostosować tę technikę do gradientów górnej, prawej i dolnej krawędzi, zmieniając część pozycji tła właściwości background Stenografia.

Oto jsfiddle dla podanego przykładu: http://jsfiddle.net/jLnDt/

 2
Author: Daniel Trebbien,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-12 23:18:49

Gradientowe obramowania z Css-triki: http://css-tricks.com/examples/GradientBorder/

.multbg-top-to-bottom {
  border-top: 3px solid black;
  background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#000), to(transparent));
  background-image: -webkit-linear-gradient(#000, transparent);
  background-image:
      -moz-linear-gradient(#000, transparent),
      -moz-linear-gradient(#000, transparent);
  background-image:
      -o-linear-gradient(#000, transparent),
      -o-linear-gradient(#000, transparent);
  background-image: 
      linear-gradient(#000, transparent),
      linear-gradient(#000, transparent);
  -moz-background-size: 3px 100%;
  background-size: 3px 100%;
  background-position: 0 0, 100% 0;
  background-repeat: no-repeat; 
}
 2
Author: VVS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-27 14:23:25

Dla obsługi między przeglądarkami możesz również spróbować naśladować gradientową obramowanie za pomocą pseudo elementów :before lub :after, w zależności od tego, co chcesz zrobić.

 1
Author: Denees,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-05 09:21:52

Wypróbuj ten kod

.gradientBoxesWithOuterShadows { 
height: 200px;
width: 400px; 
padding: 20px;
background-color: white; 

/* outer shadows  (note the rgba is red, green, blue, alpha) */
-webkit-box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.4); 
-moz-box-shadow: 0px 1px 6px rgba(23, 69, 88, .5);

/* rounded corners */
-webkit-border-radius: 12px;
-moz-border-radius: 7px; 
border-radius: 7px;

/* gradients */
background: -webkit-gradient(linear, left top, left bottom, 
color-stop(0%, white), color-stop(15%, white), color-stop(100%, #D7E9F5)); 
background: -moz-linear-gradient(top, white 0%, white 55%, #D5E4F3 130%); 
}

A może odnieść się do tego skrzypka: http://jsfiddle.net/necolas/vqnk9/

 0
Author: x'tian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-15 08:07:29

Oto ładny, półprzezroczysty sposób na obramowanie gradientu, które zanika w połowie drogi w dół. Wystarczy ustawić color-stop na rgba(0, 0, 0, 0)

.fade-out-borders {
min-height: 200px; /* for example */

-webkit-border-image: -webkit-gradient(linear, 0 0, 0 50%, from(black), to(rgba(0, 0, 0, 0))) 1 100%;
-webkit-border-image: -webkit-linear-gradient(black, rgba(0, 0, 0, 0) 50%) 1 100%;
-moz-border-image: -moz-linear-gradient(black, rgba(0, 0, 0, 0) 50%) 1 100%;
-o-border-image: -o-linear-gradient(black, rgba(0, 0, 0, 0) 50%) 1 100%;
border-image: linear-gradient(to bottom, black, rgba(0, 0, 0, 0) 50%) 1 100%;
}

<div class="fade-out-border"></div>

Objaśnienie użycia:

Formal grammar: linear-gradient(  [ <angle> | to <side-or-corner> ,]? <color-stop> [, <color-stop>]+ )
                              \---------------------------------/ \----------------------------/
                                Definition of the gradient line         List of color stops  

Więcej tutaj: https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient

 0
Author: Yes Barry,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-03 00:40:23