简体   繁体   English

使用CSS设置工具提示箭头的边框颜色

[英]Set Border Color of Tooltip Arrow with CSS

I was trying to set border colour white of my tooltip's arrow key. 我试图将工具提示的箭头键的边框颜色设置为白色。 I was able to set tooltips bottom border colour white, how can I set bottom border colour of arrow too? 我可以将工具提示的底部边框颜色设置为白色,也可以如何设置箭头的底部边框颜色? Here is what I tried: 这是我尝试过的:

 .hint { position: relative; display: inline-block; } .hint:before, .hint:after { position: absolute; opacity: 0; z-index: 100; -webkit-transition: 0.3s ease; -moz-transition: 0.3s ease; pointer-events: none; } .hint:hover:before, .hint:hover:after { opacity: 1; } .hint:before { content: ''; position: absolute; background: transparent; border: 6px solid transparent; position: absolute; transform: translateX(-50%); } .hint:after { content: attr(data-hint); background: rgba(0, 0, 0, 0.8); border-bottom-left-radius: 20px; border-top-right-radius: 20px; color: white; padding: 8px 10px; font-size: 16px; font-style: italic; white-space: nowrap; box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.4); transform: translateX(-50%); } /* top mouse-icon*/ .hint--top:before { bottom: 100%; left: 50%; margin: 0 0 -18px 0; border-top-color: rgba(0, 0, 0, 0.8); } .hint--top:after { bottom: 100%; left: 50%; margin: 0 0 -6px -10px; } /*arrow key*/ .hint--top:hover:before { margin-bottom: -12px; border-width: 12px; /*margin-left: -56px;*/ border-color: rgba(194, 225, 245, 0); border-top-color: rgba(0, 0, 0, 0.8); } .hint--top:hover:after { margin-bottom: 12px; border-width: 10px; /*margin-left: -50px;*/ border-top-color: rgba(0, 0, 0, 0.8); border-bottom: 1px solid #f0f0f0; } 
 <br /><br /><br /><br /> <div class="hint hint--top" data-hint="May Peace, Mercy and Blessings of Allah be Upon You" class="intro-sub"> <a>Assalaamu 'Alaikum</a> </div> 

For clarification, I am including sample picture here. 为了澄清起见,我在此处包括示例图片。 Right now I have this: 现在我有这个:

在此处输入图片说明

But I need something like the given image below: 但是我需要下面的给定图像:

在此处输入图片说明

First of all your html is very strange. 首先,您的html非常奇怪。 Adding class attribute twice on an element is pointless. 在元素上添加class属性两次是毫无意义的。 Also the br are not good practice for making space.. Use margins instead or something else. 另外, br也不是腾出空间的好习惯。.使用边距代替。

For your problem, you need another triangle with red border positioned under your black triangle. 对于您的问题,您需要在黑色三角形下方放置另一个带有红色边框的三角形。 This red triangle should be bigger. 这个红色三角形应该更大。

Then, the small black triangle should have a z-index bigger than your tooltip so it will cover the red border of the tooltip 然后,黑色小三角形的z索引应大于工具提示,以便覆盖工具提示的红色边框

I added all the new code at the beginning of the snippet. 我在代码段的开头添加了所有新代码。 See below 见下文

You need to change a little bit the animation. 您需要稍微更改动画。 But it should be easy 但这应该很容易

  .border-triangle { top: -20px; left: 50%; transform: translateX(-50%); width: 0; position: absolute; height: 0; border-left: 20px solid transparent; border-right: 20px solid transparent; opacity: 0; transition: 0.3s ease-out; border-top: 20px solid red; } .hint:hover .border-triangle { opacity: 1; } .hint { position: relative; display: inline-block; } .hint:before, .hint:after { position: absolute; opacity: 0; z-index: 100; -webkit-transition: 0.3s ease; -moz-transition: 0.3s ease; pointer-events: none; } .hint:hover:before, .hint:hover:after { opacity: 1; } .hint:before { content: ''; position: absolute; background: transparent; border: 6px solid transparent; position: absolute; transform: translateX(-50%); } .hint:after { content: attr(data-hint); background: rgba(0, 0, 0, 1); border-bottom-left-radius: 20px; border-top-right-radius: 20px; color: white; padding: 8px 10px; font-size: 16px; font-style: italic; white-space: nowrap; box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.4); transform: translateX(-50%); } /* top mouse-icon*/ .hint--top:before { bottom: 100%; left: 50%; margin: 0 0 -18px 0; border-top-color: rgba(0, 0, 0, 0.8); } .hint--top:after { bottom: 100%; left: 50%; margin: 0 0 -6px -10px; z-index: 1; } /*arrow key*/ .hint--top:hover:before { margin-bottom: -10px; border-width: 12px; /*margin-left: -56px;*/ border-color: rgba(194, 225, 245, 0); border-top-color: rgba(0, 0, 0, 1); z-index: 2; } .hint--top:hover:after { margin-bottom: 12px; border-width: 10px; /*margin-left: -50px;*/ border-top-color: rgba(0, 0, 0, 0.8); border-bottom: 2px solid red; } 
 <br> <br> <br> <div class="hint hint--top" data-hint="May Peace, Mercy and Blessings of Allah be Upon You" class="intro-sub"> <!-- intro-sub second class is usless --> <div class="border-triangle"> </div> <a>Assalaamu 'Alaikum</a> </div> 

