【问题标题】:Arrow button with vertical gradient background具有垂直渐变背景的箭头按钮
【发布时间】:2016-07-26 16:51:14
【问题描述】:

我需要制作这个按钮:

通常当我需要制作带有渐变和边框的箭头按钮时,我会添加具有绝对定位的.button:before 元素,使用transform: rotate(-45deg) translate(%SOMETHING%); 并添加background: linear-gradient(45deg, %COLORS%);。但是现在我需要制作按钮,箭头角度不对。我该怎么做?

【问题讨论】:

  • 为什么你也不能使用 :before 作为按钮。它会起作用的,对吧?

标签: html css css-transforms


【解决方案1】:

您可以使用:before 选择器设计此按钮:

.button {
   width: 120px;
   height: 50px;   
   position: relative;
   -moz-border-radius:    5px;
   -webkit-border-radius: 5px;
   border-radius:         5px;
   border:1px solid #4d7a9c; 
   position:relative;
   color:white;
   font-size:18px;
   
   background: #238fe7; /* Old browsers */
  background: -moz-linear-gradient(top,  #238fe7 0%, #156fba 100%); /* FF3.6-15 */
  background: -webkit-linear-gradient(top,  #238fe7 0%,#156fba 100%); /* Chrome10-25,Safari5.1-6 */
  background: linear-gradient(to bottom,  #238fe7 0%,#156fba 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#238fe7', endColorstr='#156fba',GradientType=0 ); /* IE6-9 */ 
 }

.button:before {
  content: "";
  position: absolute;
  transform: scaleX(0.6) rotate(45deg);
  height: 38px;
  width: 38px;
  right:-18px;
  top:5px;
  border-radius:  5px;
  z-index:-1px;
  
  background: #238fe7; /* Old browsers */
  background: -moz-linear-gradient(-45deg,  #238fe7 0%, #156fba 100%); /* FF3.6-15 */
  background: -webkit-linear-gradient(-45deg,  #238fe7 0%,#156fba 100%); /* Chrome10-25,Safari5.1-6 */
  background: linear-gradient(135deg,  #238fe7 0%,#156fba 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#238fe7', endColorstr='#156fba',GradientType=1 ); /* IE6-9 fallback on horizontal gradient */
}
<button class="button">Text</button>

【讨论】:

  • 我印象深刻。对于这样的按钮,我什至不会尝试纯 CSS 解决方案。我将直接使用 SVG,因为我无法想象 CSS 解决方案甚至会像您在此处实现的那样在角半径上实现一致性。恭喜!
  • 太棒了!效果很好,我觉得加outline: none;也不错。
【解决方案2】:

你可以这样做:

#trape {
  position: absolute;
  height: 50px;
  color: white;
  width: 80px;
  border:0;
  background-image: linear-gradient(0deg, red, tan);
}

#trape:before {
  content: "";
  position: absolute;
  transform: scaleX(0.6) rotate(45deg);
  height: 35px;
  width: 35px;
  right:-18px;
  top:7px;
  background-image: linear-gradient(-45deg, red, tan);
}
<button id="trape"></button>

希望对你有帮助:)

【讨论】:

  • 你也可以在transform属性中添加scaleX(0.6)来适应箭头的角度
  • @Thinker,是的,我写过我平时用的,不过角度对了。
  • @FelixYadomi 问题出在渐变中,渐变不会和button的渐变一样
  • @FelixYadomi 忍不住将您的想法添加到 sn-p :-)
猜你喜欢
  • 2018-09-11
  • 1970-01-01
  • 2012-08-26
  • 2011-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
相关资源
最近更新 更多