css 当切换搜索栏和标志下来?

dphi5xsq  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(72)
.navbar {
  background-color: #F91F46;
}

.src-bar {
  border: 0;
  border-radius: 5px;
  outline: none;
  padding-left: 15px;
  width: 30vw;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css" integrity="sha512-b2QcS5SsA8tZodcDtGRELiGv5SaKSk1vDHDaQRda0htPYWZ6046lr3kJ5bAAQdpV2mmA/4v0wQF9MyU6/pDIAg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.2/js/bootstrap.min.js" integrity="sha512-WW8/jxkELe2CAiE4LvQfwm1rajOS8PHasCCx+knHG0gBHt8EXxS6T6tJRTGuDQVnluuAvMxWF4j8SNFDKceLFg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<nav class="navbar navbar-expand-md fixed-top">
  <div class="container-fluid">
    <a class="navbar-brand ms-3" href="#">
      <img src="img/Logo.avif" width="40px" class="d-inline-block" />
    </a>

    <form role="search" class="search-bar">
      <div class="input-group">
        <input class="src-bar" type="search" placeholder="Search For Products" />
        <button class="btn bg-white" type="submit">
          <i class="fa fa-search"></i>
        </button>
      </div>
    </form>

    <div class="justify-content-end">
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#nav-colps" aria-controls="nav-colps" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

      <div class="collapse navbar-collapse" id="nav-colps">
        <ul class="navbar-nav me-auto mb-2 mb-lg-0">
          <li class="nav-item">
            <a class="nav-link active" aria-current="page" href="#">Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Link</a>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Dropdown </a>
            <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
              <li><a class="dropdown-item" href="#">Action</a></li>
              <li><a class="dropdown-item" href="#">Another action</a></li>
              <li>
                <hr class="dropdown-divider" />
              </li>
              <li><a class="dropdown-item" href="#">Something else here</a></li>
            </ul>
          </li>
          <li class="nav-item">
            <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
          </li>
        </ul>
      </div>
    </div>
  </div>
</nav>

enter image description here
当切换时,我希望折叠div居中。我知道原因-我把折叠和切换btn放在justify-enddiv里面,因为我需要切换btn的位置在右边。现在当切换时,我的折叠在右边,但我希望当切换时,折叠div居中。
当我删除justify-enddiv时,我得到了它的左侧,但如果我删除它,在mdlg视图中,每个nav元素都会移动到nav的左侧。
x1c 0d1x的数据
它看起来像这个或中心

gr8qqesn

gr8qqesn1#

我们确实需要将.nav-collapse移动到justify-end元素之外,以在较窄的视口中获得所需的布局。
要使菜单显示在右侧,请通过flex-grow-0类将flex-grow: 0应用于.nav-collapse元素来覆盖.nav-collapse上的默认flex-grow: 1
为了使链接元素在狭窄的视口中居中,.navbar-nav中的元素是垂直的flex布局,所以我们可以通过align-items-center类应用align-items: center

.navbar {
  background-color: #F91F46;
}

.src-bar {
  border: 0;
  border-radius: 5px;
  outline: none;
  padding-left: 15px;
  width: 30vw;
}

个字符
您还可以考虑通过text-center类应用text-align: center,将link元素 text 居中设置为窄视口:

.navbar {
  background-color: #F91F46;
}

.src-bar {
  border: 0;
  border-radius: 5px;
  outline: none;
  padding-left: 15px;
  width: 30vw;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css" integrity="sha512-b2QcS5SsA8tZodcDtGRELiGv5SaKSk1vDHDaQRda0htPYWZ6046lr3kJ5bAAQdpV2mmA/4v0wQF9MyU6/pDIAg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.2/js/bootstrap.min.js" integrity="sha512-WW8/jxkELe2CAiE4LvQfwm1rajOS8PHasCCx+knHG0gBHt8EXxS6T6tJRTGuDQVnluuAvMxWF4j8SNFDKceLFg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<nav class="navbar navbar-expand-md fixed-top">
  <div class="container-fluid">
    <a class="navbar-brand ms-3" href="#">
      <img src="https://picsum.photos/40/40" width="40px" class="d-inline-block" />
    </a>

    <form role="search" class="search-bar">
      <div class="input-group">
        <input class="src-bar" type="search" placeholder="Search For Products" />
        <button class="btn bg-white" type="submit">
          <i class="fa fa-search"></i>
        </button>
      </div>
    </form>
    
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#nav-colps" aria-controls="nav-colps" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse flex-grow-0" id="nav-colps">
      <ul class="navbar-nav me-auto mb-2 mb-lg-0 text-center">
        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="#">Home</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Dropdown </a>
          <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
            <li><a class="dropdown-item" href="#">Action</a></li>
            <li><a class="dropdown-item" href="#">Another action</a></li>
            <li>
              <hr class="dropdown-divider" />
            </li>
            <li><a class="dropdown-item" href="#">Something else here</a></li>
          </ul>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

的字符串

相关问题