资讯专栏INFORMATION COLUMN

11种方法实现一个tips带有描边的小箭头(更新中...)

李涛 / 2309人阅读

摘要:查看效果第六种和背景描边旋转此方法就是设置一个宽度和高度分别为的方块背景,然后背景相邻的两条边描边再有的属性旋转角度,来实现箭头的朝向。

我们在网页开发中实现一个tips时会有一个小箭头,实现这种方法的文章网上已经泛滥了,但有时实现这个小箭头不止只有单纯的三角它还有描边,今天我们就借那些现有的文章在深入一点来说说如何给tips小箭头描边,本章不涉及svg/canvas,没必要因为我讲的是css。

主体样式:

</>复制代码

  1. .dui-tips{
  2. position: relative;
  3. padding: 10px;
  4. text-align: center;
  5. border: 1px solid #f60;
  6. border-radius: 5px;
  7. background-color: #fff;
  8. }
第一种border描边双层覆盖:

就是大家常用的border,实现原理就是给其中一条边设置颜色宽度及样式,我这里使用了两个伪类进行折叠,将一个白色的覆盖在有颜色的伪类上面,再偏移1px来模拟实现1px的描边效果,代码如下:

</>复制代码

  1. .dui-tips {
  2. &:before, &:after {
  3. position: absolute;
  4. left: 50%;
  5. display: table;
  6. width: 0;
  7. height: 0;
  8. content: "";
  9. transform: translate(-50%, 0);
  10. border-width: 10px 10px 0 10px;
  11. border-style: solid;
  12. }
  13. &:before {
  14. z-index: 0;
  15. bottom: -10px;
  16. border-color: #f60 transparent transparent transparent;
  17. }
  18. &:after {
  19. z-index: 1;
  20. bottom: -8px;
  21. border-color: #fff transparent transparent transparent;
  22. }
  23. }

查看效果

第二种border描边结合滤镜drop-shadow属性:

第二种是第一种的延伸,使用滤镜filter的drop-shadow描边来实现,box-shadow和drop-shadow实现不规则投影

