appendChild probably keeps hover css state in IE9 - css

I am strugling to make a simple function work in IE9. It works perfectly in Chrome and FF.
The purpose is to have 2 "ul" lists and move "li" elements between them, on click.
I would like to have li elements with blue background in the first container (MultiListAvailableElements), changing to green on mouse over. And opposite in the second container (MultiListSelectedElements) - green background, changing to blue on mouse over.
The problem is that in IE9 element's behave like they never loose hover state after I append them to opposite list. I have to move mouse over them (and out) to make them look like they should. So, for example, I click blue element from first container, it moves to second container and is still blue (blue in the second container is only for hover state, it should be green by default as the mouse is no longer over the element because the element has moved to different place). Then I have to move mouse over and out the elements in second container to make them come back to normal (green color).
HTML:
<div style="height: 210px; width: 600px;">
<div class="MultiListAvailableElements">
<ul id="OptionsUL">
<li id="id1" onclick="MLAdd(this)">1</li>
<li id="id2" onclick="MLAdd(this)">2</li>
<li id="id3" onclick="MLAdd(this)">3</li>
<li id="id4" onclick="MLAdd(this)">4</li>
<li id="id5" onclick="MLAdd(this)">5</li>
</ul>
</div>
<div class="MultiListSelectedElements">
<ul id="SelectedUL">
</ul>
</div>
</div>
CSS:
/* ------------------------------------ Available Elements --- */
.MultiListAvailableElements {
overflow-y: scroll;
width: 250px;
height: 200px;
border: 1px solid black;
}
.MultiListAvailableElements ul {
margin: 0px;
padding: 0px;
list-style-type: none;
}
.MultiListAvailableElements ul li {
background-color: #e5ecff;
border: 1px solid #c3caff;
width: 180px;
text-align: center;
margin-bottom: 2px;
padding: 2px;
cursor: pointer;
cursor: hand;
font-family: arial;
font-size: small;
}
.MultiListAvailableElements ul li:hover {
background-color: #e5ffec;
border: 1px solid #a3ffaa;
}
/* ------------------------------------ Selected Elements --- */
.MultiListSelectedElements {
overflow-y: scroll;
width: 250px;
height: 200px;
border: 1px solid black;
}
.MultiListSelectedElements ul {
margin: 0px;
padding: 0px;
list-style-type: none;
}
.MultiListSelectedElements ul li {
background-color: #e5ffec;
border: 1px solid #a3ffaa;
width: 180px;
text-align: center;
margin-bottom: 2px;
padding: 2px;
cursor: pointer;
cursor: hand;
font-family: arial;
font-size: small;
}
.MultiListSelectedElements ul li:hover {
background-color: #e5ecff;
border: 1px solid #c3caff;
}
JavaScript:
function MLAdd(Obj) {
document.getElementById("SelectedUL").appendChild(document.getElementById(Obj.id));
document.getElementById(Obj.id).onclick = function () { MLDel(Obj); }
}
function MLDel(Obj) {
document.getElementById("OptionsUL").appendChild(document.getElementById(Obj.id));
document.getElementById(Obj.id).onclick = function () { MLAdd(Obj);
}

You don't actually have to clone. Just remove it and append it elsewhere. Also, you don't have to request the element from the DOM because you are already passing it as an argument to the function (it's coming from the this in the original function call).
function MLAdd(Obj) {
Obj.parentNode.removeChild(Obj);
document.getElementById("SelectedUL").appendChild(Obj);
Obj.onclick = function () { MLDel(Obj); }
}
I'd also cache the results of document.getElementById("SelectedUL") as well by setting it in a variable from within a closure, but I'll leave that for you to figure out.

Related

Sometimes small gap appears between child and parent in an underlined menu

I am working on a navigation with a bottom border. Active links are displayed with a second bottom border.
When I press refresh, sometimes there is a small gap between the two borders like on this screenshot. Why?
When I resize the window the gap is gone.
I have this issue in Chrome 97 on a mac. But I'm not sure if it doesn't occur in other browsers as well.
Does somebody can point me in the right direction?
* {
box-sizing: border-box;
}
a {
text-decoration: none;
}
li,
ul {
list-style-type: none;
padding: 0;
}
li,
ul {
margin: 0;
}
img{
width:100%;
height: auto;
}
.projektmenu-wrap {
border-bottom: 1px solid #000;
list-style: none;
overflow-x: auto;
padding-left: 2rem;
padding-right: 1rem;
padding-top: 0.75rem;
position: -webkit-sticky;
position: sticky;
top: 0;
white-space: nowrap;
z-index: 98;
}
.projektmenu {
align-items: flex-start;
display: flex;
flex-grow: 1;
flex-wrap: nowrap;
justify-content: flex-start;
white-space: nowrap;
width: 100%;
}
.projektmenu__item {
border-bottom: 3px solid transparent;
margin-right: 2em;
margin-top: 0;
}
.projektmenu__item--selected,
.projektmenu__item:hover {
border-bottom: 3px solid #000;
}
<img src="https://picsum.photos/200/300">
<nav class="projektmenu-wrap">
<div class="scrollable-menu">
<ul class="projektmenu">
<li class="projektmenu__item projektmenu__item--selected">
Lorem Ipsum
</li>
<li class="projektmenu__item ">
Dolor est
</li>
</ul>
</div>
</nav>
After some debugging i think the problem could be position: sticky.
Edit:
After some more debugging I realize that I forget something here, before the menu element there is an image with 100%width and auto height, maybe that could be the problem. I added it to the code above
After trying a lot of things, I have added some vw to the border, for the moment that is looking good. But it is hard to tell if it is the solution:
border-bottom: calc(3px + 0.1vw) solid #000
In general, having such a double border solution is always prone to rendering issues, because at different zoom levels the browser renders the borders a bit different.
See also: Google Chrome - Rendering differences when zooming in/out
A solution / workaround for your example would be to define the border width in relative units (e.g. rem), which isn't linked to pixels but based on font sizes (e.g. the root element).
Try this out:
.projektmenu__item--selected,
.projektmenu__item:hover {
border-bottom: 0.2rem solid #000; // alternative unit: em
// or: using a mixture of fixed and relative units
// border-bottom: calc(0.2rem + 3px) solid #000;
}
Also, although not required in this case, I would recommend you to use pseudo-elements like ::after for such markers in lists, it's basically what those are intended for.

CSS removing border on tabbed navigation menu

I'm trying to create a simple tabbed navigation menu in CSS. I am having a hard time getting the bottom border to go away on the active tab. Normally this would not be hard to do, but I also want a line height set.. so I'm using inline-block with various IE and FF fixes. This makes it display the way I want, with the exception of the bottom border.
I have tried numerous methods for getting this to work, including setting up some operators.. but I don't know enough about CSS to determine if I was using them correctly.
Here is my jsfiddle.
(Obviously my CSS skills need work and I could probably simplify the code greatly as well.)
Code:
#tab_menu {
width: 100%;
overflow: hidden;
color: #000000;
border-bottom: #dddddd solid 1px;
}
#tab_menu ul {
padding: 0px;
margin: 0px;
}
#tab_menu li {
list-style: none;
line-height: 42px;
padding-left: 15px;
padding-right: 15px;
font-size: 14px;
font-family: Arial, Helvetica, sans-serif;
display: -moz-inline-stack;
/* Firefox Fix */
display: inline-block;
/* Normal Function */
zoom: 1;
/* IE Fix */
*display: inline;
/* IE Fix */
}
.tab_menu_active {
color: #000000;
border-bottom: none;
border-left: #dddddd solid 1px;
border-right: #dddddd solid 1px;
border-top: #dddddd solid 1px;
}
.tab_menu_active a {
color: #000000;
text-decoration: none;
}
.tab_menu_not_active {
}
.tab_menu_not_active a {
color:#52a4d4;
text-decoration: none;
}
.tab_menu_not_active:hover {
background: #eeeeee;
}
HTML:
<div id="tab_menu">
<ul>
<li class="tab_menu_not_active"> Link 1
</li>
<li class="tab_menu_active"> Link 2
</li>
<li class="tab_menu_not_active"> Link 3
</li>
<li class="tab_menu_not_active"> Link 4
</li>
<li class="tab_menu_not_active"> Link 5
</li>
</ul>
</div>
DEMO
for the #tab_menu I removed the overflow:hidden;
#tab_menu {
/*overflow: hidden;*/
}
to the .tab_menu_active I have added this styles, that will add border bottom white and with position manipulation will overidre the gray border color.
.tab_menu_active {
border-bottom:solid 1px #fff;
position:relative;
top:1px;
}
.tab_menu_active a {
position:relative;
top:-1px;
}
The problem you have is your entire #tab_menu has a bottom border. There are a couple ways you could solve this, but first I'll give you some details about how to simplify your css.
Give the li's the class tab, that means that every tab you have will all get the same css. On the active one, give it a second class, active. In your css definitions, define that all tab's should have the same css (instead of having duplicate css in tab_menu_active and tab_menu_not_active).
I would recommend giving them all a border on the bottom, and then removing that border in the active one.
Here's a forked jsfiddle.

Why is my a:hover css working differently in Firefox?

I cannot figure this out. I HAVE DONE RESEARCH so please, no comments about me doing more research. Also, I am a noob, so be nice ;)
Here's my site: http://library.skybundle.com/
Hover your mouse over the two black rectangles in the main blue nav bar (header area). The a:hover should make the color change to a gray. The ISSUE is that in Chrome, this looks perfect. But, in Firefox, the padding-right isn't long enough or something, so there is always a small black rectangle at the far right side of the "Educational Courses" button (this will only be visible when hovering your cursor over the button). In other words, the gray box doesn't go all the way to the right-side end of the button area upon mouse hover. I just don't understand why this looks and works great in Chrome, but bugs out in Firefox...
Believe me when I say I have tried everything I can to fix it using Firebug in Firefox. If you play around with it using an editor in your browser, you will see that if you try to make the padding longer for Firefox, it pops the whole button down onto a new line. So to fix THAT problem, you must make the container wider, but then the original problem comes back. It's a circle of problems and I'm sure one of you geniuses out there will see a simple solution that I am missing.
Please help. Thanks!
EDIT :
Here's my JSFiddle and code. Notice how it looks great in Chrome but not in Firefox?
http://jsfiddle.net/S4st8/
HTML:
<div id="navigation">
<div id="navigation-inner">
<div id="page-nav">
<div id="primary-nav">
<ul id="top-menu">
<li id="li-left">Product Training Videos</li>
<li id="li-right">Educational Courses</li>
</ul>
</div>
</div>
</div>
</div>
CSS:
#navigation {
background: url(http://library.skybundle.com/wp-content/themes/business-services/library/styles/colour-images/mu-nav.jpg) repeat-x;
margin: 0px;
padding: 0px;
height: 40px;
width: 100%;
}
#navigation-inner {
margin: 0px auto;
padding: 0px;
height: 48px;
width: 960px;
}
#page-nav {
margin: 0px;
padding: 0px;
height: 40px;
width: 960px;
}
div#primary-nav {
position: relative;
display: block;
float: left;
margin: 0;
padding: 0;
}
ul#top-menu {
margin: -5px 0.325em 0 0.325em;
position: absolute;
padding: 0;
z-index: 100;
top: 0;
left: 3em;
width: 367px;
}
ul#top-menu li {
line-height: 3em;
list-style-type: none;
height: 49px;
background-color: #2C2C2C;
float: left;
}
li#li-right {
list-style-position: inside;
border-left: 2px solid #5E5E5E;
}
ul#top-menu li a {
font-weight: bold;
font-size: 11pt;
text-decoration: none;
padding: 15px 10px 16px 10px;
color: #ffffff;
}
ul#top-menu li a:hover {
text-decoration: none;
width: auto;
color: #ffffff;
background-color: #505354;
padding: 15px 10px 17px 10px;
}
its because a tags (anchor tags) have a default display property of inline
due to CSS Box Model you would need to adjust your padding and set the anchor tags display property to display:block;
the display block allows the anchor tag to fill the whole space of the LI tag
change ul#top-menu li a to this:
ul#top-menu li a{
color: #FFFFFF;
font-size: 11pt;
font-weight: bold;
display: block; /* add this */
padding: 0 10px; /* add this */
}
the CSS Box Model adds the content + padding + border + margin
https://developer.mozilla.org/en-US/docs/Web/CSS/box_model
Take a look at this CSS rule:
li#li-right {
border-left: 2px solid #5E5E5E;
list-style-position: inside;
}
Dropping list-style-position: inside seems to fix your issue in Firefox (and still works in Chrome), but I haven't tested the implications in other browsers. The CSS rule is documented here.
The reason why : browsers apply their own css if you don't specify it. Firefox added the space for your bullet (somehow)
FF :
list-style-image none
list-style-position outside
list-style-type disc
GooChrome :
list-style-image: none;
list-style-position: inside;
list-style-type: none;
User JasonSperske gave you a fixing solution,
i invite you to RESET your css.
PS. in the meantime, you are invited to see : https://stackoverflow.com/help AND http://sscce.org/
Reading and understanding those pages will give you few reputations points

