Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

html - How can I have perfectly centered navigation bar with equally wide links?

So I want to have a centered navigation menu/bar on my website, kind of like the one on Ovh's website. What CSS do I put (don't include colors). Here is my HTML:

<div class = "nav">
        <div class = "top_page">
            <h1 class = "mainTitle">Exatreo</h1>

            <p class = "slogan">Slogan</p>
            <br/>
        </div>
        <div class = "container">
            <ul class="center-links">
                <a href="" class = "content" >Home</a></li>
                <a href="/about" class = "content" >About</a>
                <a href="/projects" class = "content" >Projects</a>
                <a href="/recruit" class = "content" >recruting</a>
                <a href="/help" class = "content" >Help</a>
            </ul>
        </div>
        <br />
        <br />
    </div>

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Firstly, your HTML is invalid, ul can only have li as direct children...but we can use a nav element instead.

Method 1: Table Layout

.center-links {
  display: table;
  table-layout: fixed;
  width: 100%;
  text-align: center;
}
.center-links a {
  display: table-cell;
  border: 1px solid grey;
}
<div class="container">
  <nav class="center-links">
    <a href="" class="content">Home</a>
    <a href="/about" class="content">About</a>
    <a href="/projects" class="content">Projects</a>
    <a href="/recruit" class="content">recruting</a>
    <a href="/help" class="content">Help</a>
  </nav>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...