</>复制代码

  1. .dui-tips {
  2. &:after {
  3. position: absolute;
  4. left: 50%;
  5. display: table;
  6. width: 0;
  7. height: 0;
  8. content: "";
  9. transform: translate(-50%, 0);
  10. border-width: 10px 10px 0 10px;
  11. border-style: solid;
  12. bottom: -9px;
  13. border-color: #fff transparent transparent transparent;
  14. filter: drop-shadow(0px 2px 0px #f60);
  15. }
  16. }

查看效果

第三种通过特殊符号或字体双层覆盖

第三种方法和第一种类似,通过两层颜色叠加在有层级的偏移来实现

</>复制代码

  1. .dui-tips {
  2. &:before, &:after {
  3. font-size: 24px;
  4. line-height: 18px;
  5. position: absolute;
  6. left: 50%;
  7. display: table;
  8. content: "◆";
  9. transform: translate(-50%, 0);
  10. text-align: center;
  11. }
  12. &:before {
  13. z-index: 0;
  14. bottom: -10px;
  15. color: #f60;
  16. }
  17. &:after {
  18. z-index: 1;
  19. bottom: -8px;
  20. color: #fff;
  21. }
  22. }

查看效果

第四种通过text-shadow实现

这种放发通过给文子设置1px的阴影来显描边效果

</>复制代码

  1. .dui-tips {
  2. &:after {
  3. font-size: 24px;
  4. line-height: 18px;
  5. position: absolute;
  6. left: 50%;
  7. display: table;
  8. content: "◆";
  9. transform: translate(-50%, 0);
  10. text-align: center;
  11. z-index: 1;
  12. bottom: -8px;
  13. color: #fff;
  14. text-shadow: 0 2px 0 #f60;
  15. }
  16. }

查看效果

第五种 background双层覆盖

这种方式设置两个宽度和高度分别为10px的方块背景,再给两层背景分别设置不同的颜色,再通过两层背景颜色叠加,经过层级偏移再有transform的rotate属性旋转角度,来实现箭头的朝向。

</>复制代码

  1. .dui-tips {
  2. &:before, &:after {
  3. position: absolute;
  4. left: 50%;
  5. display: table;
  6. width: 10px;
  7. height: 10px;
  8. content: "";
  9. margin-left: -5px;
  10. transform: rotate(-45deg);
  11. }
  12. &:before {
  13. z-index: 0;
  14. bottom: -6px;
  15. background-color: #f60;
  16. }
  17. &:after {
  18. z-index: 1;
  19. bottom: -5px;
  20. background-color: #fff;
  21. }
  22. }

查看效果

第六种background和border背景描边旋转

此方法就是设置一个宽度和高度分别为10px的方块背景,然后背景相邻的两条边描边再有transform的rotate属性旋转角度,来实现箭头的朝向。

</>复制代码

  1. .dui-tips {
  2. &:after {
  3. position: absolute;
  4. left: 50%;
  5. display: table;
  6. width: 10px;
  7. height: 10px;
  8. margin-left: -5px;
  9. content: "";
  10. transform: rotate(-45deg);
  11. z-index: 1;
  12. bottom: -6px;
  13. border-bottom: 1px solid #f60;
  14. border-left: 1px solid #f60;
  15. background-color: #fff;
  16. }
  17. }

查看效果

第七种background和box-shadow

</>复制代码

  1. .dui-tips {
  2. &:after {
  3. position: absolute;
  4. left: 50%;
  5. display: table;
  6. width: 10px;
  7. height: 10px;
  8. content: "";
  9. margin-left: -5px;
  10. transform: rotate(-45deg);
  11. z-index: 1;
  12. bottom: -5px;
  13. background-color: #fff;
  14. box-shadow: -1px 1px 0 #f60;
  15. }
  16. }

查看效果

第八种linear-gradient

</>复制代码

  1. .dui-tips{
  2. &:before, &:after{
  3. position: absolute;
  4. left: 50%;
  5. display: table;
  6. width: 10px;
  7. height: 10px;
  8. content: "";
  9. margin-left: -5px;
  10. transform: rotate(-135deg);
  11. }
  12. &:before {
  13. z-index: 0;
  14. bottom: -6px;
  15. background: linear-gradient(-45deg, transparent 7px, #f60 0);
  16. }
  17. &:after {
  18. z-index: 1;
  19. bottom: -5px;
  20. background: linear-gradient(-45deg, transparent 7px, #fff 0);
  21. }
  22. }

查看效果

第九种linear-gradient和box-shadow

</>复制代码

  1. .dui-tips{
  2. &:after{
  3. position: absolute;
  4. left: 50%;
  5. display: table;
  6. width: 10px;
  7. height: 10px;
  8. content: "";
  9. margin-left: -5px;
  10. transform: rotate(-135deg);
  11. z-index: 1;
  12. bottom: -5px;
  13. background: linear-gradient(-45deg, transparent 7px, #fff 0);
  14. box-shadow: -1px -1px 0 #f60
  15. }
  16. }

查看效果

第十种linear-gradient和border

</>复制代码

  1. .dui-tips{
  2. &:after{
  3. position: absolute;
  4. left: 50%;
  5. display: table;
  6. width: 10px;
  7. height: 10px;
  8. content: "";
  9. margin-left: -5px;
  10. transform: rotate(-135deg);
  11. z-index: 1;
  12. bottom: -6px;
  13. background: linear-gradient(-45deg, transparent 7px, #fff 0);
  14. border-top: 1px solid #f60;
  15. border-left: 1px solid #f60;
  16. }
  17. }

查看效果

第十一种ouline

</>复制代码

  1. .dui-tips {
  2. &:before, &:after {
  3. position: absolute;
  4. left: 50%;
  5. display: table;
  6. width: 0;
  7. height: 0;
  8. content: "";
  9. transform: rotate(45deg);
  10. outline-style: solid;
  11. outline-width: 5px;
  12. }
  13. &:before {
  14. z-index: 0;
  15. bottom: -1px;
  16. outline-color: #f60;
  17. }
  18. &:after {
  19. z-index: 1;
  20. bottom: 0;
  21. outline-color: #fff;
  22. }
  23. }

查看效果

</>复制代码

  1. 作者: w3cbest前端开发
    互动: 如有疑问可进群讨论
    本文原创,著作权归作者所有。商业转载请联系@w3cbest前端开发获得授权,非商业转载请注明原链接及出处。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/53477.html

相关文章

  • 11方法实现一个tips带有描边的小箭头更新...)

    摘要:查看效果第六种和背景描边旋转此方法就是设置一个宽度和高度分别为的方块背景,然后背景相邻的两条边描边再有的属性旋转角度,来实现箭头的朝向。 showImg(https://segmentfault.com/img/bVbmX2F?w=400&h=277); 我们在网页开发中实现一个tips时会有一个小箭头,实现这种方法的文章网上已经泛滥了,但有时实现这个小箭头不止只有单纯的三角它还有描边...

    oneasp 评论0 收藏0
  • 使用SVG + CSS实现动态霓虹灯文字效果

    摘要:早上无意间进入一个网站,看到他们的效果略屌,如图刚开始以为是动画之类的,审查元素发现居然是用动画实现的,顿时激起了我的欲望,决定要一探究竟,查看代码之后,发现原理居然是如此简单多个描边动画使用不同的即可对于一个形状元素或文本元素,可以使用 早上无意间进入一个网站,看到他们的LOGO效果略屌,如图:showImg(https://segmentfault.com/img/bVT9At?w...

    IntMain 评论0 收藏0
  • canvas基础知识点(一)

    摘要:给设置宽高标签的宽高默认是是一个行内块元素可以在标签上通过,来设置可以在中给对象设置注意不要通过来调整的宽高导致内部的画布被拉伸,图形变形获取画笔工具绘图都是通过标签的画笔来进行的注意,不要写成,里面传入的参数目前也只有这一种情况描边和填充 给canvas设置宽高: canvas标签的宽高默认是300*150,是一个行内块元素 可以在canvas标签上通过width,height...

    ephererid 评论0 收藏0

发表评论

0条评论

李涛

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<