如何创建“工具提示尾部”使用纯CSS?
我刚刚遇到了一个巧妙的 CSS 技巧:
HTML:
<div>Cool Trick:<br />
<div class="tooltiptail"></div>
</div>
<br />
<div>How do I get this effect with only CSS?<br />
<div class="anothertail"></div>
</div>
CSS:
.tooltiptail {
display: block;
border-color: #ffffff #a0c7ff #ffffff #ffffff;
border-style: solid;
border-width: 20px;
width: 0px;
height: 0px;
}
.anothertail {
background-image: url(http://static.jqueryfordesigners.com/demo/images/coda/bubble-tail2.png);
display: block;
height: 29px;
width: 30px;
}
Check out the fiddle... http://jsfiddle .net/duZAx/1/
这会创建一个类似箭头/三角形的效果,即“工具提示尾部”。这让我大吃一惊! 我真的很想知道它是如何工作的?!
此外,有没有办法扩展这个 CSS 技巧来创建如下效果...
;
这是一个有趣的问题。
- 可以只使用 CSS 而不使用阴影来完成此操作吗?
- 这可以通过影子来完成并使其跨浏览器兼容吗?
I just came across a neat CSS trick:
HTML:
<div>Cool Trick:<br />
<div class="tooltiptail"></div>
</div>
<br />
<div>How do I get this effect with only CSS?<br />
<div class="anothertail"></div>
</div>
CSS:
.tooltiptail {
display: block;
border-color: #ffffff #a0c7ff #ffffff #ffffff;
border-style: solid;
border-width: 20px;
width: 0px;
height: 0px;
}
.anothertail {
background-image: url(http://static.jqueryfordesigners.com/demo/images/coda/bubble-tail2.png);
display: block;
height: 29px;
width: 30px;
}
Check out the fiddle... http://jsfiddle.net/duZAx/1/
This creates a little arrow/triangle-like effect, a "tooltip tail". This blows my mind! I'm really interested in knowing how this works?!
Further, is there a way to extend this CSS trick to create an effect as follows...
This is an interesting problem.
- Can this be done using only CSS, without the shadow?
- Can this be done with the shadow and having it cross-browser compatible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是一个带有 box-shadow 的示例,所有最新版本的浏览器都应该支持此
http://jsfiddle.net/MZXCj/ 1/
HTML:
CSS:
Here's an example with a box-shadow, all latest version browsers should support this
http://jsfiddle.net/MZXCj/1/
HTML:
CSS:
这是回答你的第一个问题的解释(我将把实际的CSS留给其他人,因为我很懒——请为你认为值得投票的他们的答案投票!):
当渲染具有不同边缘颜色但相同样式的边框时(在您的情况下,
solid
),分割每对相邻角的接缝是对角线。它与此处的图表描绘的非常相似groove
、ridge
、inset
和outset
边框样式。请注意,虽然所有浏览器的行为方式都是相同的,并且从我记事起就一直这样做,但这种行为在 CSS2.1 规范或 CSS 背景和边框模块中都没有完全定义。后者有一个部分描述角落处的颜色和样式过渡,以及该描述似乎暗示,对于角半径为零的边框,渲染的线实际上是将填充边缘的角与边框边缘的角连接起来的线(导致相等的 45 度角线)宽度边框),但规范仍然警告说,情况可能并不总是如此(特别是因为它甚至没有明确考虑角半径为零的边框)。1
由 内容(原始 W3C)盒模型,在 20 像素边框之外创建 40x40 区域,内容尺寸定义为 0x0。
用连接四个角的对角线分割一个正方形,会得到四个直角三角形,它们的直角在正方形的中点相交(见下文)。
用
顶部、底部和左侧边框为白色,以匹配
.tooltiptail
元素容器的背景,而右边框为蓝色阴影,以匹配工具提示的背景颜色:结果是这样的,标记了边框,并使用我值得信赖的线条工具添加了边框边界:
重新定向工具提示尾部只是切换工具提示颜色的问题。例如,这将产生附加到尖端底部的尾部:
jsFiddle 预览
1 如果您是标准合规性的坚持者,您不妨考虑这一切黑客攻击。
Here's an explanation to answer your first question (I'll leave the actual CSS to others as I'm lazy — please upvote their answers which you think deserve the votes!):
When rendering a border with varying edge colors but the same style (in your case,
solid
), the seam dividing each pair of adjacent corners is a diagonal line. It's quite similar to what the diagram here depicts of thegroove
,ridge
,inset
andoutset
border styles.Note that while all browsers behave the same way and have done so for as long as I can remember, this behavior is not fully defined in either the CSS2.1 spec or the CSS Backgrounds and Borders module. The latter has a section describing color and style transitions at corners, and the description seems to imply that for borders with zero corner radii, the line that is rendered is in fact a line that joins the corner of the padding edge with the corner of the border edge (resulting in a 45-degree angled line for equal-width borders), but the spec still cautions that this may not always be the case (especially since it does not even account for borders with zero corner radii explicitly).1
By the content (original W3C) box model, a 40x40 area is created out of the 20-pixel borders, with the content dimensions being defined as 0x0.
Dividing a square with diagonal lines joining its four corners results in four right triangles whose right angles meet at the square's midpoint (see below).
The top, bottom and left borders are white to match the background of the
.tooltiptail
element's container, while the right border is a shade of blue to match the background color of the tooltip:The result is this, with the borders labeled, and the border boundaries added using my trusty Line Tool:
Reorienting the tooltip tail is simply a matter of switching the tooltip color around. For example, this would yield a tail that's attached to the bottom of a tip:
jsFiddle preview
1 If you're a stickler for standards compliance, you may as well consider all this a hack.
我仅使用一个
div
元素来制作此工具提示。HTML:
CSS:
Demo
说明:
我的普通 div 带有边框,就像其他示例一样。尾部是 CSS 的简单组合:
I do this tooltip with only one
div
element.HTML:
CSS:
Demo
Explanation:
I have my normal div with border just like other example. The tail is a simple combination of CSS :
没有阴影的工具提示
Fiddle 演示
带阴影(在 WebKit 中看起来有点奇怪......必须优化它我猜测):
演示 1,演示2
Tooltip without shadow
Fiddle Demo
With Shadow (looks bit weird in WebKit... gotta optimize it I guess):
Demo 1, Demo 2
跨浏览器方法:
这个可以在 IE7+ 上运行(也可以在 IE6 中使用 (
filter: chroma(color=white);
) 运行,但不会显示箭头周围的黑色边框)。IE6 修复:
这将使 IE6 渲染的难看的黑色透明度成为您在色度过滤器中指定的颜色(我使用了白色,因此它在背景中消失)。
CSS 3 approach:
您可以使用 CSS3 旋转来做到这一点,但在不兼容 CSS3 的浏览器中会失败:
Crossbrowser approach:
This one works from IE7+ (works in IE6 using (
filter: chroma(color=white);
) too but won't display the black border around the arrow).IE6 fix:
This will make the ugly black transparecy that is rendered by IE6 the color you specified in chroma filter (I did white so it disappears in background).
CSS 3 approach:
You could do it with CSS3 rotation, but will fail in non CSS3 compliant browsers:
您可以使用 CSS 的 :before 和 :after 伪元素。例如,:before 可用于插入三角形,:after 可用于插入矩形。这两者的组合创建了一个气泡工具提示,
例如:
可以在 http:// /www.careerbless.com/services/css/csstooltipcreator.php
You can make use of the :before and :after pseudo-elements of CSS. FOr instance, :before can be used to insert a triangle and :after to insert a rectangle. The combination of these two creates a bubble tool tip
Eg :
An online tool is available at http://www.careerbless.com/services/css/csstooltipcreator.php