I currently have a html file that contain such div:
div.myClass:visited {
background-color: red;
}
<div class="myClass" href="link">
</div>
What I am trying to do here is to have div direct to the given link and if the link is visited already, div background-color changes to red. The link does work fine, however changing the background color when the link is visited does not work. Is there a way to change the background color of div that contains a visited link? Thank you!
Use Javascript to replace innerHTML
<script type = "text/javascript">
function ReplaceContentInContainer(id, content) {
var container = document.getElementById(id);
container.innerHTML = content;
} </script>
<div id="example1div" style="border-style:solid;padding:10px; text-align:center;">
I will be replaced when you click.
</div>
Click me to replace the content in the container.
use <a/> instead of div and style it like div by giving it display:block; and width and height
for example:
<a class="myClass" href="http://www.lego.com/en-us/"></a>
CSS:
.myClass {
background-color: blue;
width:200px;
height:200px;
display:block;
}
.myClass:visited {
background-color: red;
}
.myClass:hover{
background-color: green;
}
View on CodePin
Also look at :
How can I detect visited and unvisited links on a page?
Privacy and the :visited selector
Related
Please, can you tell me why when I visit one of the two links in the code, the picture does not get radius? And what can I do to get that done?
Notice the a:visited div.container div.title works fine
<html>
<head><title>Some title</title>
<style>
.container
{
width : 100;
height : 100;
}
.title
{
position : absolute;
}
a:visited.container img.pic
{
border-radius:50%;
}
a:visited div.container div.title
{
color : red;
}
</style></head>
<body>
<a href="#link01">
<div class="container">
<img class="pic" src="leb.jpg">
<div class="title">Title 01
</div>
</div>
</a>
<a href="#link02">
<div class="container">
<img class="pic" src="leb.jpg">
<div class="title">Title 02
</div>
</div>
</a>
</body>
</html>
Thanks
From w3schools.com
Definition and Usage
The :visited selector is used to select visited links.
Tip: Use the :link selector to style links to unvisited pages, the
:hover selector to style links when you mouse over them, and the
:active selector to style links when you click on them.
Browsers limits the styles that can be set for a:visited links, due to
security issues.
Allowed styles are:
color background-color border-color (and border-color for separate
sides) outline color column-rule-color the color parts of fill and
stroke All other styles are inherited from a:link.
You can use javascript for this purpose but not $("a:visited") since they don't support it anymore.
Solution with click event
a.visited .pic {
border-radius: 50%;
}
then import jQuery and add these lines between tags. This will add "visited" class on every clicked link
$("a").on('click' , function() {
$(this).addClass("visited");
})
Be aware after page reloads classes will be lost
Change the order of the declaration:
Instead of
a:visited.container img.pic
use
a.container:visited img.pic
I just saw another issue: None of your tags have the class .container set. I guess you mean something like that:
a:visited div.container img.pic
Also, the first declaration looks strange:
.container
{
width : 100;
height : 100;
}
You did not put any unit there (but it is necessary).
I recommend to change it to
.container
{
width : 100px;
height : 100px;
}
i have many link in my content
how can found and change color all link in div tag when show content to user ?
like that :
pleas visit my own site's : telegram.org , www.google.com , www.wikipedia.com , ...
and send mail to : mymail#gmail.com
If you want to style a link you should use css to do this:
.new_link {
background-color: black;
color: white;
}
<div>
<a class="new_link" href="#link2" id="link1">Go to next link</a>
<br>
<a class="new_link" href="#link1" id="link2">Go to link1</a>
Some other text that will not be styled
</div>
Here i added a class in the link elements (<a> element).
By adding a class they get the css styling.
The styling i choose here where:
Background-color
color (text-color)
You can make create css rules (like above) for all <a> elements on your page.
An other possibility is to add a class to the <div> element and select all links inside this div. All of this is possible with css.
if you want to change the color of links in all pages or app then do this in css..
a {
color: red;/*color name, color code or rbg*/
}
if you want to change the specific link color ....
a.highlight {
color: red;
}
if you want to change the all links color in specific div .....
.content a{
color:red;
}
So I'm trying to get a onMouseOver to replace an image when the mouse is hovering over a div, unfortunately, as I have it right now it only replaces the image when the mouse is directly over the image, not the div, is there a way to get this to work?
Should I use a CSS to place the image, and replace the image on hover instead?
<div class="link">
<a href="link.html">
<img src="img.png" onMouseOver="this.src='hoverimg.png'" onMouseOut="img.png'"
<div class="title">Title</div>
</a>
</div>
I prefer using CSS for this:
<div class="image-hover">
Some title
</div>
.image-hover { background: url(...);}
.image-hover:hover { background: url(...);}
This can be achieved with CSS and background images. You also should not be using a block level element (div) inside of an inline element (a). I've swapped it for a span. For example:
<style type="text/css">
.link a {
display:inline-block;
background: url('img.png') top left no-repeat;
width:(imagewidth)px;
padding-top:(imageheight)px;
}
.link a:hover {
background: url('hoverimg.png') top left no-repeat;
}
</style>
<div class="link">
<a href="link.html">
<span class="title">Title</span>
</a>
</div>
The complete optimum would be combining the two images into what is called a sprite and use background-position.
You can do this with CSS or jQuery. Most people will recommend that you use CSS because it is easier to debug:
If you want the image to change when you hover on the div, you can apply a :hover state to the div:
.link img{
background: url("image1.png");
}
.link:hover img
{
background: url("image2.png");
}
However you should note that this basically treats img as an inline-block element and does not change the src attribute.
jQuery will allow you to change the source, but it must be debugged if something goes wrong, and if JS is disabled, it will not run.
$(".link").hover(function(){
$(this).find("img").attr("src", "image2.png");
}).mouseout(function(){
$(this).find("img").attr("src", "image1.png");
});
JSFiddle
Is there any way to change an element's css while focusing or hovering on one of it's children?
Ex: while I move my mouse on A, B's background color changes.
if B is a descendant A, it is possible.
--A
-----B
using #A:hover #B { background-color: blue }
DEMO
in sibling:
---A
---B
It is : #A:hover ~ #B { background-color: blue; }
DEMO
assume B is a descendant of A.
what if I want to change #A background, while I am hovering on B. how could it be?
--A
-----B
Doing this in pure CSS is unfortunately not possible...at this time. However, there is supposedly an upcoming parent selector that would work well in this scenario. Click here for more info.
Work-around
In the meantime, you can also use javascript and/or jquery to accomplish this pretty easily.
DEMO
HTML
<div id="div1">
div 1 area
<p id="paragraph">
This is paragraph
</p>
</div>
CSS
#div1 {
border:1px solid lightgray;
}
p {
margin: 50px;
background-color: lightblue;
padding: 8px;
}
.altbg {
background:grey;
}
jQuery
$(function(){
$('#paragraph').mouseenter(function(){
$(this).parent().addClass('altbg')
}).mouseout(function(){
$(this).parent().removeClass('altbg')
});
});
It is actually possible in css.
Somebody made this fiddle:
http://jsfiddle.net/u7tYE/
#a:hover + #b {
display:block;
background-color:rgb(200,200,150);
}
#b{display:none;background-color:rgb(200,200,150;}
<a id="a">Menu</a>
<div id="b">
<p>Home</p>
<p>About</p>
<p>Login</p>
</div>
It works perfectly.
I have a span tag that has a background image on it then inside it I have an a tag with text link. The span has the background image set to the right of the text link. I want when you rollover the a tag for it to also cover the span background image in its hover state also.
I tried something like this but still not working.
span a:first-child + span a:hover{
cursor: pointer;
}
Markup html
<div class="wrapper">
<span>Study Bill</span>
<span>Download PDF</span>
</div>
Do it the other way; wrap your span with one big <a> tag, and write the link text inside the <span>. For instance:
<a href = "#">
<span style = "background-image: url(your_image.png);">
Download PDF
</span>
</a>
You can't change properties of parents in CSS upon interaction with children - it works one way only. However you can do something like this - http://jsfiddle.net/uH2XP/
some link text
<style>
a:after {
content: '';
display: inline-block;
height: 20px;
width: 20px;
background: green;
}
a:hover:after {
background: orange;
}
</style>
Just replace content: '' with content: url(path/to/your/image.png)
I would probably try to assign the background to the link and the span. Then you can have the hover state handle the background transition.
<style>
div.wrapper a, div.wrapper span{background:#f00;}
div.wrapper a:hover{background:#0f0;}
</style>
<div class="wrapper">
<span>Study Bill</span>
Download PDF
</div>
http://jsfiddle.net/sdowswell/sz6fq/