gpt4 book ai didi

具有不规则矩形和边框的 CSS 自定义形状

转载 作者:行者123 更新时间:2023-12-03 18:31:24 25 4
gpt4 key购买 nike

我正在尝试创建一个这样的按钮:

enter image description here

在网上查了下,才想到做一个平行四边形。但这是结果:

enter image description here

代码:

.parallelogram {
width: 100px;
height: 50px;
transform: skew(25deg);
background: black;
border: 1px solid #EC00F4;
color: white;
box-shadow: 0px 3px 8px #EC00F4;
}
<button class="parallelogram"> Hello button </button>

有没有办法让边缘移动到我想要的位置(如图所示)但不移动文本?

最佳答案

在伪元素上使用 clip-path。诀窍是考虑相同的剪辑路径并对一个伪元素应用缩放变换以模拟边框。只需调整多边形的值即可获得所需的结果。

悬停以查看不同的剪辑路径:

.parallelogram {
padding:20px 45px;
font-size:30px;
color: white;
border:none;
background:none;
outline:none;
position:relative;
z-index:0;
margin:15px;
filter:drop-shadow(0px 30px 25px rgba(236, 0, 244, 0.45));
}
.parallelogram:before,
.parallelogram:after {
content:"";
position:absolute;
z-index:-1;
top:0;
left:0;
right:0;
bottom:0;
clip-path: polygon(0 11%, 100% 0, 90% 88%, 3% 96%);
transition:1s all;
background:#000;
}
.parallelogram:before {
background:#EC00F4;
transform:scale(1.05,1.12);
}

.parallelogram:hover:before,
.parallelogram:hover:after {
clip-path: polygon(5% 2%, 100% 5%, 100% 100%, 0% 94%);
}
<button class="parallelogram"> Hello button </button>
<button class="parallelogram"> button </button>
<button class="parallelogram"> A </button>

你也可以考虑像素值来保持相同的形状,无论里面的内容是什么:

.parallelogram {
padding:20px 45px;
font-size:30px;
color: white;
border:none;
background:none;
outline:none;
position:relative;
z-index:0;
margin:15px;
filter:drop-shadow(0px 30px 25px rgba(236, 0, 244, 0.45));
}
.parallelogram:before,
.parallelogram:after {
content:"";
position:absolute;
z-index:-1;
top:0;
left:0;
right:0;
bottom:0;
clip-path: polygon(0 10px, 100% 0, calc(100% - 8px) calc(100% - 15px), 5px calc(100% - 8px));
transition:1s all;
background:#000;
}
.parallelogram:before {
background:#EC00F4;
transform:scale(1.05,1.12);
}

.parallelogram:hover:before,
.parallelogram:hover:after {
clip-path: polygon(0 5px, 100% 0, 100% 100%, 10px calc(100% - 20px));
}
<button class="parallelogram"> Hello button </button>
<button class="parallelogram"> button </button>
<button class="parallelogram"> A </button>

关于具有不规则矩形和边框的 CSS 自定义形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59813538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com