如何将多个 a 标签彼此对齐?

我试图在导航栏上将多个 a 标签彼此相邻对齐,但是每当我将它们对齐在中心时,它们就会彼此堆叠在顶部。我尝试了 disply: flex;但这将文本左对齐


body {

    overflow-x: hidden;

}


.navbar {

    background-color: #ffffff;

    border-bottom: 2px solid #000000;    

    margin: 0px -2000px;

    padding: 0px 2000px;

}


.cent {

    text-align: center;

  }

  


  .navbar a {

    display: block;

    color: #000000;

    padding: 14px;

    text-decoration: none;  

  }

 

<html>

    <head>

        <title>entertainment</title>

        <link rel="stylesheet" href="entertainment.css">

        <script src="entertainment.js"></script>

    </head>


    <body> 

            

    <div class="navbar">

        <div class="cent">

        <a href="#home">Home</a>

        <a href="#news">News</a>

        <a href="#contact">Contact</a>

        </div>

    </div>

    </body>

    </html>


猛跑小猪
浏览 86回答 4
4回答

侃侃尔雅

您目前已将 .navbar a 设置为 display: block。这会导致元素位于彼此下方。保留元素'块属性但仍将它们并排显示,请将 block 替换为 inline-block。这是工作代码片段:body {&nbsp; overflow-x: hidden;}.navbar {&nbsp; background-color: #ffffff;&nbsp; border-bottom: 2px solid #000000;&nbsp; margin: 0px -2000px;&nbsp; padding: 0px 2000px;}.cent {&nbsp; text-align: center;}.navbar a {&nbsp;&nbsp;&nbsp; /* This is what changed */&nbsp; display: inline-block;&nbsp;&nbsp;&nbsp; color: #000000;&nbsp; padding: 14px;&nbsp; text-decoration: none;}<html><head>&nbsp; <title>entertainment</title>&nbsp; <link rel="stylesheet" href="entertainment.css">&nbsp; <script src="entertainment.js"></script></head><body>&nbsp; <div class="navbar">&nbsp; &nbsp; <div class="cent">&nbsp; &nbsp; &nbsp; <a href="#home">Home</a>&nbsp; &nbsp; &nbsp; <a href="#news">News</a>&nbsp; &nbsp; &nbsp; <a href="#contact">Contact</a>&nbsp; &nbsp; </div>&nbsp; </div></body></html>

MMMHUHU

将 flex 与 justify-content: center 一起使用,并去掉包裹导航栏元素的双 divbody {&nbsp; overflow-x: hidden;}.navbar {&nbsp; background-color: #ffffff;&nbsp; border-bottom: 2px solid #000000;&nbsp; margin: auto 0;&nbsp; display: flex;&nbsp; justify-content: center;}.cent {&nbsp; text-align: center;}.navbar a {&nbsp; display: block;&nbsp; color: #000000;&nbsp; padding: 14px;&nbsp; text-decoration: none;}<div class="navbar">&nbsp; &nbsp; <a href="#home">Home</a>&nbsp; &nbsp; <a href="#news">News</a>&nbsp; &nbsp; <a href="#contact">Contact</a></div>

慕妹3242003

将 .navbar a 的显示规则更改为 inline-block;.navbar a {&nbsp; display: inline-block;&nbsp;}body {&nbsp; &nbsp; overflow-x: hidden;}.navbar {&nbsp; &nbsp; background-color: #ffffff;&nbsp; &nbsp; border-bottom: 2px solid #000000;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; margin: 0px -2000px;&nbsp; &nbsp; padding: 0px 2000px;}.cent {&nbsp; &nbsp; text-align: center;&nbsp; }&nbsp;&nbsp;&nbsp; .navbar a {&nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; color: #000000;&nbsp; &nbsp; padding: 14px;&nbsp; &nbsp; text-decoration: none;&nbsp;&nbsp;&nbsp; }&nbsp;<html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <title>entertainment</title>&nbsp; &nbsp; &nbsp; &nbsp; <link rel="stylesheet" href="entertainment.css">&nbsp; &nbsp; &nbsp; &nbsp; <script src="entertainment.js"></script>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <div class="navbar">&nbsp; &nbsp; &nbsp; &nbsp; <div class="cent">&nbsp; &nbsp; &nbsp; &nbsp; <a href="#home">Home</a>&nbsp; &nbsp; &nbsp; &nbsp; <a href="#news">News</a>&nbsp; &nbsp; &nbsp; &nbsp; <a href="#contact">Contact</a>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; </body>&nbsp; &nbsp; </html>

RISEBY

您可以使用&nbsp;display: inline-block&nbsp;使所有内容内联。这里有一些示例 -&nbsp;https://www.w3schools.com/css/css_display_visibility.aspbody {&nbsp; &nbsp; overflow-x: hidden;}.navbar {&nbsp; &nbsp; background-color: #ffffff;&nbsp; &nbsp; border-bottom: 2px solid #000000;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; margin: 0px -2000px;&nbsp; &nbsp; padding: 0px 2000px;}.cent {&nbsp; &nbsp; text-align: center;&nbsp; }&nbsp;&nbsp;&nbsp; .navbar a {&nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; color: #000000;&nbsp; &nbsp; padding: 14px;&nbsp; &nbsp; text-decoration: none;&nbsp;&nbsp;&nbsp; }&nbsp;<html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <title>entertainment</title>&nbsp; &nbsp; &nbsp; &nbsp; <link rel="stylesheet" href="entertainment.css">&nbsp; &nbsp; &nbsp; &nbsp; <script src="entertainment.js"></script>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <div class="navbar">&nbsp; &nbsp; &nbsp; &nbsp; <div class="cent">&nbsp; &nbsp; &nbsp; &nbsp; <a href="#home">Home</a>&nbsp; &nbsp; &nbsp; &nbsp; <a href="#news">News</a>&nbsp; &nbsp; &nbsp; &nbsp; <a href="#contact">Contact</a>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; </body>&nbsp; &nbsp; </html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5