Align to right “left triangle” in menu element

前端 未结 2 1750
再見小時候
再見小時候 2021-01-22 13:51

I build HTML/CSS/JS menu and want to align arrow to the right to point that this element is submenu.

My problem that in Firefox triangle (\"▶\" sign) shown on next line

相关标签:
2条回答
  • 2021-01-22 14:10

    getting rid of "white-space: nowrap" helps

    0 讨论(0)
  • 2021-01-22 14:19

    Finally I solve problem:

    <div class="container">
      Top level menu (hover on me)
      <div class="box">
        <div class="submenu">
          <div class="name">Long submenu 1</div>
          <div class="box">        
            <a href="#">Item 1</a>
            <a href="#">Item 2</a>
          </div>
        </div>
        <a href="#">Item 1 1 1</a>
        <a href="#">Item 2</a>
        <div class="submenu">
          <div class="name">Very long submenu 2</div>
          <div class="box">        
            <a href="#">Item 1</a>
            <a href="#">Item 2</a>
            <a href="#">Item 3</a>
          </div>
        </div>
        <a href="#">Item 1</a>
        <a href="#">Item 2</a>
      </div>
    </div>
    

    and:

    .container {
        display: inline-block;
        background: gold;
        position: relative;
    }
    .box { display: none; }
    .container:hover > .box {
      display: block;
      position: absolute;
      top: 100%;
    }
    .container .submenu {
      position: relative;
    }
    .container .submenu:hover > .box {
      display: block;
      position: absolute;
      top: 0;
      left: 100%;
    }
    
    a, .name {
      white-space: nowrap;
      display: block;
    }
    .name {
      padding-right: 1.2em;
      position: relative;
    }
    .name:after {
      content: "▶";
      position: absolute;
      top: 0;
      left: 100%;
      margin-left: -1em;
    }
    

    Essential part is to make element with triangle as block and position: relative and reserve space for triangle by padding-right: -1.2em and position triangle by position: absolute after element and move back triangle by margin-left: -1em.

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