【问题标题】:Adding CSS Trapezoid Shape :After Button添加 CSS 梯形形状 :After Button
【发布时间】:2015-03-19 04:34:15
【问题描述】:

我正在尝试为客户端创建按钮样式,但似乎无法使用 after 伪类使其正常工作。

<style>
$varBase: 40px;
$imsblue: #012169;
$imsgrey: #012169;

body {
  background:grey;
}

.btn {
  position: relative;
  float: left;
  height: $varBase;
  font-family: sans-serif;
  text-align: center;
  line-height: $varBase;
  color: white;
  white-space: nowrap;
  text-transform: uppercase;
  background: $imsblue;
  &:before {
    float: left;
    content:"";
    width: ($varBase/4);
    height: ($varBase/2);
  }
  &:after {
    position: absolute;
    content:"";
    height: ($varBase/2);
    border-left: ($varBase/2) solid $imsblue;
    border-bottom: ($varBase/2) solid transparent;
  }
  a {
    color: white;
    text-decoration:none;
    padding: ($varBase/4) ($varBase/2);
    margin-right: -10px;
  }
}
.btn3 {
  display: inline;
  color: white;
  background: linear-gradient(135deg, rgba(1,33,105,1) 0%, rgba(1,33,105,1) 93%, rgba(30, 87, 153, 0) 93%, rgba(30, 87, 153, 0) 100%);
  outline: 0;
  border: 0;
  padding: 10px 0px;
  a {
    color: inherit ;
    text-transform: uppercase;
    text-decoration:none;
    padding: ($varBase/4) $varBase;
  }
}
</style>
<div class="btn"><a href="#">Click to Submit</a></div>
<div class="btn3"><a href="#">Click to Submit</a></div>

我可以使用两个 DIV 来显示它,但我需要它来处理一个类。有人可以帮我看看我做错了什么吗?

它应该看起来像这样(当然除了颜色和大小):

【问题讨论】:

  • 你到底想做什么?有什么作用?
  • 我已经用一张我认为它会是什么样子的图形更新了我的原始问题。在我的脑海里是有道理的。
  • 检查你编译的 CSS。你没有得到你认为你得到的。

标签: html css sass css-shapes


【解决方案1】:

我认为缺少的关键元素是您需要在 :after 伪类中包含 content:""。请参阅下面的示例。

.btn {
  height: 40px;
  background: red;
  width: 128px;
  float:left;
}

.btn:after {
    width: 0px;
    height: 20px;
    border-left: 20px solid red;
    border-bottom: 20px solid white;
    float:right;
    content:"";
}
&lt;div class="btn"&gt;Button&lt;/div&gt;

【讨论】:

  • 天啊,我觉得自己像个白痴。对此感激不尽。真的,这是直到午夜才会注意到的小事之一。
【解决方案2】:

这行得通 - 我必须将你的 SCSS 转换为 CSS,但它已经足够清晰了。

.btn {
  height: 40px; width: 200px;
  background: red;
  position: relative; /* work as container */
}
.btn:after {
  content: ''; /* needed */
  display: block;
  position: absolute; /* position to container */
  right: 0; bottom: 0;
  border-left: 20px solid red;
  border-bottom: 20px solid white;
}
&lt;div class="btn"&gt;Button&lt;/div&gt;

不幸的是,你不能有“透明”的覆盖,它不会工作。我不得不使用白色。

