无法在没有锚点的情况下垂直对齐导航栏中的文本

时间:2015-10-19 19:56:22

标签: html css twitter-bootstrap

如果我使用以下组合:

<nav class="navbar navbar-default navbar-static-top">
  <div class="container">
    <ul class="nav navbar-nav">
      <li><a><span class="glyphicon glyphicon-earphone"></span> 99999999</a></li>
    </ul>
  </div>
</nav>

我有所需的输出,因为电话号码在导航栏中垂直居中。

但是,它仅适用于<a>,如果我删除它,那么它会转到顶部。

我试过<li class="navbar-btn">,但无济于事。

是否有使用bootstrap css的解决方法?

4 个答案:

答案 0 :(得分:1)

一些简单的CSS可以通过向LI元素添加一个唯一的类来解决这个问题:Bootply example

通过检查开发者控制台中的CSS,您会看到Bootstrap的导航栏中的Anchor有填充。

HTML:

<nav class="navbar navbar-default navbar-static-top">
  <div class="container">
    <ul class="nav navbar-nav">
      <li class="phone-nb"><span class="glyphicon glyphicon-earphone"></span> 99999999</li>
    </ul>
  </div>
</nav>

CSS:

.phone-nb{
    padding-top:15px;
    padding-bottom:15px;
}

答案 1 :(得分:1)

您可以使用已经内置的navbar-text课程。请参阅Docs

*如果您使用所有navbar选项,我会提供两个示例。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-default navbar-static-top">
  <div class="container">
    <p class="navbar-text"><span class="glyphicon glyphicon-earphone"></span> 99999999</p>
    <ul class="nav navbar-nav">
      <li class="active"><a href="#">Link</a>

      </li>
    </ul>
  </div>
</nav>
<hr>
<h3>Full Navbar</h3>
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>

      </button> <a class="navbar-brand" href="#">Brand</a>

      <p class="navbar-text"><span class="glyphicon glyphicon-earphone"></span> 99999999</p>
    </div>
    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a>

        </li>
        <li><a href="#">Link</a>

        </li>
        <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>

          <ul class="dropdown-menu">
            <li><a href="#">Action</a>

            </li>
            <li><a href="#">Another action</a>

            </li>
            <li><a href="#">Something else here</a>

            </li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a>

            </li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a>

            </li>
          </ul>
        </li>
      </ul>
    </div>
    <!-- /.navbar-collapse -->
  </div>
  <!-- /.container-fluid -->
</nav>

答案 2 :(得分:0)

该上下文中的锚元素具有填充,行高和调整后的显示,使其显示为居中。 <li>没有这个。如果您将.btn应用于<li>,它会根据您的意愿垂直对齐,但会将文字居中。你必须超越它以使它与右边对齐。

<nav class="navbar navbar-default navbar-static-top">
  <div class="container">
    <ul class="nav navbar-nav">
        <li class="btn"><span class="glyphicon glyphicon-earphone"></span> 99999999</li>
    </ul>
  </div>
</nav>
li.btn {
    text-align: left;
}

http://jsfiddle.net/6kaq1uwf/

答案 3 :(得分:0)

看看bootstrap css,有一个填充:锚点上10px 15px。

Package Control

Error executing: /usr/bin/security dump-trust-settings -d

SecTrustSettingsCopyTrustSettings: The specified item could not be found in the keychain.
SecTrustSettingsCopyTrustSettings: The specified item could not be found in the keychain.
Number of trusted certs = 3
Cert 0: Kaspersky Web Anti-Virus Certification Authority
   Number of trust settings : 0
Cert 1: GTE CyberTrust Global Root
Cert 2: Class 3 Public Primary Certification Authority

VCS-based packages can be ignored with the "ignore_vcs_packages" setting.

如果您不想要标签,只需将该填充添加到电话号码的容器中即可。在这种情况下,列表标记。

.nav>li>a {
  padding: 10px 15px;
}

对于文本颜色更改,从#777到#333的悬停更改。

<li class="demo"><span class="glyphicon glyphicon-earphone"></span> 99999999</li>

.demo {
   padding: 10px 15px;
}

查看此plunkr http://embed.plnkr.co/0sq4Rlc0LpcjfFtWxD3n/preview