Bootstrap 4:bootstrap 4 NavBar上的中心h1

时间:2017-09-08 12:28:22

标签: html css twitter-bootstrap

目前我正在练习bootstrap和html,我正试图将我的<h1>放在导航栏上,我已经尝试了很多方法,即使用css方式它也不起作用,所以这是我的图像screen

供您参考,我在我的HTML中使用了这段代码,

但是,它没有用,

 <h1 class="nabar" id="nabar" href="#" style="color: #2c3e50; text-align:center;">e-ticket</h1>

我的完整代码

    <nav class="navbar navbar-toggleable-md navbar-light bg-faded" style="background-color: #ecf0f1">

  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" style="background-color: #ecf0f1;">
    <span class="navbar-toggler-icon"></span>
  </button>
    <a class="nabar center-block" id="nabar" href="#" style="color: #2c3e50; text-align:center;">e-ticket</a>
  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#" style="color:#2c3e50;">Home <span class="sr-only">(current)</span></a>
      </li>


    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="text" placeholder="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

3 个答案:

答案 0 :(得分:2)

只需使用mx-auto ...

https://www.codeply.com/go/S2g7bPmHjN

<nav class="navbar navbar-toggleable-md navbar-light bg-faded" style="background-color: #ecf0f1">

    <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" style="background-color: #ecf0f1;">
        <span class="navbar-toggler-icon"></span>
    </button>
    <h1 class="mx-auto"><a class="nabar" id="nabar" href="#" style="color: #2c3e50; text-align:center;">e-ticket</a></h1>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
                <a class="nav-link" href="#" style="color:#2c3e50;">Home <span class="sr-only">(current)</span></a>
            </li>
        </ul>
        <form class="form-inline my-2 my-lg-0">
            <input class="form-control mr-sm-2" type="text" placeholder="Search">
            <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
        </form>
    </div>
</nav>

答案 1 :(得分:1)

使用中心块类应该有效 -

<a class="nabar center-block" id="nabar" href="#" style="color: #2c3e50; text-align:center;">e-ticket</a>

请在此处找到工作演示 - https://jsbin.com/quqususuda/edit?html,output

答案 2 :(得分:1)

使用电子票证文本将flex: 1;添加到您的锚元素,这应该有效。点击此链接查看flexbox

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>
   <nav class="navbar navbar-toggleable-md navbar-light bg-faded" style="background-color: #ecf0f1">

  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" style="background-color: #ecf0f1;">
    <span class="navbar-toggler-icon"></span>
  </button>
    <a class="nabar center-block" id="nabar" href="#" style="color: #2c3e50; text-align:center; flex: 1;">e-ticket</a>
  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#" style="color:#2c3e50;">Home <span class="sr-only">(current)</span></a>
      </li>


    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="text" placeholder="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>