如何使导航栏菜单与徽标对齐?

时间:2020-10-05 21:18:32

标签: html css

这是小提琴:https://jsfiddle.net/gnsho1br/2/

也有类似的问题建议将显示设置为inline-block,但我已经做到了。我的容器是col-1。我也尝试过给logofloat: left,但是菜单偏心(也许左边距从徽标的末尾开始??)。

1 个答案:

答案 0 :(得分:1)

使用display:flex代替。如果要使项目居中,请尝试align-items:center,justify-items:center。有关更多信息,请参见Flexbox

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

li,
a,
button {
  font-family: "Elliot-Sans", sans-serif;
  font-weight: 300;
  font-size: 14px;
  color: #777777;
  text-decoration: none;
}

#wrapper {
  margin: 0 15px;
  padding: 15px 0;
  position: relative;
}

.row {
  max-width: 65%;
  margin: 0 auto;
  padding: 75px 0;
  position: relative;
}

.col-1 {
  width: 100%;
  padding: 20px;
  display: flex;
}

#header {
  height: 71px !important;
  overflow: visible;
  z-index: 9999;
  width: 100%;
  position: absolute !important;
}

#header .row {
  padding: 0;
}

#header ul {
  text-align: center;
}

.logo {
  width: 150px;
  height: auto;
  cursor: pointer;
  margin-right: auto;
  background-color: inherit;
  display: inline-block;
  vertical-align: middle;
}

#topNavBar {
  list-style: none;
  margin: 0;
  background-color: #ffffff;
}
ul{
display: flex;
}

#topNavBar li {
  display: inline-block;
  padding: 0px 20px;
  background-color: inherit;
  vertical-align: middle;
}

#topNavBar li a {
  transition: all 0.2s ease 0s;
  font-weight: 400;
  text-transform: uppercase;
}

#topNavBar li a:hover {
  color: #04ad9e;
  text-decoration: none;
  padding-bottom: 4px;
  border-bottom: 3px solid #04ad9e;
}
<!DOCTYPE html>

<html lang="en">
  <head>
    <!-- <link
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
      rel="stylesheet"
    /> -->
    <link
      href="https://fonts.googleapis.com/css2?family=Rajdhani:wght@300;400&display=swap"
      rel="stylesheet"
    />
    <link
      href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css"
      rel="stylesheet"
    />
    <link href="style.css" rel="stylesheet" />
    <title>Title</title>
  </head>
  <body>
    <div id="wrapper">
      <header id="heroImage">
        <div id="header" class="navCollapse">
          <div class="row clearfix">
            <div class="col-1">
              <div class="logo">
                <img class="logo" src="image.png" alt="logo" />
              </div>
              <nav id="topNavBar">
                <ul>
                  <li><a href="index.html">Home</a></li>
                  <li><a href="#about">About</a></li>
                  <li><a href="#read">Read</a></li>
                  <li><a href="#contact">Contact</a></li>
                </ul>
              </nav>
            </div>
          </div>
        </div>
       </header>
      </div>