How to change active link color in bootstrap css?

前端 未结 9 825
余生分开走
余生分开走 2021-02-08 03:00

I am using joomla 3 and bootstrap.min.js I am creating menu and giving special class in order to change hover, active, visited links and style of the menu. I could not find how

相关标签:
9条回答
  • 2021-02-08 03:30
            $(function (){
                $('nav ul li a.sub-menu').each(function(){
                    var path = window.location.href;
                    var current = path.substring(path.lastIndexOf('/')+1);
                    var url = $(this).attr('href');
                    if(url == current){
                        $(this).addClass('active');
                    };
                });     
            });
    
    0 讨论(0)
  • 2021-02-08 03:37

    I suggest you creating an ID (#) selector locally for the Div that contains the a links, then take that id name in your style-sheet and override the existing rule.

    For instance,

    #abc a{xxx:xxx;}
    #abc a:active{xxx:xxx;}
    

    Hope this helps.

    0 讨论(0)
  • 2021-02-08 03:40

    In order to do what your are trying to do you must first understand a:hover Must come after a:link and a:visited in the CSS definition in order to be effective. If they are not in this order then they will not work.

    Second is you must understand that if you where thinking (a:active) meant the color of the current link the end user was on, this is incorrect. (a:active) changes the color when you click on the link. You can test this by holding down the mouse button on the link that you made a different color with the (a:active).

    Finally, if you are trying to do this using just CSS you have to add a specific class on the current link

  • that the end user is on. Below I left you an example hope this helps :)

    Your Navigation Bar As Follows
    -Home
    -Russia
    -Italy

    We are on the Italy Page For This Example:

    /*YOUR CSS SHOULD LOOK LIKE THIS*/
    
    /* unvisited link grey */
    #top-menu a:link {
    	color: #777;
    }
    /* visited link grey */
    #top-menu a:visited {
    	color: #777;
    }
    /* mouse over link blue */
    #top-menu a:hover {
    	color: #0CF;
    }
    /* selected link blue */
    #top-menu a:active {
    	color: #0CF;
    }
    /* !IMPORTANT ONLY ADD THIS CLASS TO YOUR ACTIVE PAGE LINK ( Color Blue )*/
    .activePage a {
    	color: #0CF !important
    }
    <div id="top-menu">
      <ul class="nav menu nav-pills topmenu">
        <li><a href="http://yourdomain.com/page1.html">Home</a></li>
        <li><a href="http://yourdomain.com/page2.html">Russian</a></li>
        <li class="activePage"><a href="http://yourdomain.com/page3.html">Italy</a></li><!--Page End User Is On-->
        <!--Look UP ^^^^^^^^ Hope this helps :)-->
      </ul>
    </div>

    Notice I did not put the .activePage tag in the other links? What this does is allow your original colors that you choose in your css for your navigation bar to still take place while the page that is active stays solid with a different color.

    The reason this worked is because I added !important at the end of the color for that separate class.

    .activePage {
      color: #0CF !important
    }
    So to apply this same technique to your other pages it would simply look like this:

    Home Page

    /*YOUR CSS SHOULD LOOK LIKE THIS*/
    
    /* unvisited link grey */
    #top-menu a:link {
    	color: #777;
    }
    /* visited link grey */
    #top-menu a:visited {
    	color: #777;
    }
    /* mouse over link blue */
    #top-menu a:hover {
    	color: #0CF;
    }
    /* selected link blue */
    #top-menu a:active {
    	color: #0CF;
    }
    /* !IMPORTANT ONLY ADD THIS CLASS TO YOUR ACTIVE PAGE LINK ( Color Blue )*/
    .activePage a {
    	color: #0CF !important
    }
    <div id="top-menu">
      <ul class="nav menu nav-pills topmenu">
        <li class="activePage"><a href="http://yourdomain.com/page1.html">Home</a></li>
        <li><a href="http://yourdomain.com/page2.html">Russian</a></li>
        <li><a href="http://yourdomain.com/page3.html">Italy</a></li>
      </ul>
    </div>
    I hope I gave you a solid answer to your question because I hate it when I look all over the web and can't truly find the answer I am looking for.

0 讨论(0)
  • 2021-02-08 03:43
    // Fix navigation menu active links
    $(document).ready(function(){
        var path = window.location.pathname,
            link = window.location.href
        ;
        $('a[href="'+path+'"], a[href="'+link+'"]').parent('li').addClass('active');
    });
    
    0 讨论(0)
  • 2021-02-08 03:43

    If you want to globally change the link colors (or pretty much anything else), create a customized download: http://twitter.github.io/bootstrap/customize.html

    In response to your comment, if you want to override the supplied CSS, you need to create a rule that is more specific. So, either create a selector like #my-custom-container .item-109 .current .active or add a !important to your rule(s) for .item-109 .current .active

    0 讨论(0)
  • 2021-02-08 03:51

    first add php code in link

    <ul class="navbar-nav">
          <li class="nav-item">
            <a class="nav-link <?php if(PAGE == 'index') { echo 'active'; } ?>" href="index.php">Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link <?php if(PAGE == 'about-us') { echo 'active'; } ?>" href="about-us.php">About Us</a>
          </li>
          <li class="nav-item">
            <a class="nav-link <?php if(PAGE == 'contact-us') { echo 'active'; } ?>" href="contact-us.php">Contact Us</a>
          </li>    
        </ul>
    

    then in every page

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