我的导航不是我想要的地方

时间:2017-03-10 19:12:33

标签: html css

如果您运行代码,您会看到我左上角有一个带文字的标题,但我希望我的导航位于右上角,但它只显示在标题的底部。我试过检查元素并试图解决它但没有快乐。这是我的代码。

 html, body {
      height: 100%;
      width: 100%;
      margin: auto;
      padding: auto;
    }
    
    h1 {
      margin: 0;
      padding: 0;
    }
    
    ul {
      margin: 0;
      padding: 0;
    }
    
    .ct {
      font-weight: lighter;
    }
    
    header {
      position: fixed;
      top: 0;
      left: 0;
      height: 75px;
      width: 100%;
      background: -webkit-linear-gradient(top, #3a537a 1%,#204377 100%);
      -webkit-box-shadow: 0px 9px 35px -5px rgba(0,0,0,0.75);
    }
    
    header h1 {
      font-size: 30px;
      font-family: Roboto;
      color: #ffffff;
      line-height: 75px;
      padding-left: 15px;
      font-weight: 400;
      letter-spacing: 0.05em;
    }
    
    header nav ul{
      float: right;
    }
    
    header nav ul li {
      display: inline-block;
    }
     <!DOCTYPE html>
     <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <title>CT Designs</title>
        <!-- Stylesheets-->
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <!-- Fonts-->
        <link href="https://fonts.googleapis.com/css?family=Roboto:100,100i,300,300i,400,400i,500,500i,700,700i,900,900i" rel="stylesheet">
     </head>
    
     <body>
      <header>
        <h1><span class="ct">CT</span> Designs</h1>
        <nav>
          <ul>
            <li>Home</li>
            <li>About Me</li>
            <li>Projects</li>
            <li>Contact</li>
          </ul>
        </nav>
      </header>
     </body>
    
     </html>

2 个答案:

答案 0 :(得分:1)

您可以将float: left添加到header h1,这样您的导航列表就可以按照您的指定向右浮动。

但我会在justify-content: space-between的标题上使用flexbox将元素向左/向右推,然后使用align-items垂直对齐标题内容。

html,
body {
  height: 100%;
  width: 100%;
  margin: auto;
  padding: auto;
}

h1 {
  margin: 0;
  padding: 0;
}

ul {
  margin: 0;
  padding: 0;
}

.ct {
  font-weight: lighter;
}

header {
  position: fixed;
  top: 0;
  right: 0;
  height: 75px;
  width: 100%;
  background: -webkit-linear-gradient(top, #3a537a 1%, #204377 100%);
  -webkit-box-shadow: 0px 9px 35px -5px rgba(0, 0, 0, 0.75);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

header h1 {
  font-size: 30px;
  font-family: Roboto;
  color: #ffffff;
  line-height: 75px;
  padding-left: 15px;
  font-weight: 400;
  letter-spacing: 0.05em;
}

header nav ul {
  float: right;
}

header nav ul li {
  display: inline-block;
}
<header>
  <h1><span class="ct">CT</span> Designs</h1>
  <nav>
    <ul>
      <li>Home</li>
      <li>About Me</li>
      <li>Projects</li>
      <li>Contact</li>
    </ul>
  </nav>
</header>

答案 1 :(得分:0)

原因是浮动。如果你使用绝对定位,它将起作用 - 调整到适合。

&#13;
&#13;
html, body {
      height: 100%;
      width: 100%;
      margin: auto;
      padding: auto;
    }
    
    h1 {
      margin: 0;
      padding: 0;
    }
    
    ul {
      margin: 0;
      padding: 0;
    }
    
    .ct {
      font-weight: lighter;
    }
    
    header {
      position: fixed;
      top: 0;
      left: 0;
      height: 75px;
      width: 100%;
      background: -webkit-linear-gradient(top, #3a537a 1%,#204377 100%);
      -webkit-box-shadow: 0px 9px 35px -5px rgba(0,0,0,0.75);
    }
    
    header h1 {
      font-size: 30px;
      font-family: Roboto;
      color: #ffffff;
      line-height: 75px;
      padding-left: 15px;
      font-weight: 400;
      letter-spacing: 0.05em;
    }
    
    header nav ul{
      position: absolute;
      top: 0;
      right: 0;
      color: white;
    }
    
    header nav ul li {
      display: inline-block;
    }
&#13;
<!DOCTYPE html>
     <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <title>CT Designs</title>
        <!-- Stylesheets-->
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <!-- Fonts-->
        <link href="https://fonts.googleapis.com/css?family=Roboto:100,100i,300,300i,400,400i,500,500i,700,700i,900,900i" rel="stylesheet">
     </head>
    
     <body>
      <header>
        <h1><span class="ct">CT</span> Designs</h1>
        <nav>
          <ul>
            <li>Home</li>
            <li>About Me</li>
            <li>Projects</li>
            <li>Contact</li>
          </ul>
        </nav>
      </header>
     </body>
    
     </html>
&#13;
&#13;
&#13;