I had struggled to draw triangle before, too. 我以前也很难画三角形。 Almost tricks such as here and here dealing with triangles in HTML handles border. 几乎所有的技巧,例如在这里这里处理HTML中的三角形的句柄边框。 So painting borders of triangles is impossible with this kind of tricks. 因此,用这种技巧不可能画出三角形的边界。

One alternative is to rotate a square and hide a half. 一种选择是旋转正方形并隐藏一半。 The advantage of this method is that drawing border is easy, but I didn't find super clean method to hide half of the square. 这种方法的优点是绘制边框很容易,但是我没有找到超级干净的方法来隐藏正方形的一半。 So when the tooltip or the triangle have opacity less than 1, this is not a desirable method. 因此,当工具提示或三角形的不透明度小于1时,这不是理想的方法。

Maybe this is what you want. 也许这就是您想要的。 Jsfiddle.net demo is here . Jsfiddle.net演示在这里

 .hint { position: relative; display: inline-block; margin-top: 40px; } .hint:before, .hint:after { position: absolute; opacity: 0; -webkit-transition: 0.3s ease; -moz-transition: 0.3s ease; pointer-events: none; } .hint:hover:before, .hint:hover:after { opacity: 1; } .hint:before { content: ''; position: absolute; background: rgba(0, 0, 0, 1); width: 6px; height: 6px; transform: translateX(-50%) rotate(45deg); z-index: 2; } .hint:after { content: attr(data-hint); background: rgba(0, 0, 0, 1); border-bottom-left-radius: 20px; border-top-right-radius: 20px; color: white; padding: 8px 10px; font-size: 16px; font-style: italic; white-space: nowrap; box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.4); transform: translateX(-50%); z-index: 1; } /* top mouse-icon*/ .hint--top:before { bottom: 100%; left: 50%; margin: 0 0 -12px 0; } .hint--top:after { bottom: 100%; left: 50%; margin: 0 0 -6px -10px; } /*arrow key*/ .hint--top:hover:before { margin-bottom: 0px; /* border-width: 2px; */ width: 24px; height: 24px; /* margin-left: -56px; */ border: solid white; border-width: 0 1px 1px 0; border-top-left-radius: 100%; box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.4); /* border-top-color: rgba(0, 0, 0, 0.8); */ } .hint--top:hover:after { margin-bottom: 12px; border-width: 10px; /*margin-left: -50px;*/ border-top-color: rgba(0, 0, 0, 0.8); border-bottom: 1px solid #f0f0f0; } 
 <div class="hint hint--top" data-hint="May Peace, Mercy and Blessings of Allah be Upon You" class="intro-sub"> <a>Assalaamu 'Alaikum</a> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM