Background normal and background active in css - css

I have the following code in CSS:
#nav li a:active{
background-color:brown;
}
#nav li a.index{background:purple;}
#nav li a.advertiser{background:red;}
#nav li a.publisher{background:green;}
#nav li a.new{background:blue;}
#nav li a.status{background:orange;}
#nav li a:hover {
border-bottom:5px solid brown;
}
and HTML
<ul id="nav">
<li><a class="index"....
<li><a class="advertiser"...
<li><a class="publisher"...
<li><a class="new"....
<li><a class="status".....
</ul>
The thing is that when i click on it, it transforms to brown but it doesn't stay brown.
Where am i mistaking can't realize.

the :active state will only be active when the user clicks on it; anchor or buttons' active states don't persists after the click is finished.
If you want to keep the anchor to brown after a click, you can use jquery to push a specific class when the user clicks on an anchor:
$('#nav a').on('click', function(e){
e.preventDefault(); //?
$('#nav a.active').removeClass('active');
$(this).addClass('active');
})

:active will give the highlights when the button or text is pressed, you need to code jquery to achieve this. Try this example:
<ul>
<li><a class="black classname" id="click">Home</a></li>
<li><a class="black" id="click">about</a></li>
<li><a class="black" id="click">services</a></li>
</ul>
CSS
.black{color:black}
.classname{color:red}
Jquery
$(document).ready(function(){
$("a#click").click(function(){
$("a").removeClass('classname')
$(this).addClass('classname')
});
});
Demo http://jsfiddle.net/XFYjz/

Related

CSS <ul><li> submenu won't close on click, need to swap with close link

when in mobile view my css responsive menu for mobile, will show the submenu when you click on the link, but how do change the link, to something like 'Close X' as the list is long, and if you back on the link it doesn't close, unless you click a different link.
I've managed to create a separate close link. But this now means I've got two links. I want 1 link which will open, then change to the a close link when the submenu is open and when you click on that it will close.
Thanks
css
nav li ul {
display:none;
}
# open
nav ul li a:hover + .hidden, .hidden:hover {
display: block;
position:absolute;
opacity:1.0;
background-color: #343434;
padding:0.5em;
}
# close
nav ul li #close:hover + .hidden, .hidden:hover {
display: none;
position:absolute;
opacity:1.0;
background-color: #343434;
padding:0.5em;
}
menu
<ul>
<li><span id="close">Close X don't show till city clicked</span>
Cities ↓ remove and replace with close or up arrow
<ul class=hidden>
<li>London</li>
<li>New York</li>
<li>Rome</li>
</ul>
</li>
</ul>
This works as you are hoping I believe, The majority of the function is via CSS to keep jquery to a minimum.
CSS Selectors
> selector for immediate children
~ selector for all siblings of the preceding element
Let me know if this isn't what you were hoping for.
Demo
// Add click event to collapse anchor
$("li > a.collapse").click( function() {
// Remove .expand class from parent
$(this).parent().removeClass("expand");
});
// Add click event to expand anchor
$("li > a.expand").click( function() {
// Add .expand class to parent
$(this).parent().addClass("expand");
});
li > a.collapse, li.expand > a.expand {
display: none;
}
li.expand > a.collapse {
display: inherit;
}
li > a.expand ~ ul {
display: none;
}
li.expand > a.expand ~ ul {
display: inherit;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li>
<a class="collapse">Close</a>
<a class="expand">Cities</a>
<ul>
<li>London</li>
<li>New York</li>
<li>Rome</li>
</ul>
</li>
</ul>

Removing the background of a vertical menu li element on hover

I have this vertical that i am trying to customize
<ul class="tracking_nav nav nav-tabs nav-stacked">
<li class="tracking_list_type" style=".tracking_list_type:hover{background-color:none !important}">
<span class="topinfo"><span class="number_plates"><img src="u_online.gif" />Number 10</span></span><span class="moving_status">76 moving</span><br/><span class="link_text">Brother David Cameroon</span><span class="linkso">TrackingPlaybackCommands</span></li>
<li>Tutorials</li>
<li>Practice Editor </li>
<li>Gallery</li>
<li>Contact</li>
</ul>
This is the css
moving_status{
float:right;
color:#76EE00;
font-weight:bold;
}
.linkso{margin-left:13px;}
.topinfo{
display:inline-block;
margin-bottom:5px;
}
.linkso > a{
margin-right:4px;
}
This is the fiddle https://jsfiddle.net/codebreaker87/eoo87zkk/
I have tried .tracking_list_type > li:hover{background-color:none !important;} but i cant seem to target the li element.How can i fix this?.
You need to target hover backgrounds with :hover. For your case, you need the following:
.nav-tabs>li>a:hover
However I would recommend
.nav-tabs>li>a:hover, .nav>li>a:focus, .nav>li>a:hover

keeping 2nd level navigation on first level li:hover visible

We have horizontal 1st level and 2nd level navigations. When a 1st level li is hovered over, a second level ul is displayed horizontally above the first level ul.
How do we make the second level ul remain visible when the mouse leaves the first level li? Is this possible with CSS only..??
Here's the CSS:
ul.ebene1{display:inline;}
ul.ebene1 li{display:inline; list-style-type:none; margin-right:2%;}
ul.ebene2{display:none;}
ul.ebene1 li:hover ul.ebene2{display:block; position:fixed; bottom:6%; width:80%; font-size:0.9em;}
And the HTML:
<ul class="ebene1">
<li>NAV1
<ul class="ebene2">
<li>Nav2</li>
<li>Nav2</li>
</ul>
</li>
<li>NAV1</li>
<li>NAV1
<ul class="ebene2">
<li>Nav2</li>
<li>Nav2</li>
</ul>
</li>
<li>NAV1</li>
</ul>
You can't do that with only CSS. The :hover pseudo classes is triggered when you hover the element. So when leaving the element stops the hover effect.
You could use JQuery to show the submenu.
[EDIT]
A Jquery solution would be this:
HTML
<ul class="ebene1">
<li>NAV1
<ul class="ebene2">
<li>Nav1.1</li>
<li>Nav1.2</li>
</ul>
</li>
<li>NAV2</li>
<li>NAV3
<ul class="ebene2">
<li>Nav3.1</li>
<li>Nav3.2</li>
</ul>
</li>
<li>NAV4</li>
</ul>
CSS
ul.ebene1 {
display:inline;
}
ul.ebene1 li {
display:inline;
list-style-type:none;
margin-right:2%;
}
.ebene2 {
display:none;
position:fixed;
bottom:6%;
width:80%;
font-size:0.9em;
}
JQuery
(".ebene1 li").mouseover( function() {
if( $(this).children('ul').length > 0 ) // check if a ul exists in the li
{
$('.ebene2').hide(); // hide all submenus
$(this).find('ul').show(); // show this submenu
}
});
And a DEMO.

CSS Active Property not working fine

Please have a look at http://jsfiddle.net/mrto2/nD2eB/.
I've given the
#filters li a:active {
border-top: 2px solid #EB2F26;
color: #EB2F26;
}
for Active font and border color but when we click on some menu, its color and border changes but it turns back while mouse release. So how can we fix this?.
There's no plain CSS way to deal with this, since link:active selector applies to <a>s being clicked. You can approach this by defining a li.active css class and adding it to links dynamically via js.
li a.active {
border: 2px red solid;
}
<ul class="nav">
<li><a>Link 1</a></li>
<li><a>Link 2</a></li>
<li><a>Link 3</a></li>
<li><a>Link 4</a></li>
<ul>
// Using Jquery
$('ul.nav li').on('click', function(){
$(this).parent().find('a').removeClass('active');
$(this).addClass('active');
});

