Search code examples
htmltwitter-bootstrapsmarty

Inline dropdown-menu <li> when using bootstrap glyphicons that right align


I have added right aligned (pull-right) bootstrap glyphicons to my dropdown-menu options. The problem is that the longest option gets a line break between the name and the glyphicon. How can I make them on them same line? When I try without pull-right everything aligns perfect.

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
    <li class="dropdown">
        <a
            href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
            aria-haspopup="true"
            aria-expanded="false">
                menu
                <span class="caret"></span>
        </a>
        <ul class="dropdown-menu">
            {foreach from=$item key=sub_title item=sub_item}
                    <li>
                        <a
                            href="{$sub_item.url}"
                            target="_blank">
                            {$sub_title}
                            <span class="glyphicon glyphicon-{$sub_item.icon} pull-right" aria-hidden="true"></span>
                        </a>
                    </li>
            {/foreach}
        </ul>
    </li>
</ul>


Solution

    1. Let's wrap the text in a separate block.
    2. The icon with float: right; has to go before the text.

    @import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');
    
    .item-text {
      margin-right: 20px;
    }
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav navbar-right">
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">menu<span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li>
              <a href="#URL" target="_blank">
                <span class="glyphicon glyphicon-tag pull-right" aria-hidden="true"></span>
                <span class="item-text">short short</span> 
              </a>
            </li>
            <li>
              <a href="#URL" target="_blank">
                <span class="glyphicon glyphicon-tag pull-right" aria-hidden="true"></span>
                <span class="item-text">long long long long long long long long long long long long long</span> 
              </a>
            </li>
            <li>
              <a href="#URL" target="_blank">
                <span class="glyphicon glyphicon-tag pull-right" aria-hidden="true"></span>
                <span class="item-text">short short</span> 
              </a>
            </li>
            <li>
              <a href="#URL" target="_blank">
                <span class="glyphicon glyphicon-tag pull-right" aria-hidden="true"></span>
                <span class="item-text">short short</span> 
              </a>
            </li>
            <li>
              <a href="#URL" target="_blank">
                <span class="glyphicon glyphicon-tag pull-right" aria-hidden="true"></span>
                <span class="item-text">long long long long long long long long long long long long long</span> 
              </a>
            </li>
            <li>
              <a href="#URL" target="_blank">
                <span class="glyphicon glyphicon-tag pull-right" aria-hidden="true"></span>
                <span class="item-text">long long long long long long long long long long long long long</span> 
              </a>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    
    <div class="alert alert-danger visible-xs text-center">
      Menu becomes visible in the window with a width of 768px or more.
    </div>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>