html - 带有 CSS 梯形形状按钮的阴影

标签 html css button shadow

我用过this创建按钮的问题。但是当我尝试为按钮创建左下角阴影时,白色区域将显示为:

.btn {
   height: 40px;
   background: red;
   width: 128px;
   margin: 15px 5px 15px 5px;
   cursor: hand;
   text-align: center;
   vertical-align: middle;
   line-height: 40px;
   -webkit-box-shadow: 2px 3px 3px #666666;
   -moz-box-shadow: 2px 3px 3px #666666;
   box-shadow: 2px 3px 3px #666666;
}

.btn:before {
   width: 0px;
   height: 20px;
   border-left:  20px solid red;
   border-top: 20px solid white;
   float:right;
   content:"";
 }

.btn:hover{
  -webkit-box-shadow: 1px 1px 1px #666666;
  -moz-box-shadow: 1px 1px 1px #666666;
  box-shadow: 1px 1px 1px #666666;
}
.userNave{
  width: 140px;
}
<nav class="userNave">
    <div class="btn" 
         onClick="alert('Hi')" 
         style="">Click Me Me</div>

  
    <div class="btn" 
         onClick="alert('Hello')" 
         style="">No Click Me </div>
    
</nav>

有什么解决办法吗?甚至更好。有什么方法可以创建一个真正的梯形按钮,使其可以与阴影一起使用并且背景匹配不会有问题。

最佳答案

这是我能想到的最好的办法,使用伪元素作为背景。

.btn {
  position: relative;
  height: 40px;
  width: 128px;
  margin: 15px 5px 15px 5px;
  padding: 0 10px 5px 0;
  cursor: hand;
  text-align: center;
  vertical-align: middle;
  line-height: 40px;
  overflow: hidden;
}
.btn:before {
  position: absolute;
  left: -23px; top: 0;
  width: calc(100% - 5px);
  height: 50%;
  background: red;
  content: "";
  z-index: -1;
  transform: skewX(45deg);
  transform-origin: left top;
  box-shadow: 0px 1px 3px 1px #666666;
}
.btn:after {
  position: absolute;
  left: 0; top: 50%;
  width: calc(100% - 5px);
  height: calc(50% - 5px);
  background: red;
  content: "";
  z-index: -1;
  box-shadow: 2px 2px 4px #666666;
}


.userNave {
  width: 140px;
}
<nav class="userNave">
  <div class="btn" onClick="alert('Hi')" style="">Click Me Me</div>
  <div class="btn" onClick="alert('Hello')" style="">No Click Me</div>
</nav>


SVG 图片很可能是更好的选择。

.btn {
  position: relative;
  height: 40px;
  width: 128px;
  margin: 15px 5px 15px 5px;
  padding: 0 0 5px 0;
  cursor: hand;
  text-align: center;
  vertical-align: middle;
  line-height: 40px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' id='trapezoid' viewbox='0 0 118 45' preserveAspectRatio='none'%3E %3Cfilter id='dropshadow' height='130%25'%3E %3CfeGaussianBlur in='SourceAlpha' stdDeviation='3'/%3E %3C!-- stdDeviation is how much to blur --%3E %3CfeOffset dx='2' dy='2' result='offsetblur'/%3E %3C!-- how much to offset --%3E %3CfeMerge%3E %3CfeMergeNode/%3E %3C!-- this contains the offset blurred image --%3E %3CfeMergeNode in='SourceGraphic'/%3E %3C!-- this contains the element that the filter is applied to --%3E %3C/feMerge%3E %3C/filter%3E %3Cpath d='M0,0 L100,0 L120,20 L120,40 L0,40z' fill='red' style='filter:url(%23dropshadow)'%3E%3C/path%3E %3C/svg%3E");
}

.userNave {
  width: 140px;
}
<nav class="userNave">
  <div class="btn" onClick="alert('Hi')" style="">Click Me Me</div>
  <div class="btn" onClick="alert('Hello')" style="">No Click Me</div>
</nav>

关于html - 带有 CSS 梯形形状按钮的阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43450999/

相关文章:

jquery - 对话框关闭按钮颜色更改

html - HTML 中按钮的隐藏属性

html - 垂直对齐图像上的标题文本

javascript - 我如何找到 HTML5 音频实际开始播放的时间(onplay 事件似乎在任何地方都不起作用)

javascript - 添加 Javascript 来替换 Span 标签

javascript - Plotly.js 模式栏将图标分组并将其放置在图例上

c# - 查找 POST 参数

HTML 表单 : use dl, dt, dd 用于布局表单输入,但会被下拉菜单扭曲

html - 将自己的 ui 组件提取到一个地方

javascript - 单击按钮不起作用。我关注了一个 YouTube 视频,但它对我不起作用