Change navbar color in Twitter Bootstrap

后端 未结 12 2013
悲&欢浪女
悲&欢浪女 2020-11-22 00:19

How would I go about modifying the CSS to change the color of the navbar in Twitter Bootstrap?

相关标签:
12条回答
  • 2020-11-22 00:19

    In this navbar CSS, set to own color:

    /* Navbar */
    .navbar-default {
        background-color: #F8F8F8;
        border-color: #E7E7E7;
    }
    /* Title */
    .navbar-default .navbar-brand {
        color: #777;
    }
    .navbar-default .navbar-brand:hover,
    .navbar-default .navbar-brand:focus {
        color: #5E5E5E;
    }
    /* Link */
    .navbar-default .navbar-nav > li > a {
        color: #777;
    }
    .navbar-default .navbar-nav > li > a:hover,
    .navbar-default .navbar-nav > li > a:focus {
        color: #333;
    }
    .navbar-default .navbar-nav > .active > a, 
    .navbar-default .navbar-nav > .active > a:hover, 
    .navbar-default .navbar-nav > .active > a:focus {
        color: #555;
        background-color: #E7E7E7;
    }
    .navbar-default .navbar-nav > .open > a, 
    .navbar-default .navbar-nav > .open > a:hover, 
    .navbar-default .navbar-nav > .open > a:focus {
        color: #555;
        background-color: #D5D5D5;
    }
    /* Caret */
    .navbar-default .navbar-nav > .dropdown > a .caret {
        border-top-color: #777;
        border-bottom-color: #777;
    }
    .navbar-default .navbar-nav > .dropdown > a:hover .caret,
    .navbar-default .navbar-nav > .dropdown > a:focus .caret {
        border-top-color: #333;
        border-bottom-color: #333;
    }
    .navbar-default .navbar-nav > .open > a .caret, 
    .navbar-default .navbar-nav > .open > a:hover .caret, 
    .navbar-default .navbar-nav > .open > a:focus .caret {
        border-top-color: #555;
        border-bottom-color: #555;
    }

    0 讨论(0)
  • 2020-11-22 00:24

    Just add an id to the HTML navbar, such as:

    <nav id="navbar-yellow" class="navbar navbar-default navbar-fixed-top" role="navigation">
    

    With this id you can style the navbar color, but also the links and dropdowns

    Examples applied to different types of navbars

    Black

    Yellow

    Darkblue

    Red (Cherry)

    Darkgreen

    Here is the CSS

    /*
     * Black navbar style
     */
    #navbar-black.navbar-default { /* #3C3C3C - #222222 */
        font-size: 14px;
        background-color: rgba(34, 34, 34, 1);
        background: -webkit-linear-gradient(top, rgba(60, 60, 60, 1) 0%, rgba(34, 34, 34, 1) 100%);
        background: linear-gradient(to bottom, rgba(60, 60, 60, 1) 0%, rgba(34, 34, 34, 1) 100%);
        border: 0px;
        border-radius: 0;
    }
    #navbar-black.navbar-default .navbar-nav>li>a:hover,
    #navbar-black.navbar-default .navbar-nav>li>a:focus,
    #navbar-black.navbar-default .navbar-nav>li>ul>li>a:hover,
    #navbar-black.navbar-default .navbar-nav>li>ul>li>a:focus,
    #navbar-black.navbar-default .navbar-nav>.active>a,
    #navbar-black.navbar-default .navbar-nav>.active>a:hover,
    #navbar-black.navbar-default .navbar-nav>.active>a:focus {
        color: rgba(255, 255, 255, 1);
        background-color: rgba(0, 0, 0, 1);
        background: -webkit-linear-gradient(top, rgba(0, 0, 0, 1) 0%, rgba(0, 0, 0, 1) 100%);
        background: linear-gradient(to bottom, rgba(0, 0, 0, 1) 0%, rgba(0, 0, 0, 1) 100%);
    }
    #sidebar-black, #column-black {
          background-color: #222222;
    }
    #navbar-black.navbar-default .navbar-toggle {
        border-color: #222222;
    }
    #navbar-black.navbar-default .navbar-toggle:hover,
    #navbar-black.navbar-default .navbar-toggle:focus {
        background-color: #3C3C3C;
    }
    #navbar-black.navbar-default .navbar-nav>li>a,
    #navbar-black.navbar-default .navbar-nav>li>ul>li>a,
    #navbar-black.navbar-default .navbar-brand {
        color: #999999;
    }
    #navbar-black.navbar-default .navbar-toggle .icon-bar,
    #navbar-black.navbar-default .navbar-toggle:hover .icon-bar,
    #navbar-black.navbar-default .navbar-toggle:focus .icon-bar {
        background-color: #ffffff;
    }
    
    /*
     * Red navbar style
     */
    #navbar-red.navbar-default { /* #990033 - #cc0033 */
        font-size: 14px;
        background-color: rgba(153, 0, 51, 1);
        background: -webkit-linear-gradient(top, rgba(204, 0, 51, 1) 0%, rgba(153, 0, 51, 1) 100%);
        background: linear-gradient(to bottom, rgba(204, 0, 51, 1) 0%, rgba(153, 0, 51, 1) 100%);
        border: 0px;
        border-radius: 0;
    }
    #navbar-red.navbar-default .navbar-nav>li>a:hover,
    #navbar-red.navbar-default .navbar-nav>li>a:focus,
    #navbar-red.navbar-default .navbar-nav>li>ul>li>a:hover,
    #navbar-red.navbar-default .navbar-nav>li>ul>li>a:focus,
    #navbar-red.navbar-default .navbar-nav>.active>a,
    #navbar-red.navbar-default .navbar-nav>.active>a:hover,
    #navbar-red.navbar-default .navbar-nav>.active>a:focus {
        color: rgba(51, 51, 51, 1);
        background-color: rgba(255, 255, 255, 1);
        background: -webkit-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 1) 100%);
        background: linear-gradient(to bottom, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 1) 100%);
    }
    #sidebar-red, #column-red {
          background-color: #990033;
    }
    #navbar-red.navbar-default .navbar-toggle {
        border-color: #990033;
    }
    #navbar-red.navbar-default .navbar-toggle:hover,
    #navbar-red.navbar-default .navbar-toggle:focus {
        background-color: #cc0033;
    }
    #navbar-red.navbar-default .navbar-nav>li>a,
    #navbar-red.navbar-default .navbar-nav>li>ul>li>a,
    #navbar-red.navbar-default .navbar-brand {
        color: #999999;
    }
    #navbar-red.navbar-default .navbar-toggle .icon-bar,
    #navbar-red.navbar-default .navbar-toggle:hover .icon-bar,
    #navbar-red.navbar-default .navbar-toggle:focus .icon-bar {
        background-color: #ffffff;
    }
    
    /*
     * Darkblue navbar style
     */
    #navbar-darkblue.navbar-default { /* #003399 - #0033cc */
        font-size: 14px;
        background-color: rgba(51, 51, 153, 1);
        background: -webkit-linear-gradient(top, rgba(51, 51, 204, 1) 0%, rgba(51, 51, 153, 1) 100%);
        background: linear-gradient(to bottom, rgba(51, 51, 204, 1) 0%, rgba(51, 51, 153, 1) 100%);
        border: 0px;
        border-radius: 0;
    }
    #navbar-darkblue.navbar-default .navbar-nav>li>a:hover,
    #navbar-darkblue.navbar-default .navbar-nav>li>a:focus,
    #navbar-darkblue.navbar-default .navbar-nav>li>ul>li>a:hover,
    #navbar-darkblue.navbar-default .navbar-nav>li>ul>li>a:focus,
    #navbar-darkblue.navbar-default .navbar-nav>.active>a,
    #navbar-darkblue.navbar-default .navbar-nav>.active>a:hover,
    #navbar-darkblue.navbar-default .navbar-nav>.active>a:focus {
        color: rgba(51, 51, 51, 1);
        background-color: rgba(255, 255, 255, 1);
        background: -webkit-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 1) 100%);
        background: linear-gradient(to bottom, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 1) 100%);
    }
    #sidebar-darkblue, #column-darkblue {
        background-color: #333399;
    }
    #navbar-darkblue.navbar-default .navbar-toggle {
        border-color: #333399;
    }
    #navbar-darkblue.navbar-default .navbar-toggle:hover,
    #navbar-darkblue.navbar-default .navbar-toggle:focus {
        background-color: #3333cc;
    }
    #navbar-darkblue.navbar-default .navbar-nav>li>a,
    #navbar-darkblue.navbar-default .navbar-nav>li>ul>li>a,
    #navbar-darkblue.navbar-default .navbar-brand {
        color: #999999;
    }
    #navbar-darkblue.navbar-default .navbar-toggle .icon-bar,
    #navbar-darkblue.navbar-default .navbar-toggle:hover .icon-bar,
    #navbar-darkblue.navbar-default .navbar-toggle:focus .icon-bar {
        background-color: #ffffff;
    }
    
    /*
     * Darkgreen navbar style
     */
    #navbar-darkgreen.navbar-default { /* #006633 - #009933 */
        font-size: 14px;
        background-color: rgba(0, 102, 51, 1);
        background: -webkit-linear-gradient(top, rgba(0, 153, 51, 1) 0%, rgba(0, 102, 51, 1) 100%);
        background: linear-gradient(to bottom, rgba(0, 153, 51, 1) 0%, rgba(0, 102, 51, 1) 100%);
        border: 0px;
        border-radius: 0;
    }
    #navbar-darkgreen.navbar-default .navbar-nav>li>a:hover,
    #navbar-darkgreen.navbar-default .navbar-nav>li>a:focus,
    #navbar-darkgreen.navbar-default .navbar-nav>li>ul>li>a:hover,
    #navbar-darkgreen.navbar-default .navbar-nav>li>ul>li>a:focus,
    #navbar-darkgreen.navbar-default .navbar-nav>.active>a,
    #navbar-darkgreen.navbar-default .navbar-nav>.active>a:hover,
    #navbar-darkgreen.navbar-default .navbar-nav>.active>a:focus {
        color: rgba(51, 51, 51, 1);
        background-color: rgba(255, 255, 255, 1);
        background: -webkit-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 1) 100%);
        background: linear-gradient(to bottom, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 1) 100%);
    }
    #sidebar-darkgreen, #column-darkgreen {
        background-color: #006633;
    }
    #navbar-darkgreen.navbar-default .navbar-toggle {
        border-color: #006633;
    }
    #navbar-darkgreen.navbar-default .navbar-toggle:hover,
    #navbar-darkgreen.navbar-default .navbar-toggle:focus {
        background-color: #009933;
    }
    #navbar-darkgreen.navbar-default .navbar-nav>li>a,
    #navbar-darkgreen.navbar-default .navbar-nav>li>ul>li>a,
    #navbar-darkgreen.navbar-default .navbar-brand {
        color: #999999;
    }
    #navbar-darkgreen.navbar-default .navbar-toggle .icon-bar,
    #navbar-darkgreen.navbar-default .navbar-toggle:hover .icon-bar,
    #navbar-darkgreen.navbar-default .navbar-toggle:focus .icon-bar {
        background-color: #ffffff;
    }
    
    /*
     * Yellow navbar style
     */
    #navbar-yellow.navbar-default { /* #99ff00 - #ccff00 */
        font-size: 14px;
        background-color: rgba(153, 255, 0, 1);
        background: -webkit-linear-gradient(top, rgba(204, 255, 0, 1) 0%, rgba(153, 255, 0, 1) 100%);
        background: linear-gradient(to bottom, rgba(204, 255, 0, 1) 0%, rgba(153, 255, 0, 1) 100%);
        border: 0px;
        border-radius: 0;
    }
    #navbar-yellow.navbar-default .navbar-nav>li>a:hover,
    #navbar-yellow.navbar-default .navbar-nav>li>a:focus,
    #navbar-yellow.navbar-default .navbar-nav>li>ul>li>a:hover,
    #navbar-yellow.navbar-default .navbar-nav>li>ul>li>a:focus,
    #navbar-yellow.navbar-default .navbar-nav>.active>a,
    #navbar-yellow.navbar-default .navbar-nav>.active>a:hover,
    #navbar-yellow.navbar-default .navbar-nav>.active>a:focus {
        color: rgba(51, 51, 51, 1);
        background-color: rgba(255, 255, 255, 1);
        background: -webkit-linear-gradient(top, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 1) 100%);
        background: linear-gradient(to bottom, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 1) 100%);
    }
    #sidebar-yellow, #column-yellow {
        background-color: #99ff00;
    }
    #navbar-yellow.navbar-default .navbar-toggle {
        border-color: #99ff00;
    }
    #navbar-yellow.navbar-default .navbar-toggle:hover,
    #navbar-yellow.navbar-default .navbar-toggle:focus {
        background-color: #ccff00;
    }
    #navbar-yellow.navbar-default .navbar-nav>li>a,
    #navbar-yellow.navbar-default .navbar-nav>li>ul>li>a,
    #navbar-yellow.navbar-default .navbar-brand {
        color: #999999;
    }
    #navbar-yellow.navbar-default .navbar-toggle .icon-bar,
    #navbar-yellow.navbar-default .navbar-toggle:hover .icon-bar,
    #navbar-yellow.navbar-default .navbar-toggle:focus .icon-bar {
        background-color: #ffffff;
    }
    
    0 讨论(0)
  • 2020-11-22 00:28

    tl;dr - TWBSColor - Generate your own Bootstrap navbar

    Version notes: - Online tool: Bootstrap 3.3.2+ / 4.0.0+ - This answer: Bootstrap 3.0.x

    Available navbars

    You've got two basic navbars:

    <!-- A light one -->
    <nav class="navbar navbar-default" role="navigation"></nav>
    <!-- A dark one -->
    <nav class="navbar navbar-inverse" role="navigation"></nav>
    

    Default color usage

    Here are the main colors and their usage:

    • #F8F8F8: navbar background
    • #E7E7E7: navbar border
    • #777: default color
    • #333: hover color (#5E5E5E for .nav-brand)
    • #555: active color
    • #D5D5D5: active background

    Default style

    If you want to put some custom style, here's the CSS you need to change:

    /* navbar */
    .navbar-default {
        background-color: #F8F8F8;
        border-color: #E7E7E7;
    }
    /* Title */
    .navbar-default .navbar-brand {
        color: #777;
    }
    .navbar-default .navbar-brand:hover,
    .navbar-default .navbar-brand:focus {
        color: #5E5E5E;
    }
    /* Link */
    .navbar-default .navbar-nav > li > a {
        color: #777;
    }
    .navbar-default .navbar-nav > li > a:hover,
    .navbar-default .navbar-nav > li > a:focus {
        color: #333;
    }
    .navbar-default .navbar-nav > .active > a,
    .navbar-default .navbar-nav > .active > a:hover,
    .navbar-default .navbar-nav > .active > a:focus {
        color: #555;
        background-color: #E7E7E7;
    }
    .navbar-default .navbar-nav > .open > a,
    .navbar-default .navbar-nav > .open > a:hover,
    .navbar-default .navbar-nav > .open > a:focus {
        color: #555;
        background-color: #D5D5D5;
    }
    /* Caret */
    .navbar-default .navbar-nav > .dropdown > a .caret {
        border-top-color: #777;
        border-bottom-color: #777;
    }
    .navbar-default .navbar-nav > .dropdown > a:hover .caret,
    .navbar-default .navbar-nav > .dropdown > a:focus .caret {
        border-top-color: #333;
        border-bottom-color: #333;
    }
    .navbar-default .navbar-nav > .open > a .caret,
    .navbar-default .navbar-nav > .open > a:hover .caret,
    .navbar-default .navbar-nav > .open > a:focus .caret {
        border-top-color: #555;
        border-bottom-color: #555;
    }
    /* Mobile version */
    .navbar-default .navbar-toggle {
        border-color: #DDD;
    }
    .navbar-default .navbar-toggle:hover,
    .navbar-default .navbar-toggle:focus {
        background-color: #DDD;
    }
    .navbar-default .navbar-toggle .icon-bar {
        background-color: #CCC;
    }
    @media (max-width: 767px) {
        .navbar-default .navbar-nav .open .dropdown-menu > li > a {
            color: #777;
        }
        .navbar-default .navbar-nav .open .dropdown-menu > li > a:hover,
        .navbar-default .navbar-nav .open .dropdown-menu > li > a:focus {
              color: #333;
        }
    }
    

    Custom colored navbar examples

    Here are four examples of a custom colored navbar:

    Enter image description here

    And the SCSS code:

    $bgDefault   : #e74c3c;
    $bgHighlight : #c0392b;
    $colDefault  : #ecf0f1;
    $colHighlight: #ffbbbc;
    .navbar-default {
      background-color: $bgDefault;
      border-color: $bgHighlight;
      .navbar-brand {
        color: $colDefault;
        &:hover, &:focus {
          color: $colHighlight; }}
      .navbar-text {
        color: $colDefault; }
      .navbar-nav {
        > li {
          > a {
            color: $colDefault;
            &:hover,  &:focus {
              color: $colHighlight; }}}
        > .active {
          > a, > a:hover, > a:focus {
            color: $colHighlight;
            background-color: $bgHighlight; }}
        > .open {
          > a, > a:hover, > a:focus {
            color: $colHighlight;
            background-color: $bgHighlight; }}}
      .navbar-toggle {
        border-color: $bgHighlight;
        &:hover, &:focus {
          background-color: $bgHighlight; }
        .icon-bar {
          background-color: $colDefault; }}
      .navbar-collapse,
      .navbar-form {
        border-color: $colDefault; }
      .navbar-link {
        color: $colDefault;
        &:hover {
          color: $colHighlight; }}}
    @media (max-width: 767px) {
      .navbar-default .navbar-nav .open .dropdown-menu {
        > li > a {
          color: $colDefault;
          &:hover, &:focus {
            color: $colHighlight; }}
        > .active {
          > a, > a:hover, > a:focus, {
            color: $colHighlight;
            background-color: $bgHighlight; }}}
    }
    

    And finally, a little gift

    I've just made a script which will allow you to generate your theme: TWBSColor - Generate your own Bootstrap navbar

    [Update]: TWBSColor now generates SCSS/SASS/Less/CSS code.
    [Update]: From now, you can use Less as the default language provided by TWBSColor
    [Update]: TWBSColor now supports drop down menus colorization
    [Update]: TWBSColor now allows to choose your version (Bootstrap 4 support added)

    0 讨论(0)
  • 2020-11-22 00:28

    Use:

    <nav class="navbar navbar-inverse" role="navigation"></nav>
    
    navbar-inverse {
        background-color: #F8F8F8;
        border-color: #E7E7E7;
    }
    

    25+ Bootstrap Nav Menus Code

    0 讨论(0)
  • 2020-11-22 00:33

    If it's only about changing the color of the Navbar my suggestion would be to use: Bootstrap Magic. You can change the values for different properties of the Navbar and see a preview.

    Download the result as a custom CSS style sheet or as a Less variables file. You can change values with input fields and color pickers.

    0 讨论(0)
  • 2020-11-22 00:34

    Update 2020

    Changing the Navbar color is different (and a little easier) in Bootstrap 4. You can create a custom navbar class, and then reference it to change the navbar without impacting other Bootstrap navs..

    <nav class="navbar navbar-custom">...</nav>
    

    Bootstrap 4.3+

    The CSS required to change the Navbar is much less in Bootstrap 4...

    .navbar-custom {
        background-color: #ff5500;
    }
    /* change the brand and text color */
    .navbar-custom .navbar-brand,
    .navbar-custom .navbar-text {
        color: rgba(255,255,255,.8);
    }
    /* change the link color */
    .navbar-custom .navbar-nav .nav-link {
        color: rgba(255,255,255,.5);
    }
    /* change the color of active or hovered links */
    .navbar-custom .nav-item.active .nav-link,
    .navbar-custom .nav-item:hover .nav-link {
        color: #ffffff;
    }
    

    Bootstrap 4 Custom Navbar Demo

    Changing the active/hover link background color also works with the same CSS, but you must adjust the padding if you want the bg color to fill the full height of the link...

    py-0 to remove vertical padding from the entire navbar...

    <nav class="navbar navbar-expand-sm navbar-custom py-0">..</nav>

    /* change the link color and padding  */
    .navbar-custom .navbar-nav .nav-link {
        color: rgba(255,255,255,.5);
        padding: .75rem 1rem;
    }
    
    /* change the color and background color of active links */
    .navbar-custom .nav-item.active .nav-link,
    .navbar-custom .nav-item:hover .nav-link {
        color: #ffffff;
        background-color: #333;
    }
    

    Bootstrap 4 Change Link and Background Color Demo

    Also see: Bootstrap 4 Change Hamburger Toggler Color


    **Bootstrap 3**
    <nav class="navbar navbar-custom">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">...
        </button>
        <a class="navbar-brand" href="#">Title</a>
      </div>
       ...
    </nav>
    
    
    .navbar-custom {
        background-color:#229922;
        color:#ffffff;
        border-radius:0;
    }
      
    .navbar-custom .navbar-nav > li > a {
        color:#fff;
    }
    
    .navbar-custom .navbar-nav > .active > a {
        color: #ffffff;
        background-color:transparent;
    }
          
    .navbar-custom .navbar-nav > li > a:hover,
    .navbar-custom .navbar-nav > li > a:focus,
    .navbar-custom .navbar-nav > .active > a:hover,
    .navbar-custom .navbar-nav > .active > a:focus,
    .navbar-custom .navbar-nav > .open >a {
        text-decoration: none;
        background-color: #33aa33;
    }
         
    .navbar-custom .navbar-brand {
        color:#eeeeee;
    }
    .navbar-custom .navbar-toggle {
        background-color:#eeeeee;
    }
    .navbar-custom .icon-bar {
        background-color:#33aa33;
    }
    

    Custom Navbar Demo on Bootply


    If the Navbar has dropdowns, add the following to change dropdown color(s):

    /* for dropdowns only */
    .navbar-custom .navbar-nav .dropdown-menu  { 
      background-color: #33aa33;
    }
    .navbar-custom .navbar-nav .dropdown-menu>li>a  { 
      color: #fff;
    }
    .navbar-custom .navbar-nav .dropdown-menu>li>a:hover,.navbar-custom .navbar-nav .dropdown-menu>li>a:focus  { 
      color: #33aa33;
    }
    

    Demo with Dropdown


    If you want to change the theme colors all together (beyond the navbar), see this answer

    0 讨论(0)
提交回复
热议问题