【问题标题】:Centering logo in header / navigation bar [duplicate]在标题/导航栏中居中徽标[重复]
【发布时间】:2020-12-29 13:08:16
【问题描述】:

我正在尝试创建一个站点并从导航栏开始。我正在尝试将徽标居中并给 subs/links(?不知道它们叫什么,a-tags)多一点空间。

我尝试关注一些视频教程和 Stackoverflow 答案,但它们导致左侧 3 个链接更向下,而右侧 3 个链接高于其他所有链接,并导致整体标题大小几乎翻了一番。如果有人可以帮助我,我将不胜感激! :) 可能有一些不需要的垃圾代码,或者总体上是一种不好的做法,但如果不清楚我在网页设计方面是一个完全的初学者:p

body { 
  margin: 0;
  font-family: 'Open Sans', sans-serif;
}

.header {
  overflow: hidden;
  background-color: #262e28;
  padding: 10px 10px;
  box-shadow: 0px 2px 8px #888888;
}

.header a {
  color: white;
  text-align: center;
  padding: 12px;
  text-decoration: none;
  font-size: 14px; 
  line-height: 25px;
  border-radius: 4px;
}

.header a.logo {
  font-size: 25px;
  color: white;
    display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}

.header a:hover {
  color: #aeaeae;
}

.header a.active {
  color: #aeaeae;
}

.header-right {
  float: right;
}

.header-left {
  float: left;
}

