【问题标题】:CSS Gradient border with :afterCSS渐变边框:after
【发布时间】:2017-05-25 09:15:51
【问题描述】:

css 问题。

是否可以在边框上添加一个渐变,以反映到上面的 div?

我可以添加纯色,但似乎找不到渐变色。

current status

	.offerBox {
		width: 360px;
		height: 170px;
		position: relative;
		border-radius: 5px;
		background: linear-gradient(to right, #fcd651 0%,#f9c100 100%); 

	}
	
	.offerBox:after {
		top: 100%;
		left: 50%;
		border: solid transparent;
		content: " ";
		height: 0;
		width: 0;
		position: absolute;
		pointer-events: none;
		border-color: rgba(136, 183, 213, 0);
		border-top-color: #f9c100;
		border-width: 30px;
		margin-left: -30px;
}
<div class="offerBox"></div>

谢谢!

【问题讨论】:

  • 如果你需要对齐底部中心,你可以使用 transform:rotate(45deg) translate(-50%, 0);在 .offerBox:after 并根据需要更新顶部。检查我的更新答案

标签: css border gradient linear-gradients


【解决方案1】:

您可以移除边框并将渐变应用到:after 背景

使用transform: rotate(45deg); 使其具有三角形外观。 和z-index: -1; 将把它推到offer div 之下。

.offerBox:after {
      top: calc(100% - 15px);
      left: 50%;
      content: " ";
      height: 30px;
      width: 30px;
      position: absolute;
      pointer-events: none;
      background: linear-gradient(to right, #fcd651 0%, #f9c100 100%);
      margin-left: -30px;
      transform: rotate(45deg);
      z-index: -1;
    }

片段

.offerBox {
  width: 360px;
  height: 170px;
  position: relative;
  border-radius: 5px;
  background: linear-gradient(to right, #fcd651 0%, #f9c100 100%);
}

.offerBox:after {
  top: calc(100% - 15px);
  left: 50%;
  content: " ";
  height: 30px;
  width: 30px;
  position: absolute;
  pointer-events: none;
  background: linear-gradient(to right, #fcd651 0%, #f9c100 100%);
  margin-left: -30px;
  transform: rotate(45deg);
  z-index: -1;
}
<div class="offerBox"></div>

【讨论】:

  • 非常感谢 Nadir!
【解决方案2】:

您可以用作背景,您可以根据需要更改渐变颜色。如果您需要对齐中心底部,您可以在.offerBox:after 中使用transform:rotate(45deg) translate(-50%, 0); 并根据需要更新顶部

.offerBox {
    width: 360px;
    height: 170px;
    position: relative;
    border-radius: 5px;
    background: linear-gradient(to right, #fcd651 0%, #f9c100 100%);
}
.offerBox:after {
    top: 95%;
    left: 50%;
    content: " ";
    display: block;
    height: 30px;
    width: 30px;
    background: linear-gradient(to right, tomato 50%, green 100%);
    position: absolute;
    pointer-events: none;
    z-index: -1;
    transform: rotate(45deg) translate(-50%, 0);
}
<div class="offerBox"></div>

【讨论】:

    【解决方案3】:

    解决您的问题的一种可能方法:使用:before:after 绘制一个“box footer”,使用skew 制作一个三角形,这样您就有了响应性和准确的背景渐变。

    .offerBox {
      position relative;
      width: 360px;
      height: 170px;
      position: relative;
      border-radius: 5px;
      background: linear-gradient(to right, #fcd651 0%, #f9c100 100%);
    }
    
    .offerBox:before {
      position: absolute;
      bottom: -5px;
      left: -10px;
      content: " ";
      height: 30px;
      width: 50%;
      pointer-events: none;
      background: white;
      transform: skew(45deg);
    }
    
    .offerBox:after {
      position: absolute;
      bottom: -5px;
      right: -10px;
      content: " ";
      height: 30px;
      width: 50%;
      pointer-events: none;
      background: white;
      transform: skew(-45deg);
    }
    <div class="offerBox"></div>

    【讨论】:

    • 非常好!这里唯一的缺点是底角不圆。
    • 对,不过可能有办法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多