CSS - 在悬停状态下添加另一个元素

2022-01-22 00:00:00 class hover css

全部,

我有一个水平菜单栏.当用户将鼠标悬停在菜单栏中的每个链接上时,我想在链接下方显示一个小三角形.

I have a horizontal menu bar. When the user hovers over each link in the menu bar, I want to show a small triangle underneath the link.

这个小三角形不是图像,而是由 CSS 边框语法呈现的.图片和代码如下:

This small triangle is not an image but is rendered by CSS border syntax. Image and code below:

这是三角形的 CSS 代码:

Here is the CSS code for the triangle:

#css_arrow {
    border-color: transparent transparent rgba(111,46,11,0.0) transparent;
    border-style: solid;
    border-width: 8px;
    height: 0;
    width: 0;
    position: absolute;
    top: 34px;
    left: 78px;

我想将三角形​​添加到悬停状态的菜单项.

I want to add the triangle to the menu item in hover state.

有人可以建议如何将此 id 添加到悬停状态.我考虑过为菜单栏中的项目使用两个类,但没有成功.这是html代码:

Can someone please advise how to go about adding this id to the hover state. I thought about using two classes for the items in the menu bar but its not working out. Here is the html code:

<div id="main_bar">
                <ul>
                    <li class="maintabs maintabs_tri"><a href="#">Overview</a></li><li class="maintabs maintabs_tri"><a href="#">Collar/ Neckline</a></li><li class="maintabs maintabs_tri"><a href="#">Sleeves</a>
                    <ul>
                        <li class="s_leftright"><a href="#">Left Sleeves</a></li>
                        <li class="s_leftright"><a href="#">Right Sleeves</a></li>
                    </ul></li><li class="maintabs maintabs_tri"><a href="#">Body</a></li>
                </ul>           
            </div>  

还有 CSS,它不起作用:

And the CSS, which doesnt work:

    .maintabs_tri:hover {
    border-color: transparent transparent rgba(111,46,11,1) transparent;
    border-style: solid;
    border-width: 8px;
    position: absolute;
    height: 0;
    width: 0;
    top: 32px;
    left: 78px;
} 

推荐答案

您需要将其放置在所有项目上,但仅在悬停时显示,即

You are going to need to place it on all items, but only display it on hover, i.e.

<ul>
   <li>
      <a href="#">Whatever <span></span></a>
   </li>
   <li>
      <a href="#">Whatever <span></span></a>
   </li>
   <li>
      <a href="#">Whatever <span></span></a>
   </li>
</ul>

在这种情况下,span 将是三角形.我假设您已经适当地设置了您的 ul an li 的样式.所以,在你的 CSS 中:

In this case, span is going to be the triangle. I'm assuming you've already styled your ul an li appropriately. So, in your css:

ul li a {
   display: block;
   width: 100px;
   height: 32px;
   float: left;
   position: relative;
}

ul li a:hover span {
  display: block;
}

ul li a span {
    display: none;
     border-color: transparent transparent rgba(111,46,11,1) transparent;
    border-style: solid;
    border-width: 8px;
    height: 0;
    width: 0;
    position: absolute;
    bottom: 0;
    left: 50%;
}

我将它嵌套在锚点中,因为这样可以最大化可点击区域.

I'm nesting it within the anchor because that maximizes the clickable area.

相关文章