Preventing Hover event of a Div triggering on parent Div?

When I mouseover .mensal DIV it will trigger the mouseover the parent .opera DIV, which seems wrong to me. I just want the "highlight" effect to to work on the child .opera DIV.
#operaContent {
padding: 0 50px 0 50px;
position: relative;
overflow: visible;
}
#operaContent .opera {
display: block;
border: 1px solid #FFFFFF;
border-bottom: 1px solid #DDDDDD;
padding: 5px;
margin-bottom: 10px;
height: 120px;
background-color: #0A8ECC;
}
#operaContent .opera:hover {
border: 1px solid #AAAAAA;
background-color: #DDDDDD;
cursor: pointer;
}
.mensal {
position: absolute;
top: 1px;
left: 8px;
z-index: 3;
display: block;
}
<div id="operaContent">
<div class="opera">
<div class="mensal">
DIV
</div>
</div>
</div>
By definition, hovering over a child, hovers over the parent as well. There is no "blocking" in html events.
There are two method chains, the bubble and the capture.
Any event taking place in the W3C event model is first captured until
it reaches the target element and then bubbles up again.
http://www.quirksmode.org/js/events_order.html
The only way you're going to stop this is to prevent the bubbling by adding javascript to your page to prevent the chain. This is simple in jQuery
$('.mensal').hover(function(e){
e.stopPropagation();
});
It occurs to me that this answer is completely unhelpful when dealing with CSS. Javascript events dont deal with CSS selectors or preventing them.
Unfortunately, with CSS alone, I do not know of a way to accomplish this (and even in javascript it can get tricky). CSS 4 selectors will allow you to specify the subject of the selector http://dev.w3.org/csswg/selectors4/#subject so that you can do something like
#operaContent .opera:hover! .mensal:not(:hover) { /*your css*/ }
but this isnt implemented yet, and is still under development for the draft.
EDIT:
Here is a javascript (jQuery) implementation that should work for you
​
$(function(){
$('.opera').hover(function() {$(this).addClass('hoverIntent')},
function(){ $(this).removeClass('hoverIntent'); }
);
$('.opera .mensal').hover(function() {
$(this).parent('.opera').removeClass('hoverIntent');
});
})​
and the modified CSS
#operaContent {
padding: 0 50px 0 50px;
position: relative;
overflow: visible;
}
#operaContent .opera {
display: block;
border: 1px solid #FFFFFF;
border-bottom: 1px solid #DDDDDD;
padding: 5px;
margin-bottom: 10px;
height: 120px;
background-color: #0A8ECC;
}
#operaContent .opera.hoverIntent {
border: 1px solid #AAAAAA;
background-color: #DDDDDD;
cursor: pointer;
}
.mensal {
position: absolute;
top: 1px;
left: 8px;
z-index: 3;
display: block;
}​
and the obligitory working demo: http://jsfiddle.net/WB6Ty/

