框箭头采取相应框的背景颜色

问题描述 投票:0回答:3

我会尝试尽可能简单地解释这个:

对于这个example,我希望每个框的箭头自动获取其各自框的背景颜色,只需在框中添加红色,黑色,灰色类。我很容易做到:

    .box.red {
      &:before {
        ...
        border-bottom: 1px solid $red;

      }
    }
   .box.black {
      &:before {
        ...
        border-bottom: 1px solid $black;

      }
    }
   .box.grey {
      &:before {
        ...
        border-bottom: 1px solid $grey;

      }
    }

但这意味着对于我创建的每种不同颜色,我都必须手动更改箭头颜色。如果将来我想添加一个绿色框,我将不得不在CSS中将其箭头颜色更改为绿色。 Sass有没有一种方法可以做到这一点所以每次盒子背景颜色改变时我都不用担心改变箭头颜色?

谢谢。

See DEMO

html css sass mixins
3个回答
1
投票

由于您的元素将具有纯色,您可以在应用旋转的位置创建带有矩形的箭头以实现布局,然后您可以使用inherit作为背景

.box {
  position: relative;
  width: 100px;
  height: 100px;
  margin-bottom: 40px;
  background:red;
}
.box::before {
  content:"";
  position:absolute;
  width:30px;
  height:30px;
  top:100%;
  left:20px;
  transform:translateY(-50%) rotateX(40deg) rotate(45deg);
  background:inherit;
}

.blue {
  background:blue;
}
.green {
  background:green;
}
<div class="box">

</div>

<div class="box blue">

</div>
<div class="box green">

</div>

您也可以依赖CSS变量,即使在很多地方使用过,也只能改变颜色一次。

这是另一种创建形状的方法,您可以轻松控制箭头的颜色,大小和位置:

.box {
  position: relative;
  width: 100px;
  height: 100px;
  padding-bottom: var(--s,20px);
  margin-bottom:10px;
  background:
    linear-gradient(to top right,transparent 49.8%,var(--c,red) 50%) var(--p,20px) 100%,
    linear-gradient(to top left, transparent 49.8%,var(--c,red) 50%) calc(var(--p,20px) + var(--s,20px)) 100%,
    var(--c,red) content-box;
  background-size:var(--s,20px) var(--s,20px);
  background-repeat:no-repeat;
}

.blue {
  --c:blue;
  --s:15px;
  --p:40px;
}
.green {
  --c:green;
  --s:10px;
  --p:60px;
}
<div class="box"></div>

<div class="box blue"></div>

<div class="box green"></div>

您可以轻松地为代码实现相同的逻辑:

.box {
  position: relative;
  width: 100px;
  height: 100px;
  margin-bottom: 40px;
  background:var(--c,green);
}
.box:before {
  content: ' ';
  border: solid transparent;
  border-bottom: 1px solid var(--c,green);
  border-width: 15px;
  height: 0;
  position: absolute;
  transform: rotate(180deg);
  width: 0;
  bottom: -30px;
  left: 20px;
}

.red {
  --c: #f00;
}

.black {
  --c: #000;
}

.grey {
  --c: #aaa;
}
<div class="box red"></div>
<div class="box black"></div>
<div class="box grey"></div>
<div class="box "></div>

1
投票

为了达到这个效果,最好的方法是使用css继承。由于父框具有背景集,因此我们只能继承:after伪元素background属性上的值 - 我们不能在边框颜色上使用它。幸运的是,我们可以在不使用边框的情况下获得“三角箭头”效果,但使用常规背景和clip-path规则。完整示例显示在下面的代码段中:

.box {
  position: relative;
  width: 100px;
  height: 100px;
  margin-bottom: 40px;
}
.box:before {
  content: '';
  position: absolute;
  background: inherit;
  clip-path: polygon(0 0, 30px 0, 15px 15px);
  width: 30px;
  height: 15px;
  bottom: -16px;
  left: 20px;
}

.red {
  background: #f00;
}

.black {
  background: #000;
}

.grey {
  background: #aaa;
}
<div class="box red"></div>
<div class="box black"></div>
<div class="box grey"></div>

IE和Edge的更新

如果所有这些都是真的:

  1. 与IE和Edge的兼容性是您关心的问题
  2. 您可以在css规则中处理一些微复制
  3. 你不打算在你的.box元素上有边框

然后你可以将border-bottom-color应用到.box类并在:after伪元素定义中继承它,如下所示:

.box {
  position: relative;
  width: 100px;
  height: 100px;
  margin-bottom: 40px;
}
.box:before {
  content: ' ';
  border-bottom: 1px solid;
  border-bottom-color: inherit;
  border-left: 1px solid transparent;
  border-right: 1px solid transparent;
  border-top: 1px solid transparent;
  border-width: 15px;
  height: 0;
  position: absolute;
  transform: rotate(180deg);
  width: 0;
  bottom: -30px;
  left: 20px;
}

.red {
  background: #f00;
  border-bottom-color: #f00;
}

.black {
  background: #000;
  border-bottom-color: #000;
}

.grey {
  background: #aaa;
  border-bottom-color: #aaa;
}
<div class="box red"></div>
<div class="box black"></div>
<div class="box grey"></div>

1
投票

这可以使用SCSS mixin来实现。 See codepen here

在链接的codepen中,有一个定义颜色的变量:

$color-list: (
  "red": #f00,
  "black": #000,
  "grey": #aaa,
);

此变量是需要定义颜色的唯一位置。如果需要更多的盒子,您可以轻松添加更多颜色。

然后有一个@mixin添加background-color和彩色边框:

@mixin box-color() {
  @each $color in map-keys($color-list) {
    &.#{$color} {
      background: map-get($color-list, $color);
      &:before {
        content: ' ';
        border: solid transparent;
        border-bottom: 1px solid map-get($color-list, $color);
        border-width: 15px;
        height: 0;
        position: absolute;
        transform: rotate(180deg);
        width: 0;
        bottom: -30px;
        left: 20px;
      }
    }
  }
}

mixin基本上为$color-list中的每种颜色添加了一个新类,并使用适当的背景和伪元素颜色填充该类。

mixin只需要包含在.box类中:

.box {
  @include box-color();
  position: relative;
  width: 100px;
  height: 100px;
  margin-bottom: 40px;
}
© www.soinside.com 2019 - 2024. All rights reserved.