Ask coding questions

← Back to all posts
Why use Javascript instead of CSS For rollovers
AIC27 (0)

###If you are making a very simple rollover, should you use javascript instead of CSS? this code

#button1{
color:green;
}

#button1:hover{
color:purple;
}

seems a lot easier to me than

var button1 = document.getElementById("button1")
function rollover(){
button1.style.color = red
}

function rollback(){
button1.style.color = blue
}

button1.onmouseover = rollover
button1.onmouseout = rollback

if a rollover is this simple, should I just use CSS instead of JS?

Answered by EpicGamer007 (1735) [earned 5 cycles]
View Answer
Voters
AIC27 (0)
Comments
hotnewtop
EpicGamer007 (1735)

always use css, never use js for setting up animations/css. if you want to change the color(or change anything style-wise) of a div or button, always use css unless it is absolutely necesary to use javascript

also, the tutorials section is for posting tutorials, for questions, you post to the ask board.

AIC27 (0)

@EpicGamer007 oops. Didn't realize I had.

Bookie0 (6259)

CSS was invented for styling webpages. JS is to make it interactive. Also, it isn't easier as first of all the CSS looks a bit more intuitive/easier to use than the JS code, and second of all there are less lines in the CSS.

xxpertHacker (930)

JS will over-complicate everything, just use HTML and CSS, if you even need CSS, that is.