I want a items that have the tag class to be grayed (with a rounded border) when the mouse moves over and when the item(s) are clicked.
A sample item might look like this <span class="tag">Some value</span>
I was hoping this would give the desired result, (tag:hover works as hoped, but tag:active doesn't):
.tag{
}
.tag:hover{
background:#CCC;
padding:4px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
.tag:active{
background:#CCC;
padding:4px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
You can use jQuery to permanently add a class to your span. You could do it like this:
.tag:hover, .tag.everclicked{
background:#CCC;
padding:4px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
And in jQuery:
$(document).ready(function()
{
$('.tag').click(function()
{
$(this).addClass('everclicked');
});
});
http://jsfiddle.net/r4gQQ/2/
You have the same CSS styles defined for both pseudo-classes. An :active element will usually have :hover too (depending on the browser), as it is applied whilst the mouse button is down on the element.
If you want the styles to stay applied when you've clicked and moved away from the element, you'll need to use JavaScript or, alternatively, give the element a tabindex attribute and use the :focus pseudo-class. Using #Marnix's test case, for example, http://jsfiddle.net/r4gQQ/1/.
First things first, the CSS for your :hover and :active states is identical, so you'll not see any change!
According to the specs for CSS on w3.org, :active used to only apply to hyperlinks in CSS1, but that's no longer the case. I couldn't find any explicit statement regarding what elements it works for now, so I'm just going to assume it works everywhere unless someone else knows better. It could be that it still only applies to elements the user is meant to "activate" by clicking, such as links and form buttons.
If :active does indeed apply to any kind of element now, then the above CSS should work as you want, provided you change the :active style to be different from the :hover style of course!
If it doesn't, then either there are still restrictions in CSS as to where you can use hover, or there's a bug in the CSS engine of your browser that restricts where you can apply the :active style in a way that goes against the current CSS specs. If the latter is the case, then you could use javascript to add a class on mousedown to the clicked element and remove it again on mouseup or mouseout. You can then style the active class in CSS as normal.
Related
My css:
.pic img:hover {
border: 1px black solid;
}
My html:
<div class="pic">
<img src="hey.jpg">
<img class="overlay" src="overlay.jpg">
</div>
I have an "overlay.jpg" that is placed in the bottom corner of the first image.
Problem with the css is that the overlay gets the :hover effect too.
The actual code is very complicated, I know the best solution would be to add a class to the first image and hover only that, but that will take more time than what i currently have to get this fixed.
The code has a class for the overlay images though, so I'm asking if theres a way to override the hover by setting some other css for the overlay class.
However .overlay:hover { border: none; } does not do the trick, even if i put it after the css above.
Your rule is less specific than the previous one so it is getting overridden.
You have to change it to:
.pic .overlay:hover { border: none; }
The concept
Specificity is the means by which a browser decides which
property values are the most relevant to an element and gets to be
applied. Specificity is only based on the matching rules which are
composed of selectors of different sorts.
How is it calculated?
The specificity is calculated on the
concatenation of the count of each selectors type. It is not a weight
that is applied to the corresponding matching expression.
In case of specificity equality, the latest declaration found in the
CSS is applied to the element.
Reference: Specificity
As long as overlay always comes after the first image, you can use :first-child to pick only the first image:
.pic img:first-child:hover {
border: 1px black solid;
}
It's supported in everything (excluding IE6, but I don't count that as a thing.)
You can also use the :not selector:
.pic img:not(.overlay):hover {
border: 1px black solid;
}
This way you won't have to worry about the order of the images and also don't have to worry about specificity by including another reset style.
I have many SELECT elements on a page, the problem is that in IE11 they have an outline which I try to remove it using CSS :
select{outline:none;}
It works fine as long as , it is the only style element! As soon as I add more style e.g background-color to SELECT, the outline:none does not work anymore!
It should work in IE and for SELECT element. However in other browser, there is no outline applied to element by default.
thanks in advance
By using a bit CSS you can get same style across all browser.
Check the DEMO.
select{
outline:none;
border:1px solid #E4E4E4;
padding:10px;
width:200px;
background: none repeat scroll 0 0 #F7F7F7;
}
In my site, every post has a bottom border. I've applied a
article:last-child {border-bottom:none;}
so that the last post doesn't have a border at the bottom, but it's still showing.
What am I doing wrong?
last-child will fail if you have any element other than article so use last-of-type instead.
Because the last-child is nav on your website, CSS will look for last article child but the last child is nav hence the selector goes wrong.
Where on the other hand last-of-type will select the last article element of it's parent.
Use this instead and it will work for sure
.content-pad-left article:last-of-type {
border-bottom:none;
}
last-child is not available in IE 8. article tag can be still solved by using modernizr.
To make backward compatible, you want to use first-child -
article { border-top: 1px solid #eee; }
article:first-child { border-top: none;}
This is what your current website look like in IE 8.
I have found an irritating bug in IE 8-10 that prevents a parent's active state being triggered. It appears that if a child of the parent element is the target of the click event the active state on the parent element is not triggered.
Here is a working example. If you click the text inside the <li> the element wont change colour. If you click inside an <li> anywhere other than on the <p> child the element will turn blue.
This is a problem as it pretty much renders the css :active pseudo state useless in IE if the element has any children.
Has anyone encountered this problem before, and even better found a way round it?
Here's an easy workaround: add a css rule to the paragraph.
Working example
CSS
ul { list-style: none; }
li { height: 50px; margin-bottom: 4px; background: red; }
li:active { background: blue; }
p:active { background: blue; height: 100%;}
I have fixed the issue by preventing pointer-events on the child element. This way the :active state is triggered directly on the parent and doesn't need to be propagated. The only downside of this solution is you cannot attach an event listener (not even a css `:hover selector) to the child anymore. So you have to move all your event listeners to the parent.
.child { pointer-events: none; }
Here is jsFiddle https://jsbin.com/govelabuca/1/edit?css,output
Just uncomment the last line in css and compare the result in IE and other modern browser
You could add another CSS selector for the <p> tag so your
li:active { background: blue; }
will become
li:active, li p:active { background: blue; }
I would suggest you would use javascript or jquery for that when you click a child element, perform the active state of of the parent.
I've stumbled upon this on IE11. I was writing a drag-n-drop styling logic using this approach suggested by Martin.
In my case I have a row with td cell elements and using :active for the parent tr does the job for other browsers. For IE, I've added a CSS rule to target the cells (tr.myRowClass > td:active) and modified the if condition in my custom JS logic executed during the mousemove event handler of the cells:
if (style.getPropertyValue('cursor') == 'auto' || document.querySelectorAll(":active").length > 0) {
The remaining task is to find the target element:
Determine which element the mouse pointer is on top of in Javascript
Find if class has border in css only, by using attribute selector or by any other means.If it is not having then apply the border.Intention is Not to fall back on to either jquery. Is this possible to acheive?
Edited to include response from #nag (OP):
In IE8 there is no border for select. So I'm trying to do a css reset like this:
select, input[type="file"], textarea {
border: solid 1px #7F9DB9;
}
The problem is it is overriding any preexisiting style because of specificity. I tried to use expression filter but with DocType in IE8 it does not seem to work.
CSS has no concept, or implementation, of if/else statements, so this is not possible in CSS only.
However, if you define the border for an element, and then later redefine that border the second statement will override the first (assuming an equally specific selector), so I'm unsure as to why you need to apply a border only if the element doesn't already have a border defined:
div {
border: none; /* removes the border */
}
/* other stuff */
div {
border: 1px solid #f90; /* defines the border */
}
Similarly:
div {
border: 5px solid #0f0; /* defines the border */
}
/* other stuff */
div {
border: 1px solid #f90; /* re-defines the border */
}
If you can define your use-case it might be possible to help you further.
Edited to address the further information in the question:
In IE8 there is no border for select. So I'm trying to do a css reset like this:
select, input[type="file"], textarea {
border: solid 1px #7F9DB9;
}
The problem is it is overriding any preexisiting style because of specificity. I tried to use expression filter but with DocType in IE8 it does not seem to work.
If the problem is specificity, then the only options you have are to either increase the specificity of the selector you want to apply, ideally use an id, or multiple ids, in your selector (the id of an ancestor element is fine) since that's the most specific selector available.
Or, you can decrease the specificity of the selector you want to override.
It's worth noting that the select element is difficult to style reliably since it's often rendered by the underlying OS, rather than the browser itself, for a consistent look within that operating system.
You have three alternatives:
Use JavaScript - that has logic, so you can check whether it has a border or not and then do something with the result. You can use a library like jQuery or MooTools to make it easier.
Make this selector more specific, so that it only applies to elements that you want it to.
Make your other selectors more specific with classes, IDs, or nested selectors (like form textarea).