I want to have the background color stay after I click on the div.
This div is linked to target another div.
<div class="barbutton">ABOUT</div>
here is the CSS of the div i click on
.barbutton:hover {
background-color: #7BDFBE;
}
so I want the background color to stay when its clicked.
I think what you're trying to achieve is the normal link behaviour i.e. it changes colour and stays that way to indicate it has already been visited. If yes, then you're approaching the problem the wrong way in CSS.
Instead of putting the CSS on a new DIV inside the <a> tag put the CSS on the link itself.
<a class="barbutton" href="#aboutbody">ABOUT</a>
Then use the link states in CSS as below:
a.barbutton:visited {
background-color: #7BDFBE;
}
a.barbutton:hover {
background-color: #7BDFBE;
}
Similarly, add the other links states you require.
To maintain state like you are wanting, I would suggest using some javascript/jQuery.
Javascript:
<div id="barbuttonid" class="barbutton">ABOUT</div>
<script type="text/javascript">
document.getElementById("barbuttonid").onclick = function() {
document.getElementById("barbuttonid").className += " clicked";
};
</script>
jQuery:
<div class="barbutton">ABOUT</div>
<script type="text/javascript">
jQuery('.barbutton').click(function() { jQuery(this).addClass('clicked'); });
</script>
CSS:
/* anchor tags are inline and cannot contain blocks elements like divs */
#aboutbody {
display: inline-block;
}
.barbutton:hover {
background-color: #7BDFBE;
}
.barbutton.clicked {
background-color: #7BDFBE;
}
Now when it is clicked, the additional class will be added and the CSS style can make the background persist for you.
EDIT/NOTE:
Ravi's answer is better as it does not use javascript. If you kept the HTML layout as you have it, the CSS declarations would have to be:
a:hover .barbutton {...}
a:vistied .barbutton {...}
Also, this way will make the style persist as long as the browser remembers that the link has been visited (user comes back in 3 days and the background color may still be persistant). My way above will not keep the background as soon as the user refreshes or leaves the page. I guess it depends on what you are going for.
Related
So I like my current background color but want to also have a button that can change it to white for others. I am making it for my website. Please help! I checked the other ones like this and none of them have helped!
#check {
display: none;
}
.myBtn:active {
background-color: white;
}
.myBtn:visited {
background-color: whitesmoke;
}
<button class="myBtn">Click here to change the background color</button>
this is what I have but it's not working
JavaScript will be required to achieve what you are asking for. As you currently have it implemented your CSS styling is styling the button and not the background of the page and the visited anchor is not valid on buttons.
var bkgColorChangeBtn = document.getElementById("backgroundColorChangeButton")
bkgColorChangeBtn.addEventListener('click', function(e) {
document.body.style.backgroundColor = 'red'
})
<body>
<button id="backgroundColorChangeButton" class ="myBtn">Click here to change the background color</button>
</body>
Above is a working example of what you are trying to do. Note this won't persist when the user refreshes the page. If you want it to persist you will have to save a user setting into a database and recall the information on page render which goes beyond regular HTML/CSS/JS.
I want to do something like this. I have a link inside a parent and I want to change the background of the parent and the link both whenever I click on the link. Is it possible?
Here is my fiddle http://jsfiddle.net/x5m5m035/
a:active {
background-color: yellow;
}
div{
background-color: red;
}
So when I click on the link, parent's background color should also be yellow.
I am sorry if it is too silly to ask.
Thanks
It's not possible using pure CSS because CSS hasn't parent selector, but you can do that using javascript or JQuery.
Try this JQuery code for instance:
$('a').mousedown(function() {
$(this).parent("div").css("background-color","yellow");
}).mouseup(function() {
$(this).parent("div").css("background-color","red");
});
Check JSFiddle Demo
There's no way to implement if without scripts.
I have this piece of code in my style sheet to change the cursor to a pointer on one page, and it works well except on firefox, where it doesn't give you a "pointer" finger to let you know the page is clickable.
html {
cursor:pointer;
}
The thing is, the normal method of putting an onclick event in the body tag isn't working. (shown below not working)
<body onclick="window.href.location=filename">
body text
</body>
How do I get a pointer finger on the whole page instead of the normal mouse cursor in firefox?
Add a hover rule to the body element.
body:hover {
cursor:pointer;
}
You'll have to fill your whole screen with the element.
html {
cursor:pointer;
width: 100%;
height: 100%;
}
Check this JSFiddle that I put together for you.
I guess you need to fill the entire page with some element like:
<div style="position:absolute;top:0px;left:0px;right:0px;bottom:0px">
</div>
and use this inside the body. There you probaly need to care about the bodies margins.
Or you append a click handler to the entire window via JavaScrip:
window.addEventListener("click", doSomething, false);
Make life easy:
CSS
#page{display:block;position:absolute;top:0;bottom:0:left:0;right:0;}
idI have these links on a page:
<div>
Item 1
Item 2
Item 3
</div>
I want to add a hover feature using css so that when a link is hovered over a image specific to that link appears as background image in the container div. I'm fairly new to jquery and js but I'd prefer a pure css solution if possible.
You have the answer to this question in your post: hover The CSS would be this:
#eq1:hover {
background-image: url ('../link_to_file.png');
}
jQuery:
You can do that in jQuery. But to do that in jQuery would be this, and also, do you have an image? I will tell you how to link the image. Use your image for that.
Example:
$(document).ready(function () {
$("#eq1").mouseover(function () { // function to run, when mouse comes over eq1
$("div").css("background", "image_image_link.png");
}
});
Instead of using div you can use (this).parent too because you said, you wanted the image to show up for the parent div. You can repeart the code for all of the three links.
Note: You cannot add CSS properties to some other divs in CSS, for that you must use jQuery.
Edit:
You wanted to add the images in some other div. That would be something like:
<div class="all-images">Add all the images here..</div>
<div class="image-viewer"></div>
Now what this actually is something like this: The .all-images is the container, that will contain all the images like thumbnails. And the .image-viewer will be used to show the image.
$(document).ready(function () {
$(".all-images").click(function () { //
$(".image-viewer").css("background", "image_image_link.png");
}
});
In the example I used, the click on the the image (.all-images) will show the image in the div. But remember to use something like a name of the image. Because jQuery won't remember which image was clicked. You can use something like src of that img.
try this
div:hover #id1{
background: white url('path/to/image.png') no-repeat top left;
}
I have the following html snippet:
page title goes here<br />
<span class="username">username goes here: </span><span class="dateandtime">date the time go here</span>
Here is the css for these classes
.title
{
color:#707070;
}
.username
{
color:#8DAAB8;
}
.dateandtime
{
color:#A5A7AC;
}
Is it possible to change the colors of these 3 items when hovering over the title?
The colors I want the items to change to are as follows
title = 000000
username = DF821B
dateandtime = 3185B6
Not sure if this is possible with css, if the html snippet structure needs to change, that will not be a problem.
I know this can be done with javascript, but wanted to know if it is possible without javascript.
Use the :hover pseudoclass:
.title:hover
{
color: #000000;
}
etc. This works in all browsers, except in IE6 and earlier, which doesn't support :hover on anything other than hyperlinks (A elements).
Edit 1: I see you want to change them all while hovering over the title. In that case, it becomes a little more complicated. You should put a <div> around it and apply the :hover pseudoclass on that. It won't just be the title (which is also possible, but has even less chance of working in IE). For that:
<div class="someclass">Title<span class="username">username</span><span class="dateandtime">date and time</span></div>
is your HTML, but your CSS would be:
.someclass .title:hover { color: #000000; }
.someclass .title:hover ~ .username { color: #DF821B; }
.someclass .title:hover ~ .dateandtime { color: #3185B6; }
Where ~ is the sibling selector (meaning it should have the same parent (.someclass) as the .title:hover).
#Harry Joy: No, it's not. My answer is different, not to mention I don't have enough rep to post comments.
Edit 2:
As requested, to make them all change while hovering over the entire container, use the above HTML with the following CSS:
.someclass:hover .title { color: #000000; }
.someclass:hover .username { color: #DF821B; }
.someclass:hover .dateandtime { color: #3185B6; }
(though basically credit for that goes to Spudley for suggesting it first).
Not totally clear on the question -- do you want each of them to have their own hover colour, or do you want all three to change colour at once, when you hover on any of them?
In the first case, it's easy: just add a :hover style for each of the three elements (you already have answers to this effect, so I won't repeat them here).
In the second case, you'll need a container element that would take the hover, so your code would look like this:
<span class='container'>
page title goes here<br />
<span class="username">username goes here: </span><span class="dateandtime">date the time go here</span>
</span>
(you may want to use <div> rather than <span>, but I'll leave that up to you)
Your CSS would then look like this:
.title {color:#707070;}
.username {color:#8DAAB8;}
.dateandtime {color:#A5A7AC;}
.container:hover .title {color:#000000;}
.container:hover .username {color:#DF821B;}
.container:hover .dateandtime {color:#3185B6;}
Obviously, change the colours in the new styles to whatever you want them to be. If all three should be the same, then you could simplify the three new styles down to something like this:
.container:hover span, .container:hover a, {color:#000000;}
Hope that helps.
One final thing to note: IE6 and below do not support the :hover style on anything except <a> elements. My recommendation to you is simply not to support IE6 for your site (there are plenty of other things broken in IE6 too), but if you do need to support it, there are hacks available to get :hover to work with it. See Whatever:Hover.
It's definitely possible, just append this to your CSS:
.title:hover
{
color:#000000;
}
.username:hover
{
color:#DF821B;
}
.dateandtime:hover
{
color:#3185B6;
}
This called a pseudo-class and will make your anchors change color when hovered )
Edit:
At first I misunderstood your question, this isn't the solution!
You can't do this in CSS alone, but you can do it jQuery easily!
Here's an example.
What you need to do is set up a class for each of the hovered states, then use jQuery to replace add a class that will change the colors as you want :)
You just have to include the jQuery framework if you haven't already:
In the <head>:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
Well you could do this one of two ways but not with CSS, you can either add and remove the appropriate classes (unobtrusive JavaScript) or change the styles directly. For instance:
document.getElementById('someElement').style.color = '#FF0000';
Or you can use a JavaScript library such as jQuery.
jQuery('p.someClass').mouseOver(function(e) {
e.target.style.color = '#FF0000';
})
.mouseOut(function(e) {
e.target.style.color = '#000000';
});