带有CSS的圆形标签形状

bli*_*liw 5 html css shapes css-shapes

我试图用html/css创建这个形状:

在此输入图像描述

我的要求是:

  • 正文背景可能会每页更改,因此无法使用Alpha遮罩图像
  • 背景颜色在悬停时可以转换
  • 形状的所有角都是圆的

有了尖角,你可以用css三角形来做,但圆角的那些给我带来麻烦.

到目前为止我在HTML中有什么:

<ul class="tags">
  <li><a href="#">Lorem</a></li>
  <li><a href="#">Ipsum</a></li>
  <li><a href="#">Longer text in tag</a></li>
  <li><a href="#">Dolor</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

在CSS中:

.tags {
  list-style: none;
  margin: 0;
  padding: 1em;
}
.tags li {
    display: inline-block;
    margin-right: 2em;
    position: relative;
}
.tags a {
    background: #283c50;
    color: #fff;
    display: block;
    line-height: 1em;
    padding: 0.5em;
    text-decoration: none;

    -webkit-border-radius: 10px 0 0 10px;
    -moz-border-radius: 10px 0 0 10px;
    border-radius: 10px 0 0 10px;

    -webkit-transition: background 200ms ease;
    -moz-transition: background 200ms ease;
    -o-transition: background 200ms ease;
    -ms-transition: background 200ms ease;
    transition: background 200ms ease;
}
.tags a:before {
    background: #283c50;
    content: "";
    height: 1.75em;
    width: 1.75em;
    position: absolute;
    top: 0.1em;
    right: -0.87em;
    z-index: -1;

    -webkit-transform: rotate(40deg);
    -moz-transform: rotate(40deg);
    -o-transform: rotate(40deg);
    -ms-transform: rotate(40deg);
    transform: rotate(40deg);

    -webkit-border-radius: 0.625em;
    -moz-border-radius: 0.625em;
    -o-border-radius: 0.625em;
    -ms-border-radius: 0.625em;
    border-radius: 0.625em;

    -webkit-transition: background 200ms ease;
    -moz-transition: background 200ms ease;
    -o-transition: background 200ms ease;
    -ms-transition: background 200ms ease;
    transition: background 200ms ease;
}
.tags a:hover,
.tags a:hover:before {
    background: #1eaa82;
}
Run Code Online (Sandbox Code Playgroud)

它只在Chrome上看起来不错.对于其他人来说,三角形位置存在差异,或者在三角形和实际标签的不同时间发生过渡.参见示例http://jsfiddle.net/hfjMk/1/

这甚至可行/可行吗?或者我是否必须放弃过渡并使用图像作为三角形部分?

jbu*_*483 5

您可以使用旋转的伪元素:

div {
  height: 50px;
  width: 150px;
  border-radius: 10px 0 0 10px;
  position: relative;
  background: tomato;
  text-align:center;
  line-height:50px;
}
div:before {
  content: "";
  position: absolute;
  top: -4px;
  right: -41px;
  height: 41px;
  width: 41px;
  transform-origin: top left;
  transform: rotate(45deg);
  background: tomato;
  border-radius: 10px;
}
Run Code Online (Sandbox Code Playgroud)
<div>Text</div>
Run Code Online (Sandbox Code Playgroud)


Sid*_*Ofc 0

目前最好的选择是使用现有的,因为这在大多数浏览器中看起来都很好。此外,我建议您可以使用浏览器嗅探器(非常危险)来修复按钮跨浏览器中的文本定位。

可以选择使用源图像,基本上形状将是透明的而不是背景。

在这种情况下,您可以根据需要更改 A 标签的背景,只需设置图像颜色以匹配页面布局的其余部分即可。

目前我能想到的就这么多了,希望对你有用。