首頁  >  問答  >  主體

導覽列上的CSS底部邊框

我有一個導覽欄,當懸停清單中的任何項目時,我在底部添加了一條紅線,但我想將該紅線移到標題下方(例如「服務」),知道如何實現此目的?

我在 codepen 中新增了一個小範例,以便您可以輕鬆檢查 HTML 和 CSS 程式碼

header {
  background-color: lightblue;
  padding-top: 1rem;
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-around;
}

header nav {
  min-width: 50%;
}

header nav ul {
  margin: 0;
  height: 100%;
  list-style: none;
  padding-left: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

header li:hover {
  height: 100%;
  border-bottom: 2px solid red;
}
<header>
  <a href="/">
    <p>Whatever logo</p>
  </a>
  <nav>
    <ul>
      <li>About us</li>
      <li>Services</li>
      <li>Pricing</li>
      <li>Blog</li>
    </ul>
  </nav>
  <a href="/">CONTACT</a>
</header>

查看程式碼的連結

P粉270891688P粉270891688176 天前356

全部回覆(2)我來回復

  • P粉274161593

    P粉2741615932024-04-07 14:15:16

    我認為只需為所有清單元素提供與標題相同的高度即可。

    像這樣:-

    header {
      background-color: lightblue;
      padding-top: 1rem;
      height: 3rem;
      position: sticky;
      top: 0;
      display: flex;
      align-items: center;
      justify-content: space-around;
    }
    
    header nav {
      min-width: 50%;
      height : 100%;
    }
    
    header nav ul {
      margin: 0;
      height: 100%;
      list-style: none;
      padding-left: 0;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    
    header li{
      height: inherit;
    }
    
    header li:hover {
      border-bottom: 2px solid red;
    }
      
        

    Whatever logo

    CONTACT

    回覆
    0
  • P粉998100648

    P粉9981006482024-04-07 13:14:16

    您可以修復標題高度,也可以修復導覽列項目的高度。 另外,您還遇到一個問題,即懸停時 li 元素會移動。您也可以透過始終向元素添加透明顏色的邊框來解決此問題,這樣元素的整體高度在懸停狀態下不會改變。

    這是固定的CSS

    header {
      background-color: lightblue;
      position: sticky;
      display: flex;
      height: 60px;
      align-items: center;
      justify-content: space-around;
    }
    
    header nav {
      min-width: 50%;
    }
    
    header nav ul {
      margin: 0;
      height: 100%;
      list-style: none;
      padding-left: 0;
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 60px;
    }
    
    header li {
      display: flex;
      align-items: center;
      border-bottom: 2px solid transparent;
      height: 60px;
    }
    
    header li:hover {
      border-bottom: 2px solid red;
    }

    https://codepen.io/swarajgk/pen/JjZewPo?editors=1100

    #

    回覆
    0
  • 取消回覆