在工具提示中显示div的顶部添加三角形?

时间:2015-05-05 08:45:30

标签: html css

我有一个div,它将在悬停某个元素时显示。当我们悬停在元素上时显示的div显示在元素下方。我需要使用css将三角形/\添加到div的顶部。

我怎样才能做到这一点?

HTML

<div class="user-options">
        <ul class="settings">
            <li><a href="home.html">JOHN DOE</a></li>
            <li><a href="user-customization.html">My Playground</a></li>
            <li><a href="myrules.html">Settings</a></li>
            <li><a href="index.html">Logout</a></li>
        </ul>
    </div>

CSS

.user > .user-options {
    background: none repeat scroll 0 0 #eeeeee;
    display: none;
    margin-top: 25px;
    overflow: auto;
    padding: 0 0 0 10px;
    position: absolute;
    width: 125px;
    z-index: 999;
    border:solid #cccccc 1px;
    margin-left:-24px;
}

3 个答案:

答案 0 :(得分:1)

你可以这样做:

注意:从overflow: auto;

中删除.user-options

<强> CSS

.user-options {
    background: none repeat scroll 0 0 #eeeeee;
    margin-top: 25px;
    padding: 0 0 0 10px;
    position: absolute;
    width: 125px;
    z-index: 999;
    border:solid #cccccc 1px;
    margin-left:-24px;
}

.user-options:after, .user-options:before {
    bottom: 100%;
    left: 50%;
    border: solid transparent;
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
}

.user-options:after {
    border-color: rgba(238, 238, 238, 0);
    border-bottom-color: #eeeeee;
    border-width: 15px;
    margin-left: -15px;
}
.user-options:before {
    border-color: rgba(204, 204, 204, 0);
    border-bottom-color: #cccccc;
    border-width: 16px;
    margin-left: -16px;
}

<强> DEMO HERE

答案 1 :(得分:0)

我会使用这个,取决于你是否想要围绕三角形的边框。如果是这样,您想同时使用:before:after

.user-options {
  background: none repeat scroll 0 0 #eeeeee;
  margin-top: 25px;
  padding: 0 0 0 10px;
  position: relative;
  width: 125px;
  z-index: 999;
  border: solid #cccccc 1px;
  margin-left: -24px;
}
.user-options:before {
  content: "";
  position: absolute;
  border-style: solid;
  border-width: 10px 12px;
  border-color: transparent transparent red transparent;
  left: 15%;
  /* TOP IS DOUBLE THE HEIGHT OF THE BORDER */
  top: -20px;
}
<div class="user-options">
  <ul class="settings">
    <li><a href="home.html">JOHN DOE</a>
    </li>
    <li><a href="user-customization.html">My Playground</a>
    </li>
    <li><a href="myrules.html">Settings</a>
    </li>
    <li><a href="index.html">Logout</a>
    </li>
  </ul>
</div>

答案 2 :(得分:0)

您可以提供以下方式:

&#13;
&#13;
.user-options {
  position: relative;
  border: 10px solid #cccccc;
  top: 100px;
}
.user-options:after,
.arrow_box:before {
  bottom: 100%;
  left: 20%;
  border: solid transparent;
  content: " ";
  position: absolute;
}
.user-options:after {
  border-bottom-color: #cccccc;
  border-width: 30px;
}
.user-options:before {
  border-bottom-color: #cccccc;
  border-width: 30px;
}
&#13;
<div class="user-options">
  <ul class="settings">
    <li><a href="home.html">JOHN DOE</a>
    </li>
    <li><a href="user-customization.html">My Playground</a>
    </li>
    <li><a href="myrules.html">Settings</a>
    </li>
    <li><a href="index.html">Logout</a>
    </li>
  </ul>
</div>
&#13;
&#13;
&#13;

检查Fiddle Here.