响应式CSS三角形,百分比宽度

前端之家收集整理的这篇文章主要介绍了响应式CSS三角形,百分比宽度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下面的代码将在< a>元件:

JSFiddle

  1. .btn {
  2. position: relative;
  3. display: inline-block;
  4. width: 100px;
  5. height: 50px;
  6. text-align: center;
  7. color: white;
  8. background: gray;
  9. line-height: 50px;
  10. text-decoration: none;
  11. }
  12. .btn:after {
  13. content: "";
  14. position: absolute;
  15. bottom: -10px;
  16. left: 0;
  17. width: 0;
  18. height: 0;
  19. border-width: 10px 50px 0 50px;
  20. border-style: solid;
  21. border-color: gray transparent transparent transparent;
  22. }
  1. <a href="#" class="btn">Hello!</a>

问题是我们必须指出链接宽度以获得适当大小的箭头,因为我们无法指出边框宽度(以像素为单位)。

如何使基于百分比的响应三角形?

解决方法

您可以使用偏斜和旋转的伪元素在链接下创建响应三角形:

DEMO(调整结果窗口大小,看看它如何反应)

三角形的宽高比与padding-bottom属性保持一致。

如果您希望形状根据其内容适应其大小,则可以删除.btn类的宽度

  1. .btn {
  2. position: relative;
  3. display: inline-block;
  4. height: 50px; width: 50%;
  5. text-align: center;
  6. color: white;
  7. background: gray;
  8. line-height: 50px;
  9. text-decoration: none;
  10. padding-bottom: 15%;
  11. background-clip: content-Box;
  12. overflow: hidden;
  13. }
  14. .btn:after {
  15. content: "";
  16. position: absolute;
  17. top:50px; left: 0;
  18. background-color: inherit;
  19. padding-bottom: 50%;
  20. width: 57.7%;
  21. z-index: -1;
  22. -webkit-transform-origin: 0 0;
  23. -ms-transform-origin: 0 0;
  24. transform-origin: 0 0;
  25. -webkit-transform: rotate(-30deg) skewX(30deg);
  26. -ms-transform: rotate(-30deg) skewX(30deg);
  27. transform: rotate(-30deg) skewX(30deg);
  28. }
  29. /** FOR THE DEMO **/
  30.  
  31. body {
  32. background: url('http://i.imgur.com/qi5FGET.jpg');
  33. background-size: cover;
  34. }
  1. <a href="#" class="btn">Hello!</a>

有关响应三角形的更多信息以及如何制作它们,您可以看一下
Triangles with transform rotate(简单而花式的三角形)

猜你在找的CSS相关文章