CSS, menu:active not working

I have a simple menu:
<ul id="menu2">
<li> Home</li>
<li> About us</li>
<li> Contacts</li>
</ul>
And in css file I have:
#menu2 {
background: black;
float: left;
list-style: none;
margin: 0;
padding: 0;
width: 220px;
}
#menu2 li {
margin: 0;
padding: 0;
}
#menu2 a {
background: black;
border-bottom: 1px solid #393939;
color: #ccc;
display: block;
margin: 0;
padding: 9px 16px;
text-decoration: none;
}
#menu2 a:hover {
background: black url("../images/select.png") left center no-repeat;
color: #fff;
padding: 9px 16px;
}
#menu2 a:active {
background: black url("../images/select.png") left center no-repeat;
color: #fff;
padding: 9px 16px;
}
Everything works well except for #menu2 a:active not working at all while #menu2 a:hover (with same rules) works well. What is the problem? Did I miss something?
It is working as expected. I colored the active state red.
Try clicking on en element and hold the button down. The background will be red.
You don't see a change, because you CSS for hover and active are identical!
Sample
http://jsfiddle.net/dqH3F/1/
Sample contains
#menu2 a:active {
background: red url("../images/select.png") left center no-repeat;
color: #fff;
padding: 9px 16px;
}
Can you provide more details of what exactly is not working and/or a demo. Looking at the code it appears to be fine.
The :active state refers to when a link is pressed, so if you press and hold your mouse button down on your menu item it should be working as expected since hover works active has the same properties.
A link with :active will not remain that way when your on the page it links too, it reverts back to a normal link.
Your background for :hover and :active in the code above is the exact same.
Are you trying to set a background x and y position on active?
Without image background and different colors (for testing) your code works fine: see here http://jsfiddle.net/stursby/9Pccb/

Resources