溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

BootStrap如何實(shí)現(xiàn)響應(yīng)式導(dǎo)航欄垂直平分和下拉列表的居中問題

發(fā)布時間:2021-07-07 11:14:14 來源:億速云 閱讀:197 作者:小新 欄目:web開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)BootStrap如何實(shí)現(xiàn)響應(yīng)式導(dǎo)航欄垂直平分和下拉列表的居中問題,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

在查看psd圖的效果是導(dǎo)航欄的a是垂直平分的。

BootStrap如何實(shí)現(xiàn)響應(yīng)式導(dǎo)航欄垂直平分和下拉列表的居中問題

那么我可否通過固定a超鏈接的高度來實(shí)現(xiàn)垂直平分呢?

<div "h-nav">
  <nav "container navbar navbar-default" role="navigation">
    <div "row">
      <div "navbar-header df-jcsbc">
        <a "navbar-brand" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
          <img src="img/test8-1/logo6.png" "">
        </a>
        <button type="button" " navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse">
          <span "sr-only">切換導(dǎo)航</span>
          <span "icon-bar"></span>
          <span "icon-bar"></span>
          <span "icon-bar"></span>
        </button>
      </div>
      <div "collapse navbar-collapse" id="navbar-collapse">
        <ul id="h-cell-1" "nav navbar-nav lsno navbar-right">
          <li ""><a href="test9-1.html" rel="external nofollow" >首頁</a></li>
          <li ""><a href="test9-3.html" rel="external nofollow" ><span "dib">職業(yè)</span></a></li>
          <li ""><a href="test9-2.html" rel="external nofollow" rel="external nofollow" ><span "dib pat">推薦</span></a></li>
          <li ""><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span "dib pat">關(guān)于</span></a></li>
        </ul>
      </div>
    </div>
  </nav>
</div>

帶著這種想法,我設(shè)定了如下屬性。

#h-cell-1 a{
  display:inline-block;
  height:97px;
  margin-left:32px;
  font-size:17.8px;
  text-decoration: none;color:white;
}

BootStrap如何實(shí)現(xiàn)響應(yīng)式導(dǎo)航欄垂直平分和下拉列表的居中問題

為此怎么實(shí)現(xiàn)垂直居中呢?

第一點(diǎn)想到的是position定位了

既然要實(shí)現(xiàn)相對于a超鏈接的position,那么必須引入一個span盒子了。

<li ""><a href="test9-2.html" rel="external nofollow" rel="external nofollow" ><span "dib pat">推薦</span></a></li>

然后增加

#h-cell-1 li{height:97px;}
#h-cell-1 a{
  display:inline-block;
  position:relative;
  width:40px;
  height:97px;
  margin-left:32px;
  border-bottom:2px solid #20B176;
  font-size:17.8px;
  text-decoration: none;color:white;
}
#h-cell-1 a span{width:40px;}

就可實(shí)現(xiàn)垂直居中了!

但另外一個問題又接著出現(xiàn)了,在galaxy5的小屏幕下,下拉菜單的li間距太大,不美觀。

BootStrap如何實(shí)現(xiàn)響應(yīng)式導(dǎo)航欄垂直平分和下拉列表的居中問題

又該怎么辦呢?

這個是在28號晚上想到方法的。靈感也是看了其他同學(xué)的日報。他們提到媒體查詢。

好,什么是媒體查詢。建議百度。這里不做介紹。

媒體查詢是吧,好那么久好辦了。

@media only screen and (max-width: 700px) {
#h-cell-1 li{height:auto;}
#h-cell-1 a{height:20px;width:100%;margin:0;padding:0;overflow:hidden;}
#h-cell-1 a span{height:auto;}
}

ok,完美。

但是還有問題??!

BootStrap如何實(shí)現(xiàn)響應(yīng)式導(dǎo)航欄垂直平分和下拉列表的居中問題

圖標(biāo),和按鈕原本是不垂直居中的??!那這個怎么搞!

<div "navbar-header df-jcsbc">
  <a "navbar-brand" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
    <img src="img/test8-1/logo6.png" "">
  </a>
  <button type="button" " navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse">
    <span "sr-only">切換導(dǎo)航</span>
    <span "icon-bar"></span>
    <span "icon-bar"></span>
    <span "icon-bar"></span>
  </button>
</div>

他們不是都在navbar-header的盒子里嗎?

那不能通過flex兩端對齊來實(shí)現(xiàn)垂直嗎?

試試看

加了個

.df-jcsbc{display:flex;justify-content: space-between;align-items: center;}

但是效果不理想?。】傮w來說,.navbar-brand 和navbra-toggle實(shí)現(xiàn)了垂直居中。

BootStrap如何實(shí)現(xiàn)響應(yīng)式導(dǎo)航欄垂直平分和下拉列表的居中問題

那,怎么辦呢?

我是直接弄

.navbar-header{position: relative;height:97px;}
.navbar-brand{
padding:0;

  }
.navbar-header img{position:absolute;
  left:20px;}
.navbar-header button{position:absolute;
  margin:0;
  right:20px;}

padding和margin來使他們讓出位置來的。

然后通過定位稍微調(diào)了點(diǎn)左右距離。

最后就可以實(shí)現(xiàn)垂直居中了。

關(guān)于“BootStrap如何實(shí)現(xiàn)響應(yīng)式導(dǎo)航欄垂直平分和下拉列表的居中問題”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI