我需要使用媒体查询设置锚标签中心

时间:2018-05-16 12:03:55

标签: css twitter-bootstrap media-queries

我正在尝试使用媒体查询将我的品牌名称移至屏幕中心,但我不认为我做得对。我正在使用Bootstrap 4.1。

  <nav class="navbar navbar-expand-sm  navbar-dark bg-dark">

  <a class="navbar-brand" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" href="#">Brand Name</a>

<div class="collapse navbar-collapse" id="navbarSupportedContent">
  <ul class="navbar-nav ml-auto">
    <li class="nav-item">
      <a class="nav-link" href="#">Sign Up</a>
    </li>
    <li class="nav-item">
      <a class="nav-link " href="#">About</a>
    </li>
    <li class="nav-item dropdown">
      <a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown">Contact</a>
      <div class="dropdown-menu" aria-labelledby="navbarDropdown">

        <a class="dropdown-item" href="#">Phone</a>
        <a class="dropdown-item" href="#">Email</a>
        <a class="dropdown-item" href="#">Social</a>
      </div>
    </li>
  </ul>

</div>

    @media (min-width: 320px)
  .navbar
    text-align: center

1 个答案:

答案 0 :(得分:1)

  

请在移动视图中查看。

.navbar默认为justify-content: flex-start,因此您需要在移动设备中更改为justify-content: center

&#13;
&#13;
@media (min-width: 320px) {
    .navbar {
        justify-content: center !important;
    }
}
&#13;
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
<nav class="navbar navbar-expand-sm  navbar-dark bg-dark">

  <a class="navbar-brand" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" href="#">Brand Name</a>

<div class="collapse navbar-collapse" id="navbarSupportedContent">
  <ul class="navbar-nav ml-auto">
    <li class="nav-item">
      <a class="nav-link" href="#">Sign Up</a>
    </li>
    <li class="nav-item">
      <a class="nav-link " href="#">About</a>
    </li>
    <li class="nav-item dropdown">
      <a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown">Contact</a>
      <div class="dropdown-menu" aria-labelledby="navbarDropdown">

        <a class="dropdown-item" href="#">Phone</a>
        <a class="dropdown-item" href="#">Email</a>
        <a class="dropdown-item" href="#">Social</a>
      </div>
    </li>
  </ul>

</div>
&#13;
&#13;
&#13;