Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

css - Bootstrap: center some navbar items

I would like to have a bootstrap navbar where some nav items are left-justified, some are right justified, and some are centered in the remaining space between them.

<div class="navbar">
    <div class="navbar-inner">
        <ul class="nav">
            <li><a>Left</a></li>
        </ul>
        <ul class="nav nav-center">
            <li><a>Center 1</a></li>
            <li><a>Center 2</a></li>
        </ul>
        <ul class="nav pull-right">
            <li><a>Right</a></li>
        </ul>
    </div>
</div>

This jsfiddle http://jsfiddle.net/b7whs/ shows this--I'd like the Center 1 and Center 2 nav items to be together, centered in the navbar. Is this possible?

There are a lot suggestions here on SO about how to do this; none of them have worked for me. Is this even possible?

In my case, what's on the left and right will always be the same size, so I think it should be feasible.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You just needed a couple of styles to get the behaviour that I think you wanted. It looks like you were going the display:inline-block route to center the elements, so I'll just continue along that approach. To your existing styles, add/modify definitions so that these styles are included:

.nav.nav-center {
    margin:0;
    float:none;
}

.navbar-inner{
    text-align:center;
}

With that, the two options should move to the exact center of your navigation bar. Here's a JSFiddle example to show you what this would look like. I hope this is what you were looking for! If not, let me know and I'll be happy to help further.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...