【问题标题】:How to align list items this way? [duplicate]如何以这种方式对齐列表项? [复制]
【发布时间】:2021-12-30 20:07:16
【问题描述】:

我很难在导航栏上正确对齐内容。我希望单词(home、players、about、contact)垂直对齐到心形标志的中间。

这就是现在的样子:

我希望它看起来像这样(在 Photoshop 上完成)

我现在的代码是:

body {
  padding: 0;
  margin: 0;
  font-size: 15px;
  font-family: Arial, Helvetica, sans-serif;
  line-height: 1.5;
  background-color: whitesmoke;
}

.container {
  width: 80%;
  margin: auto;
}


/* Header */

header {
  background: black;
  color: whitesmoke;
  padding-top: 30px;
  min-height: 50px;
  border-bottom: #fe1d61 4px solid;
}

header a {
  color: whitesmoke;
  text-decoration: none;
  font-size: 16px;
  font-weight: bold;
}

header ul {
  margin: 0;
  padding: 0 0 20px 0;
  list-style-type: none;
  text-align: center;
}

header li {
  display: inline;
  padding: 0 100px 0 100px;
  height: 30px;
}
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">

<body>
  <header>
    <div class="container">
      <nav>
        <ul>
          <li><a href="index.html">HOME</a></li>
          <li><a href="players.html">PLAYERS</a></li>
          <li>
            <a href="index.html"><img src="img/00nation_logo.png" width="60px"></a>
          </li>
          <li><a href="about.html">ABOUT</a></li>
          <li><a href="contact.html">CONTACT</a></li>
        </ul>
      </nav>
    </div>
  </header>
</body>

很抱歉,如果已经有关于同一件事的问题,但我尝试查找它并找不到任何东西。

【问题讨论】:

    标签: html css


    【解决方案1】:

    试试这个并改变你的 li CSS:

    header ul li{
        display: inline-block;
    }
    

    【讨论】:

      【解决方案2】:

      您可以在图像上应用垂直对齐属性以将其移动到其他图像的中间。 试试这个:

      style="vertical-align: middle;"

      【讨论】:

        【解决方案3】:

        为此,您需要添加更多 CSS,例如

        header li{
            display: inline;
            padding: 0 100px 0 100px;
            height: 30px;
            vertical-align: middle;   // add this line
            line-height: normal;      // add this line
        }
        

        全屏检查

        在这里您可以查看完整的示例

        body{
            padding: 0;
            margin: 0;
            font-size: 15px;
            font-family: Arial, Helvetica, sans-serif;
            line-height: 1.5;
            background-color: whitesmoke;
        }
        
        .container{
            width: 80%;
            margin: auto;
        }
        
        /* Header */
        header{
            background: black;
            color: whitesmoke;
            padding-top: 30px;
            min-height: 50px;
            border-bottom: #fe1d61 4px solid;
        }
        
        header a{
            color: whitesmoke;
            text-decoration: none;
            font-size: 14px;
            font-weight: bold;
        }
        
        header ul{
            margin: 0;
            padding: 0 0 20px 0;
            list-style-type: none;
            text-align: center;
        }
        
        header li{
            display: inline;
            padding: 0 100px 0 100px;
            height: 30px;
            vertical-align: middle;
            line-height: normal;
        }
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width">
            <meta name="description" content="Norwegian-based esports organization with teams in CS:GO, Rocket League, and many other games.">
            <meta name="author" content="@__jmarcelo__">
            <link rel="stylesheet" href="./css/style.css">
            <title>00 Nation DNB | Home</title>
        </head>
        <body>
            <header>
                <div class="container">
                    <nav>
                        <ul>
                            <li><a href="index.html">HOME</a></li>
                            <li><a href="players.html">PLAYERS</a></li>
                            <li><a href="index.html"><img src="https://pbs.twimg.com/media/E-YiHwfXsAEAuiZ.jpg" width="60px"></a></li>
                            <li><a href="about.html">ABOUT</a></li>
                            <li><a href="contact.html">CONTACT</a></li>
                        </ul>
                    </nav>
                </div>
            </header>
        </body>
        </html>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-02-02
          • 1970-01-01
          • 2014-12-23
          • 2021-09-15
          • 1970-01-01
          • 2018-02-04
          • 1970-01-01
          • 2012-09-28
          相关资源
          最近更新 更多