@media screen and (max-width: 500px) {
  .header a {
    float: none;
    display: block;
    text-align: left;
  }
  
  .header-right {
    float: none;
  }
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="assets/style.css">
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300&display=swap" rel="stylesheet"></head>
<body>

<div class="header">
  <div class="header-right">
    <a href="#"><b>1</b></a>
    <a href="#"><b>2</b></a>
    <a href="#"><b>3</b></a>
  </div>
  <a href="#" class="logo">LOGO</a>
  <div class="header-left">
    <a href="#"><b>4</b></a>
    <a href="#"><b>5</b></a>
    <a href="#"><b>6</b></a>
  </div>
</div>

</body>
</html>

【问题讨论】:

    标签: html css


    【解决方案1】:

    您可以在 header 类上使用 display:flex 属性和 align-items: center

    现在使用flex 属性是一种推荐方式,因为它在现代浏览器上的响应速度非常快

    将此代码添加到您的标头类中

    .header {
      overflow: hidden;
      background-color: #262e28;
      padding: 10px 10px;
      box-shadow: 0px 2px 8px #888888;
      display: flex;
      align-items: center;
      flex-direction: row-reverse;
    }
    

    实时工作示例:

    body {
      margin: 0;
      font-family: 'Open Sans', sans-serif;
    }
    
    .header {
      overflow: hidden;
      background-color: #262e28;
      padding: 10px 10px;
      box-shadow: 0px 2px 8px #888888;
      display: flex;
      align-items: center;
      flex-direction: row-reverse;
    }
    
    .header a {
      color: white;
      text-align: center;
      padding: 12px;
      text-decoration: none;
      font-size: 14px;
      line-height: 25px;
      border-radius: 4px;
    }
    
    .header a.logo {
      font-size: 25px;
      color: white;
      display: block;
      margin-left: auto;
      margin-right: auto;
      width: 50%;
    }
    
    .header a:hover {
      color: #aeaeae;
    }
    
    .header a.active {
      color: #aeaeae;
    }
    
    .header-right {
      float: right;
    }
    
    .header-left {
      float: left;
    }
    
    
    @media screen and (max-width: 500px) {
      .header a {
        float: none;
        display: block;
        text-align: left;
      }
      .header-right {
        float: none;
      }
    }
    <html>
    
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" type="text/css" href="assets/style.css">
      <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300&display=swap" rel="stylesheet">
    </head>
    
    <body>
    
      <div class="header">
        <div class="header-right">
          <a href="#"><b>1</b></a>
          <a href="#"><b>2</b></a>
          <a href="#"><b>3</b></a>
        </div>
        <a href="#" class="logo">LOGO</a>
        <div class="header-left">
          <a href="#"><b>4</b></a>
          <a href="#"><b>5</b></a>
          <a href="#"><b>6</b></a>
        </div>
      </div>
    
    </body>
    
    </html>

    【讨论】:

    • 我认为你搞砸了,因为左边的标题在右边,右边的标题在左边。
    • 没有?就像它按预期工作一样?根据给出的示例,我认为这不是 OP 想要的结果。
    • @TheGrandJ 现在怎么样?
    • 你去 :) 现在它与示例匹配。希望我们的答案之一是他们需要的答案。用您输入的所有细节对您的出色答案进行投票
    • @kekerino 欢迎您 - 乐于助人。如果对您有帮助,您现在可以投票支持其他人的回答。我已经投票支持您的问题,以便您可以投票支持其他答案。谢谢
    【解决方案2】:

    要解决您的问题,只需将 CSS where display: block 更改为 display: inline-block

    inline-block 告诉它在同一行而不是单独渲染块。

    body { 
      margin: 0;
      font-family: 'Open Sans', sans-serif;
    }
    
    .header {
      overflow: hidden;
      background-color: #262e28;
      padding: 10px 10px;
      box-shadow: 0px 2px 8px #888888;
    }
    
    .header a {
      color: white;
      text-align: center;
      padding: 12px;
      text-decoration: none;
      font-size: 14px; 
      line-height: 25px;
      border-radius: 4px;
    }
    
    .header a.logo {
      font-size: 25px;
      color: white;
        display: inline-block;
      margin-left: auto;
      margin-right: auto;
      width: 50%;
    }
    
    .header a:hover {
      color: #aeaeae;
    }
    
    .header a.active {
      color: #aeaeae;
    }
    
    .header-right {
      float: right;
    }
    
    .header-left {
      float: left;
    }
    
    @media screen and (max-width: 500px) {
      .header a {
        float: none;
        display: inline-block;
        text-align: left;
      }
      
      .header-right {
        float: none;
      }
    }
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="assets/style.css">
    <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300&display=swap" rel="stylesheet"></head>
    <body>
    
    <div class="header">
      <div class="header-right">
        <a href="#"><b>1</b></a>
        <a href="#"><b>2</b></a>
        <a href="#"><b>3</b></a>
      </div>
      <a href="#" class="logo">LOGO</a>
      <div class="header-left">
        <a href="#"><b>4</b></a>
        <a href="#"><b>5</b></a>
        <a href="#"><b>6</b></a>
      </div>
    </div>
    
    </body>
    </html>

    【讨论】:

      【解决方案3】:

      使用&lt;centre&gt;&lt;/centre&gt; 标签。

      【讨论】:

      【解决方案4】:

      这是pen,我让一切正常。

      只需使用CSS Flexbox,我就能得到你想要的一切。

      我添加了display:flex; and flex-direction: row;,以便导航栏内容沿行轴弯曲。此外,我添加了justify-content: space-between;,以便在主(行)轴上,内容与中间的空格对齐。

      接下来,我简单地将 display: flex; 属性与相应的属性一起添加到导航栏中的 3 个项目中,以便它们左对齐(又名flex-start)、居中和右对齐(又名flex-end)。

      我强烈建议您尝试使用 Flexbox 或 Grid 来完成所有响应式布局工作。要想真正学好 Flexbox,我建议通过Flexbox Froggy的所有步骤

      body { 
        margin: 0;
        font-family: 'Open Sans', sans-serif;
      }
      
      /* added display flex, specified row and space-between */
      .header {
        overflow: hidden;
        background-color: #262e28;
        padding: 10px 10px;
        box-shadow: 0px 2px 8px #888888;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
      }
      
      .header a {
        color: white;
        text-align: center;
        padding: 12px;
        text-decoration: none;
        font-size: 14px; 
        line-height: 25px;
        border-radius: 4px;
      }
      
      .header a.logo {
        font-size: 25px;
        color: white;
        display: flex;
        justify-content: center;
        align-items: flex-end;
        padding-top: 0;
      }
      
      .header a:hover {
        color: #aeaeae;
      }
      
      .header a.active {
        color: #aeaeae;
      }
      
      /* added display flex properties */
      .header-right {
        display: flex;
        justify-content: flex-end;
        align-items: center;
      }
      
      /* added display flex properties */
      .header-left {
        display: flex;
        justify-content: flex-start;
        align-items: center;
      }
      
      @media screen and (max-width: 500px) {
        .header a {
          float: none;
          display: block;
          text-align: left;
        }
        
        .header-right {
          float: none;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-25
        • 1970-01-01
        • 2016-06-22
        • 2020-06-17
        • 2016-09-16
        • 1970-01-01
        • 2012-11-28
        相关资源
        最近更新 更多