情不知从何起,一往而情深
排名
6
文章
199
粉丝
4
评论
3
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

css 如何实现div边框四个角的颜色不一样

706人阅读 2024/9/11 14:19 总访问:1181594 评论:0 收藏:0 手机
分类: 前端

可以伪元素和其他div来创建看起来像是边框的元素,并分别为它们设置不同的背景色,模拟四个角有不同颜色的效果。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .div-with-different-corners {
  9. position: relative;
  10. width: 200px;
  11. height: 50px;
  12. background-color: #abcdff;
  13. }
  14. /* 左上角 */
  15. .div-with-different-corners::before {
  16. content: '';
  17. position: absolute;
  18. top: 0;
  19. left: 0;
  20. width: 25px;
  21. /* 或者你想要的任何尺寸 */
  22. height: 5px;
  23. /* 或者你想要的任何尺寸 */
  24. background-color: red;
  25. /* 上左角颜色 */
  26. z-index: 10;
  27. }
  28. /* 右上角 */
  29. .div-with-different-corners::after {
  30. content: '';
  31. position: absolute;
  32. top: 0px;
  33. right: 0px;
  34. width: 25px;
  35. /* 或者你想要的任何尺寸 */
  36. height: 5px;
  37. /* 或者你想要的任何尺寸 */
  38. background-color: blue;
  39. /* 上右角颜色 */
  40. /* z-index: 10; */
  41. /* transform: rotate(90deg); */
  42. /* 旋转角度,使其看起来像是右上角 */
  43. }
  44. /* 左下角 */
  45. .leftbottom_border {
  46. content: '';
  47. position: absolute;
  48. left: 0;
  49. bottom: 0;
  50. width: 25px;
  51. /* 或者你想要的任何尺寸 */
  52. height: 5px;
  53. /* 或者你想要的任何尺寸 */
  54. background-color: purple;
  55. /* 上左角颜色 */
  56. z-index: 10;
  57. }
  58. /* 右下角 */
  59. .rightbottom_border {
  60. content: '';
  61. position: absolute;
  62. right: 0;
  63. bottom: 0;
  64. width: 25px;
  65. /* 或者你想要的任何尺寸 */
  66. height: 5px;
  67. /* 或者你想要的任何尺寸 */
  68. background-color: palevioletred;
  69. /* 上左角颜色 */
  70. z-index: 10;
  71. }
  72. </style>
  73. </head>
  74. <body>
  75. <div class="div-with-different-corners">
  76. <div class="leftbottom_border"></div>
  77. <div class="rightbottom_border"></div>
  78. </div>
  79. </body>
  80. </html>

这里之所以还要新家两个div,是因为伪类不要创建四个元素出来,只用伪类实现了两个,然后自己写了两个。

效果图:


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价