How should I go about changing the color of a span on mouseover? I currently have a tab (like in a menu) that has been classified as a span, let's say .rtsOut with a background color of #595959. Each tab is still classified as the same span, .rtsOut, but is divided into 4 different tabs. When I mouseover, I'd like to change that one tab to to, #000000, and to revert back to #595959 when the mouse is moved off of the span.
I unfortunately don't have access to the whole overlook of CSS like so:
<style>
span:hover {
background: ######;
}
</style>
and instead I have a very simple view like this, where I just input single lines of code:
#SampleThing {visibility: hidden;}
Can anyone help me? I've been Googling, and testing for hours, and can't come up with a solution.
So far, this is what I've found. I believe this is how the span has been created:
<li class="rtsLI"> == $0
<a class="rtsLink tbLeft4" id="TbInformation" href="../Info/Info.aspx">
<span class="rtsOut">
<span class="rtsIn">
<span class="rtsTxt">Edit Information</span>
</span>
</span>
</a>
</li>
And I've tried changing the background color of that span using this:
.rtsOut:hover {background: #000000;}
However, that line of code isn't working :(
Take a look at my codePen here
CSS
.rtsOut:hover{
background-color: red;
}
HTML (Assuming it looks like this)
<div>
<p>
<span class="rtsOut">One</span>
<span class="rtsOut">Two</span>
<span class="rtsOut">Three</span>
</p>
</div>
Also note that if you would like some more flexibility down the road, you may want to look into using <ul> and <li> for Nav Menu items.
When I mouseover, I'd like to change that one tab to to, #000000, and
to revert back to #595959 when the mouse is moved off of the span.
.rtsOut:hover {background: #000000;}
<span class="rtsOut">
<span class="rtsIn">
<span class="rtsTxt">Edit Information</span>
</span>
</span>
Assuming that your HTML really is:
<span class="rtsOut">
<span class="rtsIn">
<span class="rtsTxt">Edit Information</span>
</span>
</span>
and this isn't working:
.rtsOut:hover {background: #000000;}
Then, it tells me that someone has generated styles that are "more specific" than the ones you are providing.
CSS styling rules are applied from "most specific to least specific". For example: parent > .rtsOut { background-color: red; } will take precedence over .rtsOut { background-color: blue; } because it is more specific. As well, inline styles will have one of the highest precedence, so <span class="rtsOut" style="background-color:red;"></span> will override .rtsOut { background-color: blue; }. See http://vanseodesign.com/css/css-specificity-inheritance-cascaade/ for more information.
I would start by adding !important to your styles (as this will always have the highest precedence if nothing else has been given !important), to see if the above is the case.
Also, I would most definitely get used to browser developer tools. They allow you to delve right into the active code and do live edits. You will be able to see if there is an inline style or another style rule that has more precedence over the one you are trying to provide.
Related
I've done this, this , and this but gives me no luck. I'm trying to change the color of a div when the lower element is being hovered but I can't do it. I have this HTML structure like so:
<div class="ms-parent">
<button type="button" class="ms-choice"></button>
<div class="ms-drop"></div>
</div>
Here is what I've tried so far:
.ms-parent:hover,
.ms-choice:hover + .ms-drop,
.ms-drop:hover,
.ms-choice:hover,
.ms-drop:hover ~ .ms-choice{ color:#000000!important; background: #ffffff; }
So when .ms-drop is being hovered I want .ms-choice to change its style. What I'm missing here?
There is no Upper/previous element selector in CSS. You can select next immediate sibling element using + and you can select all the next siblings element using ~.
Kindly note it down, I mentioned next, because there is no previous selectors right now. Hopefully that will introduce on CSS4. You can use jquery to achieve this. Otherwise if you want to do it in CSS, you need to change the HTML structure like below.
<div class="ms-parent">
<div class="ms-drop"></div>
<button type="button" class="ms-choice"></button>
</div>
Now you can target it like below.
.ms-drop:hover ~ .ms-choice{ color:#000000!important; background: #ffffff; }
Or
.ms-drop:hover + .ms-choice{ color:#000000!important; background: #ffffff; }
DEMO
I'm trying to get text color to change and the text to be underlined when the link is selected (it will have class "selected"). For some reason I can't get it to work even with !important. And yes, I know "a" should be inside "li" :)
HTML:
<a href="">
<li class="list selected">
<table>
<tr><td class="first">Text here</td><td class="second"><div class="icon-arrow-down"></div></td></tr>
</table>
</li>
</a>
CSS:
table {
.selected {
color:green !important;
text-decoration:underline !important;
}
}
Here's my fiddle http://jsfiddle.net/vwLu8/1/.
Your selectors should not be nested unless you're using a preprocessor, you also need to change your level of specificity, change your CSS to:
Demo Fiddle
.selected table td{
color:green;
text-decoration:underline;
}
More on specificity from MDN
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.
I have defined a class in my main.css file.
.red {
color: #d14;
}
And using it like this.
<div class="navbar">
<div class="navbar-inner">
<ul class="nav">
<li class="active red">
<i class="icon-leaf icon-white"></i>Admin
</li>
</ul>
</div>
</div>
Besides my main.css I also import the twitter bootstrap css file.
This way it does not work. Is it because Bootstrap is overruling my color definition?
The only element in your markup that could visually apply this style is the <a>, and that element has a lot of really specific CSS rules applied to it by Twitter Bootstrap, stuff like this:
.navbar .nav .active > a,
.navbar .nav .active > a:hover {
color:#FFFFFF;
}
You'll have to write something even more specific to get the style to apply:
.navbar .navbar-inner .nav .red a {
color: #d14;
}
Demo: http://jsfiddle.net/pYGaG/
You could use !important on the rule if you really had to, but I really feel that you should avoid it as much as possible. If this is a single element that has this style, consider adding an id to it, which carries a lot of weight specificity-wise:
<li class="active" id="home_link">
<i class="icon-leaf icon-white"></i>Admin
</li>
#home_link a {
color: #d14;
}
http://jsfiddle.net/pYGaG/1/
Here are a couple good articles on CSS specificity:
http://css-tricks.com/specifics-on-css-specificity/
http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html
And as a side note, try to avoid presentational class names like red. Use more meaningful ones that aren't tied to the way it should look, but what it is (for example, .active-link).
Your're defining the red color on a <li>-Tag, but there is no text. The text is inside the <a>-Tag, so you need to overwrite this rule.
Code something like this:
.red a {
color: #d14;
}
Update: Go for the answer given by Wesley Murch.
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
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");
});