• 【CSS】CSS实现三角形(二)


    在上一篇文章: 

    提到了两种方式实现了三角形:

    • border边框
    • linear-gradient渐变

    本文将通过第三种方式:使用伪元素的来实现气泡三角形。

    1、实心气泡

    其实,严格来说,是通过 border+伪元素 实现的,具体的可以参考以下代码:

    1. /* 气泡三角 */
    2. .triangle {
    3. width: 100px;
    4. height: 50px;
    5. background: #abc88b;
    6. border-radius: 5px;
    7. position: relative;
    8. }
    9. /* 上 */
    10. .triangle1:before {
    11. content: "";
    12. width: 0px;
    13. height: 0px;
    14. border-bottom: 8px solid #abc88b;
    15. border-left: 10px solid transparent;
    16. border-right: 10px solid transparent;
    17. position: absolute;
    18. top: -8px;
    19. left: 40px;
    20. }
    21. /* 下 */
    22. .triangle2:before {
    23. content: "";
    24. width: 0px;
    25. height: 0px;
    26. border-top: 10px solid #abc88b;
    27. border-left: 10px solid transparent;
    28. border-right: 10px solid transparent;
    29. position: absolute;
    30. bottom: -10px;
    31. left: 40px;
    32. }
    33. /* 左 */
    34. .triangle3:before {
    35. content: "";
    36. width: 0px;
    37. height: 0px;
    38. border-right: 10px solid #abc88b;
    39. border-top: 10px solid transparent;
    40. border-bottom: 10px solid transparent;
    41. position: absolute;
    42. top: 15px;
    43. left: -10px;
    44. }
    45. /* 右 */
    46. .triangle4:before {
    47. content: "";
    48. width: 0px;
    49. height: 0px;
    50. border-left: 10px solid #abc88b;
    51. border-top: 10px solid transparent;
    52. border-bottom: 10px solid transparent;
    53. position: absolute;
    54. top: 15px;
    55. right: -10px;
    56. }

    效果图如下:

     有没有觉得很眼熟,这不就跟border直接实现差不多吗?不过是换了一种写法而已。

    相比于直接使用border,使用伪元素不需要额外再去创建一个div,在一个div上就可以搞定。

    2、空心气泡

    实现了实心气泡之后,如果想要空心的要怎么搞呢?

    首先,肯定得把背景色去了加一个border;

    border: 1px solid #abc88b;

    其次,我需要一个三角形覆盖住我的实心小三角。

    那我前面加了一个伪元素before,我可以再加一个after嘛,after是一个白色小三角,只需要比有颜色的三角形小一个px就可以解决啦。

    完整的代码如下:

    1. .triangle {
    2. width: 100px;
    3. height: 50px;
    4. border: 1px solid #abc88b;
    5. border-radius: 5px;
    6. position: relative;
    7. }
    8. /* 上 */
    9. .triangle1:before {
    10. content: "";
    11. width: 0px;
    12. height: 0px;
    13. border-bottom: 8px solid #abc88b;
    14. border-left: 10px solid transparent;
    15. border-right: 10px solid transparent;
    16. position: absolute;
    17. top: -8px;
    18. left: 40px;
    19. }
    20. .triangle1:after {
    21. content: "";
    22. width: 0px;
    23. height: 0px;
    24. border-bottom: 7px solid #ffffff;
    25. border-left: 9px solid transparent;
    26. border-right: 9px solid transparent;
    27. position: absolute;
    28. top: -7px;
    29. left: 41px;
    30. }
    31. /* 下 */
    32. .triangle2:before {
    33. content: "";
    34. width: 0px;
    35. height: 0px;
    36. border-top: 10px solid #abc88b;
    37. border-left: 10px solid transparent;
    38. border-right: 10px solid transparent;
    39. position: absolute;
    40. bottom: -10px;
    41. left: 40px;
    42. }
    43. .triangle2:after {
    44. content: "";
    45. width: 0px;
    46. height: 0px;
    47. border-top: 9px solid #ffffff;
    48. border-left: 9px solid transparent;
    49. border-right: 9px solid transparent;
    50. position: absolute;
    51. bottom: -9px;
    52. left: 41px;
    53. }
    54. /* 左 */
    55. .triangle3:before {
    56. content: "";
    57. width: 0px;
    58. height: 0px;
    59. border-right: 10px solid #abc88b;
    60. border-top: 10px solid transparent;
    61. border-bottom: 10px solid transparent;
    62. position: absolute;
    63. top: 15px;
    64. left: -10px;
    65. }
    66. .triangle3:after {
    67. content: "";
    68. width: 0px;
    69. height: 0px;
    70. border-right: 9px solid #ffffff;
    71. border-top: 9px solid transparent;
    72. border-bottom: 9px solid transparent;
    73. position: absolute;
    74. top: 16px;
    75. left: -9px;
    76. }
    77. /* 右 */
    78. .triangle4:before {
    79. content: "";
    80. width: 0px;
    81. height: 0px;
    82. border-left: 10px solid #abc88b;
    83. border-top: 10px solid transparent;
    84. border-bottom: 10px solid transparent;
    85. position: absolute;
    86. top: 15px;
    87. right: -10px;
    88. }
    89. .triangle4:after {
    90. content: "";
    91. width: 0px;
    92. height: 0px;
    93. border-left: 9px solid #ffffff;
    94. border-top: 9px solid transparent;
    95. border-bottom: 9px solid transparent;
    96. position: absolute;
    97. top: 16px;
    98. right: -9px;
    99. }

    效果图如下:

     

     OK,掌握了这几种方法之后,实现三角形应该是问题不大了,后续会补充其他方式哦!

  • 相关阅读:
    大数据之SparkSQL 完整使用 (第八章)
    关于#c语言#的问题:力扣求长度最小的数组
    Hadoop 3.x(入门)----【Hadoop概述】
    解决方案 | 如何构建市政综合管廊安全运行监测系统?
    基于canvas实现的多功能画板
    java毕业设计宠物找家系统mybatis+源码+调试部署+系统+数据库+lw
    机器学习和深度学习 -- 李宏毅(笔记与个人理解)Day19
    Camera/Lens流程(1)——点亮
    Pytest 的高级用法之 插件开发
    JavaSE 第十章 集合
  • 原文地址:https://blog.csdn.net/weixin_38629529/article/details/126443087