【问题标题】:Horizontal line with fade out effect具有淡出效果的水平线
【发布时间】:2015-06-23 15:41:05
【问题描述】:

我正在尝试用类似这样的渐变线替换引导程序 <hr />

到目前为止,我已经尝试过使用这个 mixin:

.horizontal-gradient (@startColor: #eee, @endColor: white) {
  background-color: @startColor;
  background-image: -webkit-gradient(linear, left top, right top, from(@startColor), to(@endColor));
  background-image: -webkit-linear-gradient(left, @startColor, @endColor);
  background-image: -moz-linear-gradient(left, @startColor, @endColor);
  background-image: -ms-linear-gradient(left, @startColor, @endColor);
  background-image: -o-linear-gradient(left, @startColor, @endColor);
}

但是……它没有用。

编辑

我决定创建一个单独的类(<hr/> 将来可能会很有用)。但是,我仍然无法让它工作......

.post-title-line {
  background-color: #403a41;
  background-image: -webkit-gradient(linear, left top, right top, from(#403a41), to(rgba(0, 0, 0, 0)));
  background-image: -webkit-linear-gradient(left, #403a41, rgba(0, 0, 0, 0));
  background-image: -moz-linear-gradient(left, #403a41, rgba(0, 0, 0, 0));
  background-image: -ms-linear-gradient(left, #403a41, rgba(0, 0, 0, 0));
  background-image: -o-linear-gradient(left, #403a41, rgba(0, 0, 0, 0));
  width: 100%;
  height: 1px;
  position: relative;
}
<div class="post-title-line"></div>

【问题讨论】:

  • 请详细说明没有工作,并告诉我们编译后的 CSS 输出或调用 mixin 的方式。如果使用正确,该 mixin 应该可以正常工作。
  • 你究竟是如何使用这个mixin的? Mixin 不会自行生成任何 CSS。默认情况下,Bootstrap 使用最小的 hr 来自常见的 normalize goody,并且它不调用任何 mixins。更重要的是它的高度为零(所以即使你将background-image 设置为hr 它仍然是不可见的)。
  • 好的,我创建了一个单独的类,将其命名为post-title-line,然后将width: 100%; height: 1px; position: relative; 添加到它和mixin。这是css输出pastebin.com/cnMsZ7qq

标签: css less linear-gradients


【解决方案1】:

原因:

您的问题不在于 mixin,而在于颜色的选择。 rgba(0,0,0,0) 等效于 transparent 但您已经在选择器中设置了 background-color。如此有效地,您的渐变从#403a41 变为#403a41,这只会产生一条实线。

您可以通过将 background-color 更改为选择器中的其他内容来验证此行为。在下面的 sn-p 中,我将其设置为红色,以便您了解我的意思。

.post-title-line {
  background-color: red;
  background-image: -webkit-gradient(linear, left top, right top, from(#403a41), to(rgba(0, 0, 0, 0)));
  background-image: -webkit-linear-gradient(left, #403a41, rgba(0, 0, 0, 0));
  background-image: -moz-linear-gradient(left, #403a41, rgba(0, 0, 0, 0));
  background-image: -ms-linear-gradient(left, #403a41, rgba(0, 0, 0, 0));
  background-image: -o-linear-gradient(left, #403a41, rgba(0, 0, 0, 0));
  width: 100%;
  height: 1px;
  position: relative;
}
<div class="post-title-line"></div>

解决方案:

理想的解决方案是修改 mixin 代码,如下所示,只使用 background 属性。识别渐变的较新浏览器将覆盖第一个属性(纯色),而不能识别渐变的浏览器将继续使用它。

.horizontal-gradient (@startColor: #eee, @endColor: white) {
  background: @startColor;
  background: -webkit-gradient(linear, left top, right top, from(@startColor), to(@endColor));
  background: -webkit-linear-gradient(left, @startColor, @endColor);
  background: -moz-linear-gradient(left, @startColor, @endColor);
  background: -ms-linear-gradient(left, @startColor, @endColor);
  background: -o-linear-gradient(left, @startColor, @endColor);
}

如果您无法更改 mixin,您可以在调用 mixin 后覆盖 background-color(到 transparent)或将 endColor 颜色设置为 rgba(255,255,255,1)

我会推荐前者,因为rgba(255,255,255,1) 等同于white,并且当您的页面背景不是白色而transparent and rgba(0,0,0,0) are the same as per W3C spec 时可能不适合。

请注意,此解决方案将对不支持渐变的旧浏览器的回退产生影响。他们只会看到一条实线。

.post-title-line {
  background-color: #403a41;
  background-image: -webkit-gradient(linear, left top, right top, from(#403a41), to(rgba(0, 0, 0, 0)));
  background-image: -webkit-linear-gradient(left, #403a41, rgba(0, 0, 0, 0));
  background-image: -moz-linear-gradient(left, #403a41, rgba(0, 0, 0, 0));
  background-image: -ms-linear-gradient(left, #403a41, rgba(0, 0, 0, 0));
  background-image: -o-linear-gradient(left, #403a41, rgba(0, 0, 0, 0));
  width: 100%;
  height: 1px;
  position: relative;
  background-color: transparent; /* add this line after the mixin call */
}
<div class="post-title-line"></div>

【讨论】:

  • 注意有still border around,所以我想还需要抑制边框(或者1px height 不够,因为Bootstrap 的box-sizing: content-box;)。
  • @seven-phases-max:是的,这是hr 的默认行为,基于我所看到的,但在这种情况下,OP 使用的是div,所以它不应该是一个问题: )
  • 啊,对,我错过了他使用的是 <div> 而不是 <hr> ,抱歉。
  • @seven-phases-max:不,没问题,伙计。很高兴有这个评论,因为它可能会帮助其他尝试使用 hr 的人。
  • @Harry :感谢您的宝贵时间,它奏效了。这是最终结果以及我决定保持正常<hr /> 的原因。 :awesomescreenshot.com/image/350019/…
猜你喜欢
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
  • 2020-12-28
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 2011-01-19
  • 2019-11-17
相关资源
最近更新 更多