I'm trying to recreate something like the chrome developer tools element inspector, wherein I can get the element that's currently being hovered.
I want to add a hover effect to every element on the page sort of like this:
:hover {
border: 1px solid blue !important;
}
But the problem is that it'll show me every single parent element up until that point since they are also being hovered.
:hover {
border: 1px solid blue !important;
}
<div>1
<div>1.1
<div>1.1.1</div>
<div>1.1.2</div>
</div>
<div>1.2
<div>1.2.1</div>
<div>1.2.2</div>
</div>
</div>
Since there is no parent selector, I can't check to omit elements that have children that have the :hover property.
I can't use :last-child as the lowest level element may be a grandchild.
Any ways to style just the child-most element being hovered?
Found a solution using jQuery'smouseenter/mouseleave:
$("*")
.mouseenter(function(){
$(this).addClass("hovered");
$(this).parents(".hovered").removeClass("hovered");
})
.mouseleave(function(){
$(this).removeClass("hovered");
$(this).closest(":hover").addClass("hovered");
});
.hovered {
border: 1px solid blue !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>1
<div>1.1
<div>1.1.1</div>
<div>1.1.2</div>
</div>
<div>1.2
<div>1.2.1</div>
<div>1.2.2</div>
</div>
</div>
Related
I know that I can just disable pointer events on the covering elements, but I don't want to do that. I want both the covering and the covered elements to respond to mouse events, so that they both transition into the state corresponding to the hover selector when the mouse pointer touches them both.
You can define a parent container around the DOM elements that you would like to track for hovering. This StackOverflow thread shows a simple approach with a parent div having the class "section". It contains two elements that should both have a border around when hovering (code snippet is cited from this answer post):
<html>
<style type="text/css">
.section { background:#ccc; }
.layer { background:#ddd; }
.section:hover img { border:2px solid #333; }
.section:hover .layer { border:2px solid #F90; }
</style>
</head>
<body>
<div class="section">
<img src="myImage.jpg" />
<div class="layer">Lorem Ipsum</div>
</div>
</body>
</html>
I have defined this hover for div element
div.MyCSSClass:hover
{
background-color: purple;
}
This is my HTML source:
<div class="
<ul class="MyParentCSSClass">
<li>
<div>
<div>
<div class="MyCSSClass">
<!-- I want to remove CSS hover for this div element -->
I want to remove the hover when the div.MyCSSClass is a child of MyParentCSSClass, So I add this to remove the hover style in CSS:
.MyParentCSSClass div.MyCSSClass:hover
{
}
But it did not work. I still see the same hover style.
Is there a way to remove hover in CSS without me creating a new CSS class for my div tag? I want to keep the same name as I have other CSS property uses the 'MyCSSClass'.
Thanks for the suggestion. I tried
background-color: none !important;
But when I look into chrome, that CSS is being over-written by
.MyGrandParentClass div.MyCSSClass:hover
{
background-color: purple;
}
and the html source is
<div class="MyGrandParent">
<ul class="MyParentCSSClass">
<li>
<div>
<div>
<div class="MyCSSClass">
<!-- I want to remove CSS hover for this div element -->
My question is how my 'Remove hover' css rule is being over-written? I have put "!important" to my rule.
.MyParentCSSClass div.MyCSSClass:hover {
background-color: none;
}
This will overwrite the background color given by div.MyCSSClass:hover. if you are keeping MyParentCSSClass div.MyCSSClass:hover empty as MyParentCSSClass div.MyCSSClass:hover {}, it will not overwrite anything or doing nothing actually.
You need to re-write all the previously added styles to the hover event. In the case you specified, please do the following:
.MyParentCSSClass div.MyCSSClass:hover
{
background-color: none;
}
Background-color : none; is not w3c standard. It will work on some browser but according to w3c standard it's not right way.
So try to use background-color: transparent which will work good on all browsers and w3c can validate your code.
Have fun.
I am building a my first site. A big part of the specifications is that it should be very user friendly.
I have a a few images at the top of my home page that have hyperlinks attached. They have a grey border that is changed to pink when hovering over the image.
The problem I have is that I can tab my was to the images and hitting return results in the link being opened so that is fine but I the border does not change colour when I have tabbed to it, so it is difficult if not impossible to know what image you are currently tabbed to.
Border code:
<style>
IMG.HoverBorder {border:1px solid #eee;}
IMG.HoverBorder:hover {border:1px solid #FC359A;}
</style>
The :focus pseudo-class matches elements that have focus, but an img element normally does not have (and need not have) focus. But an a element that constitutes a link is focusable, so you need to use a selector that matches an img element that a child of a focused element. Example (using a 3px thick border just for clarity):
IMG.HoverBorder { border: 3px solid #eee; }
IMG.HoverBorder:hover { border: 3px solid #FC359A; }
a:focus IMG.HoverBorder {border-color: green;}
img { vertical-align: bottom; } /* to make border sit tight around */
<input placeholder="Click here and press TAB">
<img class=HoverBorder src="http://lorempixel.com/100/50" alt=dummy1>
<img class=HoverBorder src="http://lorempixel.com/100/100" alt=dummy2>
I want that when the mouse stay over of the div hello, the paragraph with class nice_day and image dont be affected.
How should do it using css3?
<div class="hello">
<div class="stack"><img src="1.png"/></div>
<div class="overflow"><p class="ilove"><img src="2.png"/>Im a text</p>
<p class ="programming"Im other text</p>
<div class="Have">
<img src="3.png"/>
<p class="nice_day"></p>
</div>
</div>
</div>
Thanks
Option #1 - Add Selectively
This approach looks at the :hover state of the ancestor and only adds styles where desired.
Simple: http://jsfiddle.net/gF7Ju/2/
Multiple Elements: http://jsfiddle.net/gF7Ju/3/
CSS
/* just for formatting so we can see the boxes */
.hello {
border: 1px solid red;
}
.hello > div {
padding: 6px;
border: 1px solid silver;
}
/*
Selector(s) here to determine which element(s) should be impacted by the
parent's hover, and which should not.
*/
.hello:hover div:first-child {
background-color: yellow;
}
HTML
<div class="hello">
<div>Hover effect</div>
<div>No hover effect</div>
</div>
Option #2 - Cancel/Ignore Selectively
Depending on what properties should be modified on hover, you could apply the hover style to the ancestor and then restyle the children (e.g. setting background-color to white, even though the parent's hovered background is yellow).
I prefer the first approach where possible.
IF you want to keep styles of child elements on hover of parent element, you need to cancel the parent style like following
.hello{text-decoration: underline;}
.hello:hover img, .hello:hover .nice_day{text-decoration: none}
Note that we're setting text-decoration: none for the children elements, img; and .nice_day in case they get underlined when .hello is hovered.
you can set again your default css to p.nice_day :
div.hello:hover p.nice_day{
//your default css of .nice_day
}
I'm aware that the :empty pseudo-class will select all elements with no children, but I want to only select elements with text-nodes as children.
I have a bottom-border on my hyperlinks that're a different color than the text, but this is a problem because the hyperlinked images are also getting this underline.
I've tried a *:not(*){ border-bottom-width: 0; } in an attempt to fix this, but it didn't work. Is there a way to select a tags with only text-nodes for children?
If I understand your problem correctly, you are trying to keep your hyperlinked images from being underlined. If so, why not do something like: a img { text-decoration:none }?
Edit: If its the links on img tags you don't want underlined, apply a class to those links with text-decoration:none
NEW ANSWER:
If you want a border under the image, but not the text do this:
a img { border-bottom: 1px solid #000; }
a:emtpy { border: none; }
If you want the opposite (border under the text but not the image) do this:
a:empty { border-bottom: 1px solid #000; }
a img { border: none; }
OLD ANSWER:
If it's just a problem with images that are wrapped in a tags, try:
a img { border-bottom: none; }
Instead of a crazy selector, why not hide the border with a negative margin:
a img {
margin-bottom: -6px;
}
Demo
When the ONLY CHILD of <a> is not an img ...
a:only-child:not(img)
{
border-bottom-width: 1;
}
This cannot be accomplished because of the way border property is applied and rendered outside the top-most box of your anchor - effectively the only way to achieve such an effect with a border would be to negate the property. Sometimes it coult be visually acceptable to use a bottom border in a background colour to overlay over that of of your anchor's - an unreliable practice to be frowned upon. Maybe the effect could be simulated with filters, but I wouldn't count on it being sufficiently well-supported cross-browser.
What I propose is going back to the text-decoration property *while still maintaining a different, independent underline colour` - a neat approach overall, but not without the overhead of an additional element:
<style>
.fancy-underline { color:blue; text-decoration:underline; }
.fancy-underline a { color:black; text-decoration:none; }
</style>
<span class="fancy-underline"><a href="#">I am a fancy link
<img src="//placekitten.com/30/30/" /> with an image in the middle of it
</a></span>
http://jsfiddle.net/ovfiddle/TwmmF/3/
I ended up just using jQuery. I don't believe it's possible with just CSS right now.
jQuery('document').ready(function(){
jQuery("a").each(function(){
if(this.children.length !== 0)
this.style.borderBottomWidth='0';
});
});