Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

CSS button not changing colour during hover (css and html)

For some reason my code is not allowing me to change the background colour of my button when I hover over it.

.button {
    display:inline-block;
    padding:0.3em 1.2em;
    margin:0 0.1em 0.1em 0;
    border:0.16em solid rgba(255,255,255,0);
    border-radius:2em;
    box-sizing: border-box;
    text-decoration:none;
    font-family:'Roboto',sans-serif;
    font-weight:300;
    color:#FFFFFF;
    text-shadow: 0 0.04em 0.04em rgba(0,0,0,0.35);
    text-align:center;
    transition: all 0.2s;
    background-color:#f14e4e
}

.button.new-quote:hover {
?   background-color: black !important;
}
<input type="button" class="new-quote button" value= 'test'/>
question from:https://stackoverflow.com/questions/65866240/css-button-not-changing-colour-during-hover-css-and-html

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is nothing wrong with your selector. You have correctly selected the button hover. I have run your code and guess what it is working fine.

I found the problem why did your code didn't work.

.button.new-quote:hover {
?   background-color: black !important;
}

This is your selector and now checks my selector

.button.new-quote:hover {
  background-color: black !important;
}

Can you see a little difference here? That is extra whitespaces behind background-color property. I have removed all the spaces first and then added two spaces and then it worked as we all are expected.

In my opinion, I think you have copied the background-color property from somewhere else which has a copy policy to add hidden character spaces.

.button {
  display: inline-block;
  padding: 0.3em 1.2em;
  margin: 0 0.1em 0.1em 0;
  border: 0.16em solid rgba(255, 255, 255, 0);
  border-radius: 2em;
  box-sizing: border-box;
  text-decoration: none;
  font-family: 'Roboto', sans-serif;
  font-weight: 300;
  color: #FFFFFF;
  text-shadow: 0 0.04em 0.04em rgba(0, 0, 0, 0.35);
  text-align: center;
  transition: all 0.2s;
  background-color: #f14e4e
}

.button.new-quote:hover {
  background-color: black !important;
}
<input type="button" class="new-quote button" value='test' />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...