CSS:如何将箭头附加到div并使其重叠边框

13

我正在尝试创建一个带有错误提示的弹出框,但是我遇到了麻烦,无法使箭头出现在我要附加它的div边框上方。我会感激任何帮助。

目前这就是我的进展...

This is what I have...

这是我使用的CSS代码,但无法使其工作:

1. 整个弹出框的DIV:

<div class="info-popover">
    <div class="inner"></div>
    <div class="arrow"></div>
</div>

2.每个元素的CSS:

.info-popover {
    height: 250px;
    margin-top: -255px;
    position: absolute;
    width: 400px;
}

.info-popover .inner {
    background-color: #FFFFFF;
    border: 1px solid #003366;
    border-radius: 10px 10px 10px 10px;
    height: 240px;
    margin-top: 0;
    width: 100%;
}

.info-popover .arrow {
    background: url("/images/dock/popover-arrow.png") no-repeat scroll center -5px transparent;
    height: 15px;
    position: relative;
    width: 100%;
    z-index: 999;
}
3个回答

24

CSS解决方案:

http://jsfiddle.net/wn7JN/

.bubble 
{
position: relative;
width: 400px;
height: 250px;
padding: 0px;
background: #FFFFFF;
border: #000 solid 1px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}

.bubble:after 
{
content: "";
position: absolute;
bottom: -25px;
left: 175px;
border-style: solid;
border-width: 25px 25px 0;
border-color: #FFFFFF transparent;
display: block;
width: 0;
z-index: 1;
}

.bubble:before 
{
content: "";
position: absolute;
top: 250px;
left: 174px;
border-style: solid;
border-width: 26px 26px 0;
border-color: #000 transparent;
display: block;
width: 0;
z-index: 0;
}

1
我本来想建议使用CSS3而不是图片(因为你标记了问题CSS3),但是我更喜欢坚持回答问题,因为每次我建议不同的事情时,我的答案都会得到负面评价... - Santiago Baigorria
3
实际上,我认为既然CSS3被标记了,而这个问题是“CSS:如何将箭头附加到div并使其重叠边框”的答案,那么这将是解决这个问题的一个好方法。 - Andrew Clody
1
这是一个更好的方法。也许,Santz,我应该说“对此的任何帮助或更好的解决方案都将不胜感激”。我想下次我可以这样做。我希望我也能接受你的答案,因为当我为ie7创建css文件时,我会使用那个解决方案,因为它不支持这些css3伪元素...我已经为你的答案点赞了。感谢你的努力。这个解决方案很好地适合了我的需求... - AKKAweb

7
最简单的方法:
HTML:
<div class="meow">
</div>

CSS(层叠样式表):
.meow { 
  height: 100px;
  width: 300px;
  margin: 30px;
  background: linear-gradient(#333, #222);
  -o-border-radius: 4px;
  -ms-border-radius: 4px;
  -moz-border-radius: 4px;
  -webkit-border-radius: 4px;
  border-radius: 4px;
}

.meow:after {
  content: " ";
  border-top: 11px solid #222;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  position: relative;
  top: 111px;
  right: -140px;
}

实时预览:CodePen.io

只需进行一些简单的编辑。


1
尝试一下:

HTML

<div class="info-popover">
    <div class="inner"></div>
    <p class="arrow"></p>
</div>

CSS

.info-popover {
    position: relative;
    /* any other CSS */
}

.arrow {
    background: url("/images/dock/popover-arrow.png") no-repeat 0 0;
    height: 15px;
    width: 20px; /* width of arrow image? */
    display: block;
    position: absolute;
    bottom: -15px;
    left: 0; margin: 0 auto; right: 0; /* to center the arrow */
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接