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
234 views
in Technique[技术] by (71.8m points)

html - Change text color inside box on hover

So right now self teaching myself so html and CSS and I have a slight problem. In my menu bar I have some list items that I want to change color when the mouse hovers over the box, but I'm unsure how to do that. Right now I have:

#Menu li:hover{
    background-color: rgba(255,165,0,0.5)
}

#Menu li a:hover{
    color: black;
}

But this makes it where when I hover over the text it changes and only the box changes. I tried:

#Menu li:hover{
    background-color: rgba(255,165,0,0.5)
    color: black;
}

but that does nothing except change the box color. Also having a gradual change instead of an abrupt change would be nice as well.

#Menu {
    height: 40px;
    background-color: rgb(255,165,0);

}
#Menu ul{
    margin:auto;
    padding:0;
}

#Menu li {
    list-style-type: none;
    float: left;
    display: block;
    width:20%;
    height:40px;
    text-align: center;
    line-height:40px;
    font-family: inherit;
    font-size:16px;
}

#Menu li a{
    text-decoration: none;
    color:white;
    position: fixed;

}

#Menu li:hover {
    background-color: #FFB733;

}

#Menu li a:hover {
    color: black;
}
<html>
  <head>
    <style type="text/css">
      body {
        margin: 0;
      }
    </style>
  </head>
  
  <body>
    <div id="Menu">
      <ul>
        <li><a href="#">Cat</a></li>
        <li><a href="#">Dog</a></li>
        <li><a href="#">Mouse</a></li>
      </ul>
   </div>
  </body>
</html>
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)
#Menu li {
    transition: 2s;
}

#Menu li:hover {
    background-color: rgba(255,165,0,0.5);
    color: red;
}

See the example http://jsfiddle.net/k35rdfhp/1/

The item change background-color and color when hover the box of item, not only the text.


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

...