link button changes with CSS?

In simple term i would like to achieve something we have on stack overflow top navigator where the color of the button which is active is different BUT using CSS only NOT JavaScript ?
But the a:active does not seems to be working as expected.
I saw some very common examples where this is done by assigning the class to active element in the markup from server or client side. Is that possible only using the CSS ?
<HTML>
<HEAD>
<TITLE>Its Nav</TITLE>
<STYLE type="text/css">
div {
width:200;
}
ul {
list-style:none;
}
ul li a {
padding:2 4 2 4;
border:1px solid blue;
display:block;
text-decoration:none;
}
ul li a:active {
color:green;
}
ul li a:hover {
color:red;
}
ul li a:link {
color:gray;
}
</STYLE>
</HEAD>
<BODY>
<div>
<ul>
<li><a href="#" >link one</a></li>
<li><a href="#" >link two</a></li>
<li><a href="#" >link three</a></li>
<li><a href="#" >link four</a></li>
<li><a href="#" >link five</a></li>
<li><a href="#" >link six</a></li>
<li><a href="#" >link seven</a></li>
<li><a href="#" >link eight</a></li>
<li><a href="#" >link nine</a></li>
<li><a href="#" >link ten</a></li>
</ul>
<div>
</BODY>
</HTML>
If I understand what you want to do, using a:active won't help you. The css pseudo-class active shows when the link is clicked (exactly when it is clicked and before the next page loads). After that, it changes to visited.
Edit: I didn't see that you wanted an exclusively CSS solution first. I believe that what you need is something that has already been answered before. Check How to set a active link as default when page first time load an see if it helps :)
Since you don't want a JavaScript solution, I suggest creating a new class for the active <li> tag in your navigation bar. You can do that dynamically (for example with php) or manually for each page within the html.
As other people already commented, it'd be easier to give you an answer that you can apply directly, if you posted your code.

Resources