I'm preparing a WordPress theme and can't figure why :active in my CSS doesn't work while :hover is working fine.
HTML:
(...)
<div id="mainMenu">
<div id="mm_links">
<a id="mm_1" href="index.php/about-icar/">About ICAR</a><br/>
<a id="mm_2" href="">News</a><br/>
<a id="mm_3" href="index.php/for-the-authors/">For the authors</a><br/>
<a id="mm_4" href="index.php/resources/">Resources</a><br/>
<a id="mm_5" href="index.php/contact-promotion/">Contact<br/>& promotion</a><br/>
</div>
<div id="mm_tail"></div>
</div>
(...)
CSS:
div#mainMenu{
float: left;
width: 140px;
height: 430px;
margin-top: 48px;
background-image: url("img/mainMenu.svg");
background-size:cover;
text-align: center;
}
div#mainMenu a{
font-family: 'Raleway', sans-serif;
font-size: 10.6pt;
text-decoration: none;
}
div#mainMenu a:link, div#mainMenu a:visited, div#mainMenu a:active {
color: #262E5B;
}
div#mainMenu a:hover{
color: #262E5B;
}
a#mm_1 {
position: relative;
display: table-cell;
top: 21px;
left: 18px;
width: 120px;
height: 35px;
vertical-align: middle;
}
a#mm_1:hover, a#mm_1:active { <-- THIS IS NOT WORKING
background-image: url('img/ElementAbout.png');
background-size: cover;
}
div#mm_links{
clear:both;
height: 430px;
}
div#mm_tail{
background-color: white;
border-left: 2px solid #262E5B;
border-right: 2px solid #262E5B;
border-bottom: 2px solid #262E5B;
width: 30px;
height: 100%;
clear: both;
float: right;
}
What are you trying to accomplish? If you remove the :hover and press the link I think you will find that the :active is working just fine. But it doesn't do much good since you are styling it the same as when you hover over it. So basically you are hovering over it and then the background-image shows, then you click the link and the same background-image keeps showing.
a#mm_1:active {
background-image: url('img/ElementAbout.png');
background-size: cover;
}
The :hover pseudo-class selects an element when the mouse cursor is right above it.
The :active pseudo-class applies styles when the element is clicked (or activated in another way).
In your code, your :hover and :active styles are identical.
a#mm_1:hover, a#mm_1:active {
background-image: url('img/ElementAbout.png');
background-size: cover;
}
You won't notice any difference when hovering or clicking.
However, give each a different style and you'll see the difference:
a#mm_1:hover { background-color: yellow; }
a#mm_1:active { background-color: red; }
DEMO
Related
So I want to be able to have a link which when hovered, display an image..
So far, I only found tutorial to change an image while it's hovered..
How should I do that please ?
BTW : I'm a total beginner in HTML..
Thanks !
Use adjacent sibling selector and the display property.
img {
display: none;
}
a:hover + img {
display: block;
}
<a href='http://example.com'>Hover Here</a>
<img src='https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png' width='200'>
jQuery for life.
$('.link').mouseover(function() {
$('.dog').show();
}).mouseleave(function() {
$('.dog').hide();
})
#import url('https://fonts.googleapis.com/css?family=Raleway');
.link {
height: 40px;
width: 200px;
border: 1px solid black;
text-align: center;
border-radius: 5px;
margin-bottom: 10px;
}
a {
text-decoration: none;
color: black;
font-size: 30px;
font-family: Raleway;
}
.dog {
background: url('https://i.ytimg.com/vi/opKg3fyqWt4/hqdefault.jpg');
height: 150px;
width: 150px;
background-size: cover;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='link'>
<a href='http://www.google.com'>Hover me</a>
</div>
<div class='dog'>
</div>
Indeed there is couple ways to achieve what you want. probably if you are a beginner in html and css, the best for you is to use zer00ne snippet. Hovever, if you want to dig deeper I also have the following solution for you:
You can test it here
https://jsbin.com/barixeqigi/17/edit?html,css,js,output
HTML
<a href="#" title="super eye" alt="super eye">
hover
</a>
CSS
a {
position:relative;
}
a:after {
content:'';
display:block;
width: 200px;
height: 200px;
opacity:0;
background: url('http://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg') no-repeat center;
background-size: cover;
border-radius:50%;
position:absolute:
right:0px;
top:0px;
-webkit-transition: all 3s;
transition: all 3s;
}
a:hover:after {
opacity: 100;
}
I have a spaninside the anchorelement.
All I want is:
1) Have a width for the anchor text, so that it does not overlap on the span element.
2) The span element should be aligned to the first line of the anchor text
3) The span element to be right of the anchor text
a {
width: 147px;
display: inline-block!important;
}
span.expand {
background: url(https://cdn2.iconfinder.com/data/icons/deadsimple/youtube.gif) no-repeat right center;
text-decoration: none !important;
padding-right: 14px;
margin-left: 100px;
}
<a href="#">
Enrollment to communication skills
<span class="expand"></span>
</a>
I have no other choice but to use span inside anchor, even though it is not a recommended.
Not sure what you want to achieve, but based on what you have mentioned, here is the solution.
WORKING DEMO
The CSS Change:
a {
width: auto;
display: inline-block!important;
}
span.expand {
background: url(https://cdn2.iconfinder.com/data/icons/deadsimple/youtube.gif) no-repeat right center;
text-decoration: none !important;
background-position: top right;
padding: 11px;
float: right;
}
Edited CSS Change:
If the width needs to exist without setting it to auto, below is the code.
WORKING DEMO WITHOUT AUTO WIDTH
a {
width: 135px;
display: inline-block;
position: relative;
}
span.expand {
background: url(https://cdn2.iconfinder.com/data/icons/deadsimple/youtube.gif) no-repeat right center;
text-decoration: none !important;
background-position: 0 0px;
padding: 11px;
position: absolute;
z-index: 1;
top: 1px;
right: 17px;
}
Hope this helps.
You can do this in a few ways. One way is by moving the <span> outside the anchor. Another way is by adding the background image to the anchor and remove the span.
Both ways are visible in this JSFiddle: https://jsfiddle.net/xpoa06x8/
<a href="#">
<!-- using a backgroundimage -->
Enrollment to communication skills
</a>
<br/><br/><br/><br/><br/>
<a class="href" href="#">
<!-- using an extra span -->
Enrollment to communication skills
</a>
<span class="extend"></span>
CSS:
a {
width: 146px;
display: inline-block!important;
background-image: url(https://cdn2.iconfinder.com/data/icons/deadsimple/youtube.gif);
background-repeat: no-repeat;
background-position: 120px 0px;
}
/* Second example */
.href{
background-image: none;
float:left;
display:block !important;
}
.extend{
display:block;
float:left;
background-image: url(https://cdn2.iconfinder.com/data/icons/deadsimple/youtube.gif);
background-repeat: no-repeat;
width:15px;
height:15px;
}
Working fiddle
I added :after pseudo-element instead .extend with position:absolute;
a {
width: 147px;
display: inline-block!important;
position:relative
}
a:after {
background: url(https://cdn2.iconfinder.com/data/icons/deadsimple/youtube.gif) no-repeat right center;
text-decoration: none !important;
width: 15px;
height: 15px;
vertical-align: baseline;
display: block;
margin-left: 5px;
content: '';
position: absolute;
right: -10px;
top: 0;
}
I have a navigation which holds either text or images for links. I want the image to change on hover, so am using CSS backgrounds inside an empty div. However, I am looking for a way of doing this without using "position: absolute;" as the containing a tag will not expand to fill its dimensions. I would also like to do this without using a transparent placeholder image as I want to find a more elegant solution.
Here's the jsfiddle and the code:
http://jsfiddle.net/urhLs736/1/
<nav id="navigation">
<ul>
<li><a onclick="example1.html">PAGE 1</a></li>
<li><a onclick="example2.html">PAGE 2</a></li>
<li><div id="nav-image"></div></li>
</ul>
</nav>
and for the CSS:
#navigation {
z-index: 1;
background-color: #3A5E90;
color: #FFFFFF;
text-align: center;
width: 100%;
padding-top: 5px;
padding-bottom: 5px;
}
#navigation.fixed {
position: fixed;
top: 0px;
}
#navigation li {
display: inline;
}
#navigation a {
cursor: pointer;
padding-left: 3.5%;
padding-right: 3.5%;
color: #FFFFFF;
text-decoration: none;
}
#navigation a:hover {
background: #FFFFFF;
color: #3A5E90;
padding-top: 2%;
padding-bottom: 2%;
}
#nav-image {
display: inline;
background: url('https://avatars1.githubusercontent.com/u/3081095?v=2&s=72') no-repeat;
background-size: 100%;
margin-bottom: -6px;
height: 24px;
width: 100px;
}
#nav-image:hover {
height: 24px;
width: 100px;
background: url('https://avatars3.githubusercontent.com/u/5278945?v=2&s=96') no-repeat;
background-size: 100%;
}
I think that your layout will work as is with a minor adjustment to the CSS:
#nav-image {
display: inline-block;
background: url('https://avatars1.githubusercontent.com/u/3081095?v=2&s=72') no-repeat;
background-size: 100%;
margin-bottom: -6px;
height: 24px;
width: 100px;
border: 1px dotted yellow;
}
If you use display: inline-block, the div will take up the specified width and height and the background image will be visible, and the hover effect will work as you expect.
See demo: http://jsfiddle.net/audetwebdesign/9fd1dxn4/
In order to achieve this, you have to change both your HTML and your CSS.
First, your HTML should go like this:
<nav id="navigation">
<ul>
<li><a onclick="example1.html">PAGE 1</a>
</li>
<li><a onclick="example2.html">PAGE 2</a>
</li>
<li id="nav-image">PAGE 3
</li>
</ul>
</nav>
Note that I have added some content in your empty div. If you have an empty <li>, you'll have no background at all (just like your example) since you have a 0x0 pixels li element. I have added some content so the li displays as a general rule, which anyways won't be necessary after you see the CSS, which is the following:
#navigation {
z-index: 1;
background-color: #3A5E90;
color: #FFFFFF;
text-align: center;
width: 100%;
padding-top: 5px;
padding-bottom: 5px;
}
#navigation.fixed {
position: fixed;
top: 0px;
}
#navigation li {
display: inline-block;
height: 24px;
width: 100px;
padding-left: 3.5%;
padding-right: 3.5%;
}
#navigation ul li a {
cursor: pointer;
padding:2% 3.5%;
color: #FFFFFF;
text-decoration: none;
display:block;
height: 24px;
width: 100px;
}
#navigation a:hover {
background: #FFFFFF;
color: #3A5E90;
}
#nav-image {
background: url('https://avatars1.githubusercontent.com/u/3081095?v=2&s=72') no-repeat;
background-size: 100%;
}
#nav-image:hover {
background: url('https://avatars3.githubusercontent.com/u/5278945?v=2&s=96') no-repeat;
background-size: 100%;
}
#nav-image:hover a {
background:transparent
}
OK, now you see I have made some changes and added width and height to the li (the same you had in your sample, but you can change it to anything you want). Now, if you delete the content inside the empty DIV, you'll see how the rendering changes. While it's very easy to solve, I'll leave it to you so you can practice and understand how the whole positioning and display thing works. Also, you can add paddings, margins, et
Here you have a fiddle so you can see it in action and play around
The question doesn't describe this pretty well.
So I got three small images that are suppose to change on hover and work as a link, but it ''detects'' the hover only in a small part of the image. If I drag my mouse to the bottom of the image link, it's not even clickable, so the link only works in the top part of the image.
See for yourself:
http://jsfiddle.net/M3LC9/ (JSFiddle doesn't like pictures..)
<div class="kielet">
<nav>
<!--Englanti-->
<img class="icon" src="iconit/en.gif" title="in english" onmouseover="this.src='iconit/en_hover.gif'" onmouseout= "this.src='iconit/en.gif'">
<!--Ruotsi-->
<img class="icon" src="iconit/swe.gif" title="på svenska" onmouseover="this.src='iconit/swe_hover.gif'" onmouseout="this.src='iconit/swe.gif'">
<!--Venäjä-->
<img class="icon" src="iconit/ru.gif" title="По русски" onmouseover="this.src='iconit/ru_hover.gif'" onmouseout="this.src='iconit/ru.gif'">
</div>
.kielet {
top:0px;
width:100%;
background: black;
right: 0px;
margin-bottom:0px;
padding:0px;
}
.kielet nav {
right: 0px;
margin-right: 0px;
text-align: right;
}
.icon {
width: 50px;
height: 100%;
right: 0px;
margin: 20px;
margin-top:0px;
margin-bottom:0px;
display:inline;
padding: 0px;
}
You currently have your images set to display as inline. This will make them adhere to any line-height defaults a browser may have set on your a element, keeping your a element at a smaller height. This can be visualised in Chrome's Element Inspector:
To change this, simply set the display on your a elements to inline-block:
a {
display: inline-block;
}
JSFiddle demo.
Note that you may want to be a bit more specific with your a selector by specifying .kielet nav a, for instance, or giving your a elements their own class identifier.
Try changing the display property to display:inline-block
.icon {
width: 50px;
height: 100%;
right: 0px;
margin: 20px;
margin-top:0px;
margin-bottom:0px;
display:inline-block; <----
padding: 0px;
}
JSFiddle
Usually you don't implement your hover-state with javascript and <img />
You can easily do this with CSS.
HTML
<div class="kielet">
<nav>
<!--Englanti-->
<!--Ruotsi-->
<!--Venäjä-->
</nav>
</div>
CSS
.kielet {
background: black;
padding: 5px;
text-align: center;
}
a.icon {
display: inline-block;
width: 16px;
heiht: 16px;
line-height: 16px;
}
a.icon_ru { background: url(http://placehold.it/16x16/ffc) center no-repeat; }
a.icon_ru:hover { background: url(http://placehold.it/16x16/ff0) center no-repeat; }
a.icon_en { background: url(http://placehold.it/16x16/cff) center no-repeat; }
a.icon_en:hover { background: url(http://placehold.it/16x16/0ff) center no-repeat; }
a.icon_swe { background: url(http://placehold.it/16x16/fcf) center no-repeat; }
a.icon_swe:hover { background: url(http://placehold.it/16x16/f0f) center no-repeat; }
jsFiddle
I'm playing with 3 images. and it's making me dizzy. What I want is when a tab is active it will change the background-image.
I have this code right now in my html:
div id="promo-nav-wrapper">
<ul>
<li id="active">
</li>
<li>
</li>
</ul>
</div>
My CSS
#promo-nav-wrapper {
background: url("http://imageshack.us/photo/my-images/580/menubackground.png/");
background-repeat: repeat-x;
width: 100%;
height: 82px;
}
#promo-nav-wrapper ul {
text-align: center;
}
#promo-nav-wrapper ul li {
display: inline-block;
margin-top: 20px;
}
#promo-nav-wrapper ul li a {
height:53px;
width:41px;
display:block;
text-decoration: none;
background-repeat: no-repeat;
text-shadow: none;
}
a.promo-call{
background-image:url("http://imageshack.us/photo/my-images/861/callicon2.png/");
z-index: 3;
margin-right: 10px;
}
a.promo-text {
background-image:url("http://imageshack.us/photo/my-images/807/texticon2.png/");
margin-left: 10px;
margin-right: 10px;
}
/*this don't work*/
#promo-nav-wrapper li#active a {
background-image: url('http://imageshack.us/photo/my-images/694/selectediconbackground.png/') no-repeat!important;
height: 76px;
width: 64px;
/*background: blue;*/
}
My problem is I can't snip the image when an li is active, it doesn't show the background. T_T. Say like this
#active a{ background-image: url('selected_icon_background.png');
....
}
Acc to what i am understanding after reading the problem -
First of all in fiddle: http://jsfiddle.net/si_dean_ako/kyYWU/ images are not loading and secondly the css rule is wrong on #promo-nav-wrapper li#active a
Try to remove
#promo-nav-wrapper li#active a {
background-image: url('http://imageshack.us/photo/my-images/694/selectediconbackground.png/') no-repeat!important;
height: 76px;
width: 64px;
/*background: blue;*/
}
and add like this
#promo-nav-wrapper li#active {
background: url('http://imageshack.us/photo/my-images/694/selectediconbackground.png/') no-repeat!important;
height: 76px;
width: 64px;
}
When we want to write multiple value (shorthand properties) at that time we have to use background property of css and in above fiddle background-image is used and on that the url('image path'), no-repeat, !important; is applied. And background-image always take the path of the image.
So it better to use like that background: url('http://imageshack.us/photo/my-images/694/selectediconbackground.png/') no-repeat!important;
See updated fiddle: http://jsfiddle.net/kyYWU/2/ in this fiddle active image is showing behind the .
See the output generated on my local machine:
Try this
#promo-nav-wrapper #active a { background-image: url('selected_icon_background.png');
....
}
at the end of your stylesheet