CSS - 均匀分布的导航栏

时间:2015-10-30 11:54:20

标签: css twitter-bootstrap nav

我正在尝试均匀分布导航栏,使其在整个页面上传播,而不是像目前那样在右侧留下间隙。

我如何通过CSS实现这一目标?

HTML

    <nav class="navbar navbar-default" role="navigation">
        <!--– Brand and toggle get grouped for better mobile display –-->
        <div class="container">
        <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        </button>

        <!--– Collect the nav links, forms, and other content for toggling –-->
        <div class="collapse navbar-collapse in navbar-ex1-collapse">
        <ul id="menu-main-menu" class="nav navbar-nav divider"><li id="menu-item-20" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-8 current_page_item menu-item-20 active"><a title="Find Your Home" href="http://localhost/radleigh/find-your-home/">Find Your Home</a></li>
<li id="menu-item-21" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-21"><a title="About Us" href="http://localhost/radleigh/about-us/">About Us</a></li>
<li id="menu-item-22" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22"><a title="Helping You Move" href="http://localhost/radleigh/helping-you-move/">Helping You Move</a></li>
<li id="menu-item-23" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-23"><a title="News" href="http://localhost/radleigh/news/">News</a></li>
<li id="menu-item-24" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-24"><a title="Get in Touch" href="http://localhost/radleigh/get-in-touch/">Get in Touch</a></li>
</ul>        </div><!--– /.navbar-collapse –-->
        </div><!--– /.container –-->
        </div>
    </nav>

CSS

/* Navigation
-------------------------*/
.navbar-default {
    background-color: #002e5d;
    border-radius: 0;
    font-size: 18px;
}

#toggle-navbar {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    width: 50px;
    height: 100px;
    line-height: 95px;
    font-size: 2em;
    font-weight: 900;
    color: #fff;
    text-align: center;
    background: #ffcd00;
}

#toggle-navbar:hover {
  cursor: pointer;
}

.navbar {
    border-radius: 0;
    border: none;
    margin-bottom: 0;
}

.navbar-default .navbar-nav li {
    padding: 0 32px;
    height: 40px;
    margin: 10px 0;
}

.navbar-default .navbar-nav li:not(:last-child) {
    border-right: solid 1px #FFF;
}

.navbar-default .navbar-nav li a {
    color: #FFF;    
    padding: 10px;
}

.navbar-default .navbar-nav li a:hover, .navbar-default .navbar-nav li a:focus {
    color: #e8e8e8; 
}

.navbar-collapse {
    display: table;
    float: right;
    height: auto !important;
    max-height: none;
    overflow: hidden;
    padding: 0;
    position: relative;
}

.navbar-collapse.in {
  overflow: hidden;
}

.navbar-default .navbar-toggle .icon-bar {
    background-color: #FFF;
}

.navbar-default .navbar-toggle {
    border-color: #FFF;
}

.navbar-default .navbar-toggle .icon-bar:hover {
    background-color: #FFF;
}

.navbar-default .navbar-toggle:focus, .navbar-default .navbar-toggle:hover {
    background-color: #1b66b2;
}
.navbar-default .navbar-nav>.active>a, .navbar-default .navbar-nav>.active>a:focus, .navbar-default .navbar-nav>.active>a:hover {
    background-color: #1b66b2;
    color: #FFF;
}

.navbar .current_page_item {
    background: #1b66b2;    
    border-right: none !important;
    height: 60px !important;
    margin: 0 !important;
    font-weight: 700;   
}

.navbar .current_page_item:not(:first-child) {
    margin: 0 -1px !important;
}

.navbar .current_page_item a {
    line-height: 40px !important;   
}

.menu-main-menu {
    font-size: 17px;    
}

我做了一个jsfiddle:https://jsfiddle.net/xfj7ummm/

2 个答案:

答案 0 :(得分:3)

默认的Bootstrap样式不支持您所需的行为,因此您需要覆盖其中的几个。为了方便起见,并防止这些新样式(可能)影响其他元素,我在导航栏中添加了id字段:

<div id="myNavbar" class="navbar-header">

然后我用自己的方式覆盖不需要的样式:

