How do you get a triangle hover effect on a pure css navbar?

前端 未结 3 2044
孤独总比滥情好
孤独总比滥情好 2020-12-10 09:06

I would like to have a little triangle underneath the the text that points up when the user hovers over the different tabs. Here is a bit of code I\'m working with.

相关标签:
3条回答
  • 2020-12-10 09:49

    Here is a fiddle that uses an background-image that will display over the hovered menu item. It not pretty but further css should help with that.

    UPDATE

    Sorry I must have misread that. Here is a working fiddle with a smaller arrow pointing in the proper direction.

    0 讨论(0)
  • 2020-12-10 10:05

    Here is a modification to your jsfiddle:

    I've added a <span class="arrow"></span> to contain the triangles in the HTML:

    <div class="tab_container">
        <div id="test1-header" class="accordion_headings header_highlight" >Home<span class="arrow"></span></div>
        <div id="test2-header" class="accordion_headings" >About<span class="arrow"></span></div>
        <div id="test3-header" class="accordion_headings" >Work<span class="arrow"></span></div>
        <div id="test4-header" class="accordion_headings" >Social<span class="arrow"></span></div>
        <div id="test5-header" class="accordion_headings" >Contact<span class="arrow"></span></div>
    </div>
    

    Here are the changes made to your menu which reduce the size of the triangle and position them at the bottom center of each menu item when hovered over: CSS:

    /*
    .accordion_headings:hover{
        background:#00CCFF;
        width: 0;
        height: 0;
        border-left: 5px solid transparent;
        border-right: 5px solid transparent;
        border-bottom: 5px solid red;
    }
    */
    .accordion_headings{
        position:relative;
    }
    .accordion_headings .arrow{
        display:none;
    }
    .accordion_headings:hover .arrow{
        width: 0;
        height: 0;
        border-left: 5px solid transparent;
        border-right: 5px solid transparent;
        border-bottom: 5px solid red;
    
        display:block;
        position:absolute;
        bottom:0;
        left:49%;
    }
    
    0 讨论(0)
  • 2020-12-10 10:06

    I think this is probably what you're looking for:

    Fiddle

    Also, please use semantic markup:

    1. If your using HTML5 wrap your navigation in <nav> tags.
    2. Your links (if they really are going to be links) should be <a> elements.
    3. For a list of links like you have it is advised to use a list (<ul> & <li>).

    body {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 11px;
    }
    
    nav ul {
      margin: 0;
      padding: 0;
      list-style: none;
    }
    
    nav li {
      float: left;
      width: 20%;
    }
    
    nav a {
      display: block;
      padding: 5px;
      border-bottom: 1px solid black;
      background: #fff;
      color: #000;
      font-weight: bold;
      position: relative;
    }
    
    nav a:hover,
    .active {
      background: #bbb;
    }
    
    nav a:hover:after {
      content: "";
      display: block;
      border: 12px solid #bbb;
      border-bottom-color: #000;
      position: absolute;
      bottom: 0px;
      left: 50%;
      margin-left: -12px;
    }
    <nav>
      <ul>
        <li><a href="#" class="active">Link1</a></li>
        <li><a href="#">Link2</a></li>
        <li><a href="#">Link3</a></li>
        <li><a href="#">Link4</a></li>
        <li><a href="#">Link5</a></li>
      </ul>
    </nav>

    0 讨论(0)
提交回复
热议问题