Psuedo-class to keep div visible on focus / active? - css

I have a div that is hidden until the user clicks on a link. Using the a:active + div selector the div is shown. I then have div:active, div:focus to keep the div visible.
Whilst making the div appear was simple enough, keeping it visible is the problem I have. If I click on the div (taking the active off the link and placing focus / active on the div) then the div disappears again.
I have tried using div:hover and while that shows the div (even after I click on it) when I hover off the div still disappears. Why are :active and :focus not being applied to my div?
Example: http://jsfiddle.net/pJWPE/

You could take a different approach, using the :target pseudoclass instead. The best way to illustrate this is with an example:
#box {
display: none;
}
#box:target {
display: block;
}
Open Close
<div id="box">content</div>
Warning, I'm not sure what browser support is like for this example. It works in my version of Chrome.

Why are :active and :focus not being applied to my div?
Because :active and :focus have some restrictions:
6.6.1.2. The user action pseudo-classes :hover, :active, and :focus
Interactive user agents sometimes change the rendering in response to user actions. Selectors provides three pseudo-classes for the selection of an element the user is acting on.
The :active pseudo-class applies while an element is being activated by the user. For example, between the times the user presses the mouse button and releases it. On systems with more than one mouse button, :active applies only to the primary or primary activation button (typically the "left" mouse button), and any aliases thereof.
The :focus pseudo-class applies while an element has the focus (accepts keyboard or mouse events, or other forms of input).
There may be document language or implementation specific limits on which elements can become :active or acquire :focus.
http://www.w3.org/TR/2011/REC-css3-selectors-20110929/#dynamic-pseudos
A <div> isn't any form of input (<textare>, <input>, ...) or otherwise interactive element (like <a>, <audio>, <video>). It's just a container. Neither :focus nor :active are going to be applied.
Use :target instead as suggested by Casey.

Context is a menu containing submenus, which I want to be shown on clicking the related menu, and keep it showed while clicking a link. (quite similar to you).
HTML Markup :
<nav>
<ul>
<li>
<a href="#" >
</a>
<div>
<ul>
<li>
submenu link1</li>
<li>
submenu link2</li>
<li>
submenu link3</li>
</ul>
</div>
</li>
</ul>
</nav>
For Firefox & Chrome web browser, I personnally use :
nav > ul > li > a:focus + div, nav > ul > li > a + div:active {
display : block;
}
The first selector references to my link anchor, in order to get which submenu show when it is clicked/tab selected. The second one, is to keep the submenu div opened as you click on a link (it keep showing the active div).
This works great for me but not for IE unfortunatly.

Related

How does browser select a css property value to render from two different css selectors rules?

I'm working on a Bootstrap project, the first task is to use Bootstrap navbar to build a navigation in the header. I want to use collapse functionality of Bootstrap to create a dropdown-menu div, when users hover or active a nav item, it will be visible. I want to make this dropdown-menu div 100% width of the screen.
Here's the sample code:
<ul class="nav navbar-nav">
<li class="dropdown">
<a ...>ABOUT</a>
<div class="dropdown-menu">
</li>
</ul>
The question is: the position property of dropdown-menu is absolute, which makes this div positioned based on the closest positioned ancestor. In the sample code is "li" tag, because in Bootstrap there's a rule:
.nav>li{
position: relative;
}
When I add a rule, which could make this "li" tag position static:
.navbar-nav>li {
position: inherit;
}
This rule is ignored by browser, I found this from Chrome inspect. When I uncheck the nav>li rule, my rule works. I want to know how does browser choose which of the css property value to render when it gets two rules on the same element and same property?
Thanks.
In short, specificity. The more specific the selector is, the higher priority it will be given.
For more details, look here... https://developer.mozilla.org/en/docs/Web/CSS/Specificity
Having said that, your rule appears to be wrong, as you need a dot to indicate a class...
.navbar-nav>li {
position: inherit;
}

<li> not pushing down subsequent <li>

I am creating a responsive mobile version of my website. As you can see in this jsfiddle, when a user hovers <li> item, the menu opens up but overlays the <li> instead of expanding the menu.
I have added display: block in various places and width: 100% hoping that would fix the issue.
What do I need in the code for the menu to expand with a <ul> tag correctly once hovering over an <li> tag?
It's not pushing the menu down because it's positioned absolutely, therefore being removed from the normal document flow.
Remove all position:absolute statements from the li ul selector.
Working demo:
http://jsfiddle.net/306pfgs3/1/

Using CSS only, is it possible to make a UL list have no bullet when it has only one item?

Is it possible to use CSS only, to have a solution that works even on IE8, that
when a <ul> list has 2 or more items, then keep the bullets in front of the items as usual.
when there is only 1 item, then don't show the bullet, because it can be strange that a single item has a bullet in front of it.
An example is shown on http://jsfiddle.net/SzQL6/37/
<input id="an-input">
<ul id="the-list">
<li>error msg 1</li>
<li>error msg 2</li>
</ul>
(The error messages should be left-aligned with the left edge of the input box above the error messages, when there are bullets and when there is no bullet.)
Using JavaScript, I could do it: http://jsfiddle.net/SzQL6/40/
But can this be done by CSS only (and that it can work on IE8)?
Yes, you can do it with CSS only, though it is only compatible with CSS3 (which anybody with a updated browser will have, or as Guffa pointed out, IE >= 9), by using the :only-child pseudo-class selector:
.error-list { list-style-position: inside; margin-top: 0; color: #f77 }
.error-list li:only-child { list-style-type: none; }
The rule is being applied to any li element that is the only child of its parent (.error-list).
According to MDN:
The :only-child CSS pseudo-class represents any element which is the only child of its parent. This is the same as :first-child:last-child or :nth-child(1):nth-last-child(1), but with a lower specificity.
See the example on this fiddle:
http://jsfiddle.net/SzQL6/41/

safari css link not working

i have a ul style menu looking like this:
<ul>
<li>
<span>Hem</span>
</li>
etc.. etc..
</ul>
the li element has an image as background that changes on hover. i do not want to display the text inside the container and therefore it has visibility: hidden.
Fun thing now; you are still able to click the link in IE and FF but in safari only the mouseover works, but you can't click the link. Changing the visibility of the span container does make it possible to click the link.
Question now is how to change the css code so that it behaves like IE and FF?
I don't want to show the text, but i do want to be able to click the menu - duh!
Rather than setting the visibility of the anchor to hidden consider the following:
ul li a
{
display:block;
width:100%;
height:100%;
text-indent: -9999px; // Hide the text
}
Working Example

CSS round corners problem

I have this problem with CSS round corners. The link to example is here: http://www.xplus.dnawrot.nazwa.pl/
As you can see at the top on the dark blue background is a dropdown menu. The problem concern hover state on the first ul li list(items: oferta, klienci, akademia) wchih has children. As you will notice if you make mouse focus on the oferta item it display round corners properly, but if you go down to second ul li list (drop down - "rozwiazania branzowe", "produkty", etc) right side of round corner disapperas. And it's logical becouse there is CSS style for li element and a element, and if the mouse leaves an a element the hover is removed. Does anyone has a solution for this problem?
If the description is not clear let me know and I'll try to explain more detailed.
Regards,
Dave
Maybe you could try to wrap "a" and "ul" in a "span" tag and then use css to control span:hover instead of a:hover.
<li class="">
<span>
<a class="sf-with-ul" title="Klienci" href="http://www.xplus.dnawrot.nazwa.pl/pl/klienci">
<ul style="display: none; visibility: hidden;">
</span>
</li>
I haven't tested it but you could give it a try.

Resources