#myNavbar {
    float: none;
}
#myNavbar > .navbar-collapse {
    float: none;
}
#menu-main-menu {
    float: none;
    text-align: center;
}
#menu-main-menu > li {
    float: none;
    display: inline-block;
}

正如您在此jsfiddle update中所看到的,导航标签现在位于页面的中心位置。

答案 1 :(得分:0)

要均匀分发您的链接,您可以display:table使用navbar display:table-cell li .navbar-collapse,我会在768px以上的媒体查询中使用.navbar.navbar-default { background-color: #002e5d; border-radius: 0; font-size: 18px; } .navbar.navbar-default .navbar-nav > li > a { color: #fff; } .navbar.navbar-default .navbar-nav > li > a:hover, .navbar.navbar-default .navbar-nav > li > a:focus { color: #e8e8e8; } .navbar.navbar-default .navbar-nav > .active > a, .navbar.navbar-default .navbar-nav > .active > a:focus, .navbar.navbar-default .navbar-nav > .active > a:hover { background-color: #1b66b2; color: #FFF; } .navbar.navbar-default .current_page_item { background: #1b66b2; font-weight: 700; } .navbar.navbar-default .navbar-toggle .icon-bar, .navbar.navbar-default .navbar-toggle .icon-bar:hover { background-color: #fff; } .navbar.navbar-default .navbar-toggle { border-color: #fff; } .navbar.navbar-default .navbar-toggle:focus, .navbar.navbar-default .navbar-toggle:hover { background-color: #1b66b2; } @media (max-width: 991px) and (min-width: 768px) { .navbar.navbar-default .navbar-nav > li > a { padding-left: 5px; padding-right: 5px; white-space: nowrap; } } @media (min-width: 768px) { .navbar.navbar-default .navbar-nav { margin: 0; display: table; width: 100%; } .navbar.navbar-default .navbar-nav > li { display: table-cell; float: none; padding-top: 10px; padding-bottom: 10px; } .navbar.navbar-default .navbar-nav > li > a { text-align: center; } .navbar-default .navbar-nav li:not(:last-child) { border-right: solid 1px #FFF; } .navbar.navbar-default .navbar-header .navbar-brand { display: none; } } @media (max-width: 767px) { .navbar.navbar-default { padding-top: 10px; padding-bottom: 10px; } .navbar.navbar-default .navbar-collapse { border: none; box-shadow: none; } .navbar.navbar-default .navbar-header .navbar-brand { color: #fff; font-size: 28px; font-weight: bold; } } @media (max-width: 360px) { .navbar.navbar-default .navbar-header .navbar-brand { font-size: 24px; } }以避免更改在移动视口上导航。

还添加了一些媒体查询以补偿链接标题的字体大小和长度。

*您的HTML也存在一些不一致之处。你的关闭navbar-header div应该在<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <nav class="navbar navbar-default" role="navigation"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-ex1-collapse" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button><a class="navbar-brand" href="http://localhost/radleigh/find-your-home/">Find Your Home</a> </div> <div class="collapse navbar-collapse navbar-ex1-collapse"> <ul id="menu-main-menu" class="nav navbar-nav"> <li id="menu-item-20" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-8 current_page_item menu-item-20 active"><a title="Find Your Home" href="http://localhost/radleigh/find-your-home/">Find Your Home</a> </li> <li id="menu-item-21" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-21"><a title="About Us" href="http://localhost/radleigh/about-us/">About Us</a> </li> <li id="menu-item-22" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22"><a title="Helping You Move" href="http://localhost/radleigh/helping-you-move/">Helping You Move</a> </li> <li id="menu-item-23" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-23"><a title="News" href="http://localhost/radleigh/news/">News</a> </li> <li id="menu-item-24" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-24"><a title="Get in Touch" href="http://localhost/radleigh/get-in-touch/">Get in Touch</a> </li> </ul> </div> </div> </nav>之前,而不是之后。

请参见整页的示例代码段。

string input = "Jan (10) Anna (100)";
string[] split = input.Split(new[] {' ', ')', '('}, StringSplitOptions.RemoveEmptyEntries);
Jan , 10 , Anna , 100