ЦСС Триангле Микин - ЦСС-трикови

Anonim

Постоји прилично популаран ЦСС хакер који користи прозирне ивице на елементу ширине / висине 0 да би имитирао троуглове. Овде се налази ЦСС исечак на ЦСС-триковима који га приказује.

Ако се попут мене никада не сетите како то функционише, будите сигурни да можемо да нам помогнемо помоћу Сасс-а.

/// Triangle helper mixin /// @param (Direction) $direction - Triangle direction, either `top`, `right`, `bottom` or `left` /// @param (Color) $color (currentcolor) - Triangle color /// @param (Length) $size (1em) - Triangle size @mixin triangle($direction, $color: currentcolor, $size: 1em) ( @if not index(top right bottom left, $direction) ( @error "Direction must be either `top`, `right`, `bottom` or `left`."; ) width: 0; height: 0; content: ''; z-index: 2; border-#(opposite-position($direction)): ($size * 1.5) solid $color; $perpendicular-borders: $size solid transparent; @if $direction == top or $direction == bottom ( border-left: $perpendicular-borders; border-right: $perpendicular-borders; ) @else if $direction == right or $direction == left ( border-bottom: $perpendicular-borders; border-top: $perpendicular-borders; ) )

Додатне напомене:

* opposite-positionФункција долази из Цомпасс-а; ако не користите Цомпасс, можда ћете морати да имате свој;
* Микин се не бави позиционирањем троугла; сасвим је у реду комбинирати га са мешавином за позиционирање;
* contentДиректива треба да дозволи да се користи на псеудо-елементима, што заправо и завршава у већини случајева.

Употреба

.foo::before ( @include triangle(bottom); position: absolute; left: 50%; bottom: 100%; )
.foo::before ( width: 0; height: 0; content: ''; z-index: 2; border-top: 1.5em solid currentColor; border-left: 1em solid transparent; border-right: 1em solid transparent; position: absolute; left: 50%; bottom: 100%; )