【问题标题】:Can't center this无法居中
【发布时间】:2017-10-13 15:50:13
【问题描述】:

我尝试将导航段落居中,它不会居中。我现在尝试了很多东西,但找不到结果。

这是我的 CSS:

header {
  background: #444444;
  z-index: 100;
  transition: height 600ms 0s ease;
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  color: #ffffff;
}

#head {
  font-family: quicksand;
  text-align: center;
}

nav {
  background: #333333;
  position: fixed;
  top: 82px;
  right: 0;
  left: 0;
  color: #ffffff;
}

.navs {
  font-family: quicksand;
  font-size: 18px;
  text-align: center;
  display: inline-block;
}

a,
a:link,
a:visited,
a:hover {
  text-decoration: none;
  color: white;
  font-family: quicksand
}

#left {
  margin-right: 15px;
}

Here is my HTML :
<header>
  <h1 id="head">Resume - Mathias Nicolajsen</h1>
</header>
<nav>
  <a href="diagonaler.html"><strong><p class="navs" id="left">Projecter</p></strong></a>
  <strong><p class="navs">Omkring mig</p></strong>
</nav>
<p>Lavet af Mathias Nicolajsen</p>

我该怎么做?

【问题讨论】:

  • 你试过 margin:0 autofloat:none
  • 你能分享你的html sn-p

标签: html css alignment center


【解决方案1】:

只需将text-align: center; 添加到nav { } 即可使元素居中。

【讨论】:

    【解决方案2】:

    header {
      background: #444444;
      z-index: 100;
      transition: height 600ms 0s ease;
      position: fixed;
      top: 0;
      right: 0;
      left: 0;
      color: #ffffff;
    }
    
    #head {
      font-family: quicksand;
      text-align: center;
    }
    
    nav {
      background: #333333;
      position: fixed;
      top: 82px;
      right: 0;
      left: 0;
      color: #ffffff;
      text-align: center;
    }
    
    .navs {
      font-family: quicksand;
      font-size: 18px;
      text-align: center;
      display: inline-block;
    }
    
    a,
    a:link,
    a:visited,
    a:hover {
      text-decoration: none;
      color: white;
      font-family: quicksand
    }
    
    #left {
      margin-right: 15px;
    }
    <header>
      <h1 id="head">Resume - Mathias Nicolajsen</h1>
    </header>
    <nav>
      <a href="diagonaler.html"><strong><p class="navs" id="left">Projecter</p></strong></a>
      <strong><p class="navs">Omkring mig</p></strong>
    </nav>
    <p>Lavet af Mathias Nicolajsen</p>

    尝试添加text-align: center

    【讨论】:

      【解决方案3】:

      只需将您的nav替换为以下CSS

      nav {
              background: #333333;
              position: fixed;
              top: 82px;
              right: 0;
              left: 0;
              color: #ffffff;
              text-align: center;
          }
      

      【讨论】:

        【解决方案4】:

        试试这个。只有text-align: center; 才能解决您的问题。

        header {
          background: #444444;
          z-index: 100;
          transition: height 600ms 0s ease;
          position: fixed;
          top: 0;
          right: 0;
          left: 0;
          color: #ffffff;
        }
        
        #head {
          font-family: quicksand;
          text-align: center;
        }
        
        nav {
          background: #333333;
          position: fixed;
          top: 82px;
          right: 0;
          left: 0;
          color: #ffffff;
          text-align: center;
        }
        
        .navs {
          font-family: quicksand;
          font-size: 18px;
          text-align: center;
          display: inline-block;
        }
        
        a,
        a:link,
        a:visited,
        a:hover {
          text-decoration: none;
          color: white;
          font-family: quicksand
        }
        
        #left {
          margin-right: 15px;
        }
        <header>
          <h1 id="head">Resume - Mathias Nicolajsen</h1>
        </header>
        <nav>
          <a href="diagonaler.html"><strong><p class="navs" id="left">Projecter</p></strong></a>
          <strong><p class="navs">Omkring mig</p></strong>
        </nav>
        <p>Lavet af Mathias Nicolajsen</p>

        【讨论】:

        • 没有看到你的答案和赞成的答案有任何区别。这就是投反对票的原因。
        • 谁对我的答案投了反对票,应该知道,当我开始通过 sn-p 回答它时,当时没有发布任何答案。如果其他人也比我的帖子更快地给出相同的答案,并且如果我的帖子花了一些时间进行 sn-p ,并不意味着这是错误答案或复制。当您投反对票时,其他人不会认为它有帮助。当我发布时,我看到其他人有相同的答案,我也可以投反对票或删除我的答案。相反,我保持开放,认为它可以帮助其他人。当你给他们投票时,这是你的决定,但也应该考虑其他条件。
        猜你喜欢
        • 2016-04-09
        • 2011-05-30
        • 1970-01-01
        • 2020-12-06
        • 2020-10-19
        • 2013-12-23
        • 2016-02-17
        • 2017-05-15
        • 2016-04-14
        相关资源
        最近更新 更多