【讨论】:

    【解决方案3】:

    我找到了一种适用于“剪切”透明的解决方案。您可以为按钮使用常规背景或图像背景:

    http://jsfiddle.net/q45w2f78/

    <div class="buttoncut gon">My button</div>
    

    CSS:

    .gon {
      width: 220px;
      height: 220px;
      background: darkblue;
      background-size: 220px 220px;
    
      /* Text styling */
      line-height: 220px;
      text-align: center;
      font-family: sans-serif;
      font-size: 15px;
      font-weight: bold;
      letter-spacing: 6px;
      color: beige;
    }
    .gon:hover {
      color: #fff;
      text-shadow: 0 0 10px white;
    }
    
    .buttoncut {
      height: 200px;
      -webkit-clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%); clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%);
      -moz-clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%); clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%);
      -ms-clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%); clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%);
      clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%); clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%);
    }
    

    我使用这个生成器来获得正确的多边形 css:http://bennettfeely.com/clippy/

    【讨论】:

    • 哇!这真太了不起了。我一直在寻找这样的网站,但遗憾的是 Google 不是我的朋友。
    • 是的,我花了一点时间才找到它。最重要的词是“剪辑路径”,使用它后我发现了这个宝石。我喜欢那个剪辑路径:多边形实际上是从 div 中切掉的,而不仅仅是用颜色覆盖它。这意味着您可以使用例如背景图像上方的 div。
    • 只要你有恒定的高度,我想你可以使用 2 个 div。一个持续切割的“结束” div。
    • 这里有一个解决方案。非常快速的修复,可能有一些错误...jsfiddle.net/q45w2f78/2
    • 您可以使用 jquery 选择器添加所需的 div,只是为了查看。不知道如何以轻量级的方式做到这一点。
    【解决方案4】:

    渐变:

    您可以使用渐变来实现这一点,这样您就可以将它应用到任何元素(这是通过按钮元素完成的):

    html,body{
      background:red;
      }
    
    button {
      background: -moz-linear-gradient(-45deg, rgba(30, 87, 153, 1) 0%, rgba(30, 87, 153, 1) 89%, rgba(30, 87, 153, 0) 90%, rgba(30, 87, 153, 0) 100%);
      /* FF3.6+ */
      background: -webkit-gradient(linear, left top, right bottom, color-stop(0%, rgba(30, 87, 153, 1)), color-stop(89%, rgba(30, 87, 153, 1)), color-stop(90%, rgba(30, 87, 153, 0)), color-stop(100%, rgba(30, 87, 153, 0)));
      /* Chrome,Safari4+ */
      background: -webkit-linear-gradient(-45deg, rgba(30, 87, 153, 1) 0%, rgba(30, 87, 153, 1) 89%, rgba(30, 87, 153, 0) 90%, rgba(30, 87, 153, 0) 100%);
      /* Chrome10+,Safari5.1+ */
      background: -o-linear-gradient(-45deg, rgba(30, 87, 153, 1) 0%, rgba(30, 87, 153, 1) 89%, rgba(30, 87, 153, 0) 90%, rgba(30, 87, 153, 0) 100%);
      /* Opera 11.10+ */
      background: -ms-linear-gradient(-45deg, rgba(30, 87, 153, 1) 0%, rgba(30, 87, 153, 1) 89%, rgba(30, 87, 153, 0) 90%, rgba(30, 87, 153, 0) 100%);
      /* IE10+ */
      background: linear-gradient(135deg, rgba(30, 87, 153, 1) 0%, rgba(30, 87, 153, 1) 89%, rgba(30, 87, 153, 0) 90%, rgba(30, 87, 153, 0) 100%);
      /* W3C */
      filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#1e5799', endColorstr='#001e5799', GradientType=1);
      /* IE6-9 fallback on horizontal gradient */
      
      outline:0;
      border:0;
      padding:5px;
    }
    &lt;button&gt;PressMe&lt;/button&gt;

    伪元素(不适合渐变/图像背景)

    div {
      position: relative;
      display:inline-block;
      padding:5px;
      background:gray;
    }
    div:after{
      content:"";
      position:absolute;
      border-bottom:10px solid blue;
      border-left:10px solid transparent;
      bottom:0;
      right:0;
      }
    html,body{
      background:blue;
      }
    &lt;div&gt;Press Me!&lt;/div&gt;

    剪辑路径

    button {
      padding: 10px;
      height: 60px;
      width: 60px;
      -webkit-clip-path: polygon(0% 0%, 100% 0, 100% 75%, 75% 100%, 0% 100%);
      clip-path: polygon(0% 0%, 100% 0, 100% 75%, 75% 100%, 0% 100%);
    }
    html,body{
      background:green;
      }
    &lt;button&gt;press me!!!&lt;/button&gt;

    动态长度

    通过使用下面的sn-p,你可以做出一个不受长度影响的很棒的按钮!

    button {
      position: relative;
      border: 0;
      outline: 0;
      height: 20px;
      background: gray;
    }
    button:after {
      position: absolute;
      content: "";
      right: -10px;
      width: 0px;
      height: 0px;
      bottom: 0;
      border-bottom: 10px solid transparent;
      border-left: 10px solid gray;
    }
    button:before {
      position: absolute;
      content: "";
      right: -10px;
      width: 10px;
      height: 10px;
      top: 0;
      background: gray;
    }
    html,
    body {
      background: red;
    }
    
    /*HOVER EFFECTS*/
    button:hover,
    button:hover:before {
      background: yellow;
    }
    button:hover:after {
      border-left: 10px solid yellow;
    }
    &lt;button&gt;press me and plus i can get really long!&lt;/button&gt;

    【讨论】:

    • 我再次更新了我原来的操作码。我不得不取消剪辑路径。它只适用于固定宽度的按钮,我不确定他们可以制作多大的按钮。我能够在之前和之后的 css 以及线性渐变中获得良好的结果。然而,线性渐变在处理超长文本时也存在问题。
    • @bmoneruxui:渐变可以添加到按钮末尾的伪元素。我会尽快编辑这个,但现在我有一些工作要做。但这可以通过结合这两个想法来完成。将发布 tomoz deffo!
    • @bmoneruxui: 看最后一个 sn-p :)
    猜你喜欢
    • 2016-02-03
    • 1970-01-01
    • 2017-09-13
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多