I have a problem with my navbar, can someone help me ?
I have different links (<a>) that "unfold" an unordered list (<ul> of links <li><a><li>).
My <ul> is set to opacity: 0, and in my CSS I have :
.tab ul {
opacity: 0;
pointer-events: none;
}
.tab a:focus + ul {
opacity: 1;
transform: translateY(0px);
pointer-events: all;
}
(this is only the part of the code that doesn't work)
When I click the link, the tab does unfold, and pointer-events is set to all (I know it is because the sublinks color changes when hovered).
My problem is that whenever I click a sublink, the focus on the tab link is lost, and it seems to set pointer-events to none BEFORE executing the sublink's action (a basic href="page.html").
I tried removing :focus (as if the tab was in constant focus state), and the sublinks redirect me on the pages as intended.
I also tried putting pointer-events: all on ul > li > a, but it didn't work.
That's why I came to the conclusion that the tab link lose focus and the pointer event is set to none before the sublink is actually clicked.
Does someone know a way to work around this and execute the sublink's action before the focus loss ?
The tab is unfold and the sublink is hovered (mouse invisible on the screenshot thought) :
If I understood you correctly, then maybe add this solution can help you:
.tab a + ul:hover {
opacity: 1;
transform: translateY(0px);
pointer-events: all !important;
}
Because the focus of the link is lost, the submenu disappears, but you can leave the submenu by ul:hover.
P.S. By the way, on the example of the site that you specified, add this code at the end, it quite works (I tried using Web Inspector):
.menu-loisirs a + ul:hover, .menu-sports a + ul:hover {
opacity: 1;
transform: translateY(0px);
pointer-events: all !important;
}
For this kind of interaction, :focus-within is usually quite suitable. When the "trigger" link and wrapper of its children share same wrapper, it makes sense to bind logic to state of that parent wrapper. To make such structure keyboard accessible, can use .wrapper:not(:hover):not(:focus-within) .trigger + .content { /* visually hidden state styles */}:
.tab:not(:hover):not(:focus-within) a + ul {
display: none; /* for brevity, use accessible hiding in real world */
}
/* not necessary */
p[id]:not(:target) {
display: none;
}
a[href]:empty:before {
content: '๐ ' attr(href);
}
p[id]:empty:before {
content: 'ยง ' attr(id)
}
<div class="tab">
<ul>
<li>
</li>
<li>
</li>
</ul>
</div>
<div class="tab">
<ul>
<li>
</li>
<li>
</li>
</ul>
</div>
<p id="a"></p>
<p id="a1"></p>
<p id="a2"></p>
<p id="b"></p>
<p id="b1"></p>
<p id="b2"></p>
Check the browser support (not supported in IE11 and older browsers) and consult accessibility and screen reader support before using in production.
Related
I have a problem with my hamburger menu, especially with the icons. My HTML looks like this:
<input type="checkbox" id="check"/>
<header class="IndexHeader">
<nav class="navigation">
<label class="Hamburger" for="check">โฐ</label>
<label class="schliessen" for="check">โ</label>
<ul class="IndexNavliste">
a list...
</ul>
</nav>
</header>
I want my .schliessen label to rotate 180 degrees when I click on the .Hamburger label, so that it is like an animation effect. I tried it like this with jQuery:
$(".Hamburger").click(function(){
$(".schliessen").css("transform","rotate(180deg)");
});
That didn't really work for me. Also, I think I need a transition in it so that I really can see it when I click the label. I also tried to do it in the CSS directly so when my checkbox is checked. That works but I couldn't see the animation and my hover effect didn't work anymore after that...
.Hamburger{
display: block;
transition: 500ms;
}
.schliessen{
transition: 500ms;
}
.schliessen:hover{
color: black;
}
#check:checked + .IndexHeader .navigation .Hamburger{
display: none;
}
#check:checked + .IndexHeader .navigation .schliessen{
display: block;
transform: rotate(180deg);
}
The issue with transition and transform:rotate("180deg") is happening because transform never gets a new value from first click onwards on Hamburger. To overcome that I have added and removed classes on clicks.
Have also removed un-necessary CSS and added what is required for this problem :
Here is the working code:
codepen.io/emmeiWhite/pen/XWjBXRY?editors=1111
I wrote a radio list with image animate. When it was hovered and checked, it can change to another image.
it's works on all of the browsers but on IE its not work when I hover on it.
I don't know if I wrote some css wrong or miss something about ie issue?
the html is:
<ul>
<li>
<input type="radio" id="f-option" name="gender">
<label for="f-option" class="gender female"><img src="images/52x42.png"></label>
<div class="check"></div>
</li>
</ul>
and my css is:
.user-form ul li label.gender.female {
background-image: url(../images/female.png);
}
.user-form input[type=radio]:checked ~ label.gender.female,
.user-form input[type=radio]:hover ~ label.gender.female {
background-image: url(../images/female-checked.png);
}
its works all of the browser but not work on ie
can anybody help me fix it?
The online demo is in this bottom of page:
http://bestinthink.com/wg/buy-p1.html
Try the below. I'm accessing the female class directly after the label as .gender and .female were siblings.
.user-form ul li label.gender.female {
background-image: url(../images/female.png);
}
.user-form input[type=radio]:checked ~ label .female,
.user-form input[type=radio]:hover ~ label .female {
background-image: url(../images/female-checked.png);
}
Your example works in modern browsers because they treat hovering over your label element as though you are hovering over the radio element itself, even though the radio element is set to visibility:hidden. IE doesn't behave that way. You have to explicitly hover over the radio element โ which you can't do because you've hidden it โ to see your input[type=radio]:hover styles.I suggest changing the background-image on label:hover rather than input[type=radio]:hover. The below code might work for you.
.user-form input[type=radio]:checked ~ label.gender.female,
.user-form label.gender.female:hover {
background-image: url(../images/female-checked.png);
}
Use it and let me know how it goes.โบ
I'll update if I find a less hacky solution.
I know the title is too general. I couldn't find a specific title.
Here is the sample https://jsfiddle.net/Exlord/1soagsL5/
HTML
<ul>
<li>item1</li>
<li>item1</li>
<li>item with submenu >
<ul>
<li>item1</li>
<li>item1</li>
</ul>
</li>
<li>item1</li>
<li id='clickable'>item1</li>
</ul>
JavaScript
var el = document.getElementById('clickable');
el.addEventListener('click', function(e) {
console.log(e.target);
}, false);
CSS
ul {
list-style: none;
margin: 0;
padding: 0;
width: 200px;
max-width: 100%;
}
li {
background: gray;
padding: 5px;
border: solid 1px black;
position: relative;
}
li:hover > ul {
display: block;
}
#media all and (min-width: 481px) {
ul ul {
display: none;
position: absolute;
left: 100%;
top: 0;
}
}
#media all and (max-width: 480px) {
ul ul {
display: none;
}
}
Try it, it's a very simple menu, hover the middle item item with submenu, a simple submenu will be shown with simple CSS :hover.
Click on the last item, it has a click event, it will fire correctly.
Now open this in Chrome's mobile device mod with touch simulation.
Click/touch the item with submenu, the submenu will open inside the parent element. This is the expected behavior.
Now click/touch the last element. The click event will not fire.
How can I fix this with CSS only hover?
UPDATE : As far as I can tell the problem is that on first touch the last hovered element (li with submenu) gets unhovered and the submenu ul gets hidden and the parent ul's height shrinks and the li element that was under the touched point moves up, so its not under the touched point anymore and it does not get clicked/touched !!!!
I think you figured out the cause on your own. The click target does seem to move away before event reaches the element.
The easiest solution I see is to use some supplementary Javascript to open and close the submenu on clicks. You use Javascript for the #clickable event anyway so a little more won't be detrimental, just as long as you keep the :hover for cases where Javascript is fails to load.
Original Answer
Touch devices do not have a hover state, due to the hardware
interface. Browser makers chose a compromise to allow for sites which
rely on it. Whenever a hover event/style is defined for an element the
first tap will trigger that effect instead of the click. A second tap
should, at least in most cases, trigger the original click event.
For your particular case you don't have a ul within the
li#clickable, but the :hover is still detected and the browser
behaves as it was designed. Excluding your #clickable from the hover
event will theoretically fix it (I am not able to test it myself at
this time).
li:not(#clickable):hover > ul {
display: block;
}
I have a simple menu with a hover state:
<nav id="menu">
<div>Home</div>
<div>
1
<nav>
<div>1.1</div>
<div>1.2</div>
<div>1.3</div>
</nav>
</div>
</nav>
CSS:
#menu > div > nav {
display: none;
position: absolute;
z-index: 9999;
}
#menu > div:hover > nav {
display: block;
}
But the :hover state never ends. After another tap (somewhere else) :hover still stays. Can I get around this without javascript? (Fiddle)
It seems like the only way to get rid of :hover is to :focus somewhere (element.focus()) or hover on something else.
No. Hover states are partially broken on some mobile devices simply because you can't hover over an element. You will have to use javascript.
You can use the hover media query to disable hover states on iOS:
https://developer.mozilla.org/en-US/docs/Web/CSS/#media/hover
My aim is to make the p tag with show class get displayed when the span (with Show Me text) is clicked. I had tried to do this using the :focus pseudo-selector but using this method makes the p tag get displayed only till somewhere else is clicked, where the p tag gets hidden again.
Is there any way to make the :focus selector display even after clicking away (or) is there a different/better way (without using JS) to display the p when the Show Me is clicked and make it stay that way even after clicking outside?
Fiddle Demo
HTML CODE
<span class="span1" tabindex="0">Show Me</span>
<p class="show" >This will show on click</p>
CSS CODE
body {
display: block;
}
.show {
display: none;
}
.span1:focus ~ .show {
display: block;
}
The :focus pseudo-class will not work because, as the naming indicates, it is applicable only when the focus is on the element and there is no way to make the focus 'stick' even after we click elsewhere.
An alternate way of achieving this would be to use the :target [1] pseudo-class/selector. This would make the p display whenever the link is clicked, since it is deemed as 'the target'.
body {
display: block;
}
.show {
display: none;
}
#content:target {
display: block;
}
<a class="span1" href='#content'>Show Me</a>
<p class="show" id='content'>This will show on click</p>
[1] - :target selector is not supported by IE <= 8.