I want to style the :active state of a button that is represented by an <a> tag. The <a> tag has an inner <span> (beacuse I want to add an icon to this button).
I notice the :active state is triggered properly in everything but Internet Explorer 8. In IE 8, it appears that the area around the <span> (the <a>’s padding) triggers the :active state, but when clicking directly on the text within the <span>, the :active state is not triggered.
Is there a way to fix this without resorting to Javascript?
HTML
<a class="button" href="#">
<span>Add a link</span>
</a>
CSS
a.button { some styles }
a.button:active { some other styles }
Right, terribly over-complicated solution (and still imperfect), but: if you don’t wrap the link text in the <span>, and instead just use the <span> as a place to put your background image and position it absolutely within the <a>, then the <span> (mostly) stops blocking the :active state.
Test page: http://www.pauldwaite.co.uk/test-pages/2769392/3/
HTML
<a class="button" href="#">
<span></span>Link
</a>
CSS
<style type="text/css">
a.button {
position: relative;
padding: 10px;
color: #c00;
}
a.button:active {
color: #009;
font-weight: bold;
}
a.button span {
position: absolute;
top: 50%;
left: 3px;
margin-top: -2px;
border: solid 2px #000;
}
</style>
Of course, the area that the <span> covers still traps the click event, so when the user clicks on there, they won’t see the :active state. It is a slight improvement on the previous situation.
Tricky: IE 8 doesn’t seem to register the <a> tag as active when the <span> is clicked. (IE 6 and 7 are both fine. You found a regression!)
It does, however, register the <span> tag as active. If you can apply all the styles you want to change for the :active state to the <span>, then IE 8 will play along, e.g.
a.button:active,
a.button span:active/* This selector is for IE 8 only */ {
color: #009;
font-weight: bold;
}
Test page: http://www.pauldwaite.co.uk/test-pages/2769392/
Any styles that only apply to the link won’t change in IE 8 though. In the example above, the text changes colour when clicked, but the underline does not, as the underline style is attached only to the link (via the browser’s default styles), not the <span>.
I had the same issue, and FINALLY figured it out:
You need a target in the <a> tag, i.e. add the "href" attribute in the <a> tag:
<a id="logonButton" class="button submit" href="#Url.Action("Index", "Home")"><span>Log On</span></a>
Works like a charm in all IE versions. :)
Maybe:
a.button span { ...
a.button span:hover { ...
would work?
Alternatively, you could put the <span> outside the <a> instead. That seems to work.
HTML
<span><a class="button" href="#">
Add a link
</a></span>
Test page: http://www.pauldwaite.co.uk/test-pages/2769392/2/
Had exactly same problem today.
Try setting
z-index: -1; position: relative;
on the span.
This is what i came up with after reading this post.
I actualle wrote a long answer, with example code etc etc etc.. but while indent'ing css code, IE had a choke and crashed..
I came up with a solution that fixes the ie8 bug using jquery. Its an unreasonable use of resources for such a minor bug, but the app I was working on a the time was using a lot of jQuery already so it didn't matter.
HTML
<span>Button</span>
CSS
a.btn:active,
a.btn.ie8:hover { /* <-ie8 hack */
/* mouse down state a.btn style */
}
a.btn:active span,
a.btn.ie8:hover span { /* <-ie8 hack */
/* mouse down state a.btn span style */
}
Jquery
$(document).ready(function() {
var isIE8 = ($.browser.msie == true && $.browser.version == "8.0") ? true : false;
if (isIE8 === true) {
$("a.btn").bind({
mousedown: function() {
$(this).addClass('ie8');
},
mouseleave: function() {
$(this).removeClass('ie8');
}
});
}
});
You can fix it using this:
$('.yourspan').mousedown(function(){
$(this).parents('.youranchor:first').css("background-position","bottom");
});
$('.yourspan').mouseup(function(){
$(this).parents('.youranchor:first').css("background-position","top");
});
Related
I've just started using FontAwesome, so far so good. One question though, when I use it with an anchor tag and it has text-decoration:none, and on hover text-decoration:underline. When I hover the link, the icon gets the underline effect, too…how do I get only the link to be underlined, not the icon?
I tried to placing it outside the anchor tag, but it doesn't get the color I assigned to the link
Sample code:
<style>
a{color:red;text-decoration:none;}
a:hover{text-decoration:underline;}
</style>
<span class="fa fa-camera-retro"> </span>This's a test
Thank you
I popped your exact code into JSFiddle and noticed that the camera icon itself wasn't being underlined completely, but the space between the icon and the text was.
So, if that's what you're experiencing, you can simply add a bit of padding after the icon, that way there's no whitespace to underline.
a {
color: red;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.fa {
padding-right: 5px;
}
a:hover .fa {
color: blue;
}
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet" />
<span class="fa fa-camera-retro"></span>This is a test
The last item in the CSS was merely to show that no underline effect was happening on hover by changing the icon's color to show formatting wasn't being applied from other items. Notice there's no space after the span tag, instead the space is created by the 5px padding applied to anything with the .fa class.
I tested this in both a very recent version of Firefox, and IE9 because those are what's on my work machine.
I had a similar issue, and found that the fa class defines an inline-block display mode. If I forced the display to inline inside a link, then everything was fine.
a > .fa {
display: inline;
}
Put your <span> outside of the <a> so its not affected by your hover...
<span class="fa fa-camera-retro"></span>This is a test
You can add a style for the a:hover span.fa selector:
<style>
a {
color: red;
text-decoration: none;
}
a:hover span.fa {
text-decoration: none;
}
</style>
<span class="fa fa-camera-retro"></span> This is a test
I'm a bit confused about the use of the :last-child selector. I have different buttons with the mark-up like this:
<div class="people">
<a href="pd.html" title="Post-Doctorate's">
<div class="people-button">Post-Doctorate's</div>
</a>
<a href="staff.html" title="Staff">
<div class="people-button">Staff</div>
</a>
<a href="phdstudents.html" title="PhD Students">
<div class="people-button">PhD Students</div>
</a>
</div>
Now I want to give the last button a different margin-bottom so I tried to use this:
.people a:last-child .people-button {
margin-bottom: 5px;
}
But it seems this is not the correct selector. Any help or explanation on how to do this?
Your code is right and works (see fiddle), but only in supported browsers.
ie: last-child won't work below IE9.
To see compatibility table see:
http://www.quirksmode.org/css/selectors/
https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child
Another, issue is that it could be overridden by another CSS rule with major precedence. Use Developer Tool (F12) in your browser OR context menu (right-click the anchor) for inspect the element.
Generally, the browser will show in the right pane for this element, the computed CSS styles & all styles that were applied (and overriden).
You could rewrite your code. Take off the divs inside a, and set a as display: block to break line and work padding:
<a href="pd.html" title="Post-Doctorate's" class="people-button">
Post-Doctorate's
</a>
<a href="staff.html" title="Staff" class="people-button">
Staff
</a>
<a href="phdstudents.html" title="PhD Students" class="people-button">
PhD Students
</a>
CSS:
.people-button {
display: block;
}
.people a:last-child {
padding-bottom: 50px; /* if you want space inside anchor */
background-color: #0aa;
}
.people {
border: 1px solid #000;
padding-bottom: 20px; /* if you just want space at bottom of container */
}
http://jsfiddle.net/m7CNv/2/
I have a row of divs with :hover and it is working when I hover over the images within the divs. However, it doesn't want to work for the text. I am on the newer side of html and css, so help appreciated. I must be missing something obvious?
The first one with the div.topIconsHover:hover CSS works. The other does not. I have tried applying the topIconsHover class to the div as well and it still doesn't work. So, I must be doing something wrong with the HTML? But I'm just not sure what. Help appreciated! Thanks.
Note: I have the CSS in an external sheet.
div.topIconsHover:hover {
background-color:#555555;
}
<div class="topIcons topIconsHover">
<img src="tools16lg.png" />
</div>
div.topTextHover:hover {
background-color:#555555;
color:#ffffff
}
<div id="topBrowse" class="topTextHover">
Browse
</div>
The color attribute is working only with text elements, not divs. So you should apply the class tag to your href tag like this :
<style type="text/css">
.topTextHover:hover {
background-color:#555555;
color:#ffffff
}
</style>
<div id="topBrowse">
Browse
</div>
EDIT :
If you're looking to define a base class for the link itself, and a HOVER state, do it like this :
<style type="text/css">
.topTextHover {
background-color: transparent;
color: #0000ff;
}
.topTextHover:hover {
background-color: #555555;
color: #ffffff;
}
</style>
<div id="topBrowse">
Browse
</div>
Good luck
You applied style to the ":hover text" but not for links. This should do the trick (not tested):
div.topIconsHover:hover {
background-color:#555555;
}
<div class="topIcons topIconsHover">
<img src="tools16lg.png" />
</div>
div.topTextHover:hover, div.topTextHover:hover a {
background-color:#555555;
color:#ffffff
}
<div id="topBrowse" class="topTextHover">
Browse
</div>
Anchor tags have a default text colour which gets priority (usually blue). What you need is to define this explicitly:
div.topIconsHover:hover {
background-color: #555555;
}
div.topTextHover:hover {
background-color: #555555;
}
div.topTextHover:hover a {
color: #ffffff
}
There are two really simple ways to resolve this issue.
First if you don't have any height/width requirements on the anchor tag (<a href=''></a>) being inside the div do the following:
.topTextHover a:hover{
background-color:#555555;
color:#ffffff
}
<div id="topBrowse" class="topTextHover">
Browse
</div>
If you do have spacial requirements for the text inside the div (i.e. you want the text to be vertically-aligned to the center and horizontally centered) then I would do the following note* this is backwards compatible but is really only compliant with CSS3
#BrowseLink:hover {
background-color:#555555;
color:#ffffff
}
<a id="BrowseLink" href="browse.html">
<div id="topBrowse" class="topTextHover">
Browse
</div>
</a>
Also of note IE6 doesn't like the pseudo-class hover on anything other than an anchor tag and therefor will not work properly. This may be applicable in other browsers as well but the main one that I know that has issues is IE6 of the browsers that are typically seen on a website.
I have a list of elements, and i want to change a style of an element when one clicks on the list element(and that specific style to stay the same until the user presses another list item).
I tried using the 'active' style, but no success.
My code:
#product_types
{
background-color: #B0B0B0;
position: relative; /*overflow: hidden;*/
}
#product_types a:active
{
background-color:yellow;
}
but the element is 'yellow' only a millisecond, while i actually click on it...
Use the :focus pseudo class
#product_types a:focus
{
background-color:yellow;
}
See this example -> http://jsfiddle.net/7RASJ/
The focus pseudo class works on elements like form fields, links etc.
The reason it doesn't work in other browsers is related to the css focus specification. It states:
The :focus pseudo-class applies while an element has the focus
(accepts keyboard events or other forms of text input).
So it seems to work perfectly fine with text input fields or when you focus using the tab key. To make the above compatible with other browsers add the tabindex attribute to each element and this appears to fix the problem.
HTML:
<ul>
<li id = 'product_types'>First</li>
<li id = 'product_types'>Second</li>
</ul>
CSS:
#product_types {
background-color: #B0B0B0;
position: relative;
}
#product_types a:focus {
background-color:yellow;
}
JSFiddle Example
I got one strange problem which I never got before. Please see this code:
The css:
#btn{
margin-left:150px;
padding:10px;
display:block;
}
#btn a{
padding:5px 20px;
background:green;
color:#FFF;
text-decoration:none;
outline:none;
}
#btn a:hover{
background:#933;
}
#btn a:focus, #btn a:active{
background:#CF0;
color:#000;
}
Here the HTML
<div id="btn">
Click here
</div>
The focus and active css working well in firefox, but not in the chrome and safari.
Yeah seems like little problem with focus in webkit. Not really a bug. Easily fixable in html. Just use tabindex.
[hide]
[show]
ta da ...
This is also the case for Webkit based 'focus' events, it doesn't take. The fix is to put a tabindex="0" attribute on the A and then it receives the focus event. You might also want to have at least a "#" as the href just in case.
It's fixable, some additional code needed though...
<div id="btn">
Click here
</div>
jsfiddle
I know it's ridiculous... You can read more here
Hope this helps
The solution posted by user1040252 did the trick for me.
I have a div with images that sets an image in a span tag to visible on a click.
Firefox ignores the classname:focus in my CSS file.
<div class="thumbnail_frame">
<img src="pictures\\figures\\thumbs\\image_1.JPG"/>
<span>
<img src="pictures\\figures\\image_1.JPG"/>
</span>
</div>
My CSS (part of it):
.thumbnail_frame:focus span{visibility: visible;}
//...
.thumbnail_frame span
{
visibility: hidden;
position: fixed;
top: 20px;
left: 20px
}
But this only worked in Internet Exporer 9. Firefox 12 kept ignoring the focus also in other simple examples like found here:
explanation:
http://de.selfhtml.org/css/eigenschaften/pseudoformate.htm
try it:
http://de.selfhtml.org/css/eigenschaften/anzeige/pseudo_links.htm
But adding tabindex="0", as in
<div tabindex="0" class="thumbnail_frame">
<img src="pictures\\figures\\thumbs\\image_1.JPG"/>
<span>
<img src="pictures\\figures\\image_1.JPG"/>
</span>
</div>
works like a charm. One click opens the hidden span, and the second one closes it very neatly.
Use tabindex="0" to make an element focusable if it is not already. See https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex for more information about tabindex.
Setting tabindex to -1 makes it unfocusable. Setting tabindex to a positive integer is not recommended unless you're trying to explicitly set the tab order, as it can create accessibility issues.
For more information about tabindex and accessibility, see https://webaim.org/techniques/keyboard/tabindex.
You should know that the pseudo class :focus doesn't go with A. The A tag has 4 pseudo classes : :link, :hover, :active, :visited