【问题标题】:Selecting color when hovering over navbar悬停在导航栏上时选择颜色
【发布时间】:2016-12-27 23:38:18
【问题描述】:

所以我有这个导航栏,我想在鼠标悬停时更改它的颜色。它的默认值为黑色,我希望它是浅灰白色。我在这里做错了什么?

HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Frontend</title>
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="box">
                <div id="header">
                    <br />
                    <div id="searchMenu">
                        <input id="sweBtn" type="button" value="SVENSKA" />
                        <input id="engBtn" type="button" value="ENGLISH" />
                        <input id="searchTxt" type="text" />
                        <input id="searchBtn" type="button" value="SÖK" />
                    </div>
                    <br />
                </div>
                <div class="col-sm-4 text-left" id="imgText">
                    <h3>Title</h3>
                    <p>Paragraph</p>
                </div>
                <div class="col-sm-4">
                    <img id="imgHeader" src="~/img/Header.png" />
                </div>
                <div>
                    <ul id="navBar"> //Here is the navbar I am talking about
                        <li><a>BEHÖVER DU AVOKAT?</a></li>
                        <li><a>ADVOKATETIK</a></li>
                        <li><a>ATT BLI ADVOKAT</a></li>
                        <li><a>UTBILDNING</a></li>
                        <li><a>ADVOKATSSAMFUNDET TYCKER</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <footer>

    </footer>
</body>
</html>

<link href="~/Content/custom.css" rel="stylesheet" />

CSS:

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333;
}

li {
    float: left;
}

    li a {
        display: block;
        color: white;
        text-align: center;
        padding: 14px 16px;
        text-decoration: none;
    }

        li a:hover {
            background-color: #111;
        }

#header {
    background-color: #503F31;
    color: white;
    padding: 30px;
}

#searchMenu {
    margin-left: 1450px;
}

#sweBtn {
    font-family: 'Times New Roman';
    background-color: Transparent;
    color: grey;
}

#engBtn {
    font-family: 'Times New Roman';
    background-color: Transparent;
    color: gold;
}

#searchTxt {
    background-color: grey;
    border-color: grey;
}

#searchBtn {
    font-family: 'Times New Roman';
    background-color: Transparent;
    color: gold;
}

#imgText {
    font-size: 25px;
    position: absolute;
}

#imgHeader {
    width: 1904px;
}

#navBar {
    font-family: 'Times New Roman';
    color: gold;
    background-color: #503F31;
}

#footer {
    background-color: grey;
    color: white;
    padding-bottom: -250px;
}

*::selection { /*This should handle the hover color situation right?*/
  background: #cc0000;
  color: #ffffff;
}
*::-moz-selection {
  background: #cc0000;
  color: #ffffff;
}
*::-webkit-selection {
  background: #cc0000;
  color: #ffffff;
}

还有一个旁注:我希望每个列表项都用这样的垂直线分隔:“|”

【问题讨论】:

  • 那么你的悬停 CSS 规则在哪里?
  • *::selection { /*这应该处理悬停颜色的情况吧?*/ background: #cc0000;颜色:#ffffff; }
  • 不,那是完全不同的事情。

标签: html css twitter-bootstrap colors


【解决方案1】:

li a:hover 背景颜色更改为 #CCC 并删除它:

*::selection { /*This should handle the hover color situation right?*/
  background: #cc0000;
  color: #ffffff;
}
*::-moz-selection {
   background: #cc0000;
   color: #ffffff;
}
*::-webkit-selection {
   background: #cc0000;
   color: #ffffff;
}

我假设您的 #111 不是“黑色”而是“黑色”,因此被视为黑色 ;)。

【讨论】:

    【解决方案2】:

    只需替换这些块:

    li a {
        display: block;
        color: white;
        text-align: center;
        padding: 14px 16px;
        text-decoration: none;
        /* Separator Border */
        border-right: 1px solid #000;
    }
    
    li a:hover {
        /* Background color on hover */
        background-color: silver;
        color: #fff;
    }
    

    最终的CSS:

    ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: #333;
    }
    
    li {
        float: left;
    }
    
    li a {
        display: block;
        color: white;
        text-align: center;
        padding: 14px 16px;
        text-decoration: none;
        border-right: 1px solid #000;
    }
    
    li a:hover {
        background-color: silver;
        color: #fff;
    }
    
    #header {
        background-color: #503F31;
        color: white;
        padding: 30px;
    }
    
    #searchMenu {
        margin-left: 1450px;
    }
    
    #sweBtn {
        font-family: 'Times New Roman';
        background-color: Transparent;
        color: grey;
    }
    
    #engBtn {
        font-family: 'Times New Roman';
        background-color: Transparent;
        color: gold;
    }
    
    #searchTxt {
        background-color: grey;
        border-color: grey;
    }
    
    #searchBtn {
        font-family: 'Times New Roman';
        background-color: Transparent;
        color: gold;
    }
    
    #imgText {
        font-size: 25px;
        position: absolute;
    }
    
    #imgHeader {
        width: 1904px;
    }
    
    #navBar {
        font-family: 'Times New Roman';
        color: gold;
        background-color: #503F31;
    }
    
    #footer {
        background-color: grey;
        color: white;
        padding-bottom: -250px;
    }
    
    *::selection { /*This should handle the hover color situation right?*/
      background: #cc0000;
      color: #ffffff;
    }
    *::-moz-selection {
      background: #cc0000;
      color: #ffffff;
    }
    *::-webkit-selection {
      background: #cc0000;
      color: #ffffff;
    }
    

    【讨论】:

      【解决方案3】:

      尝试添加大括号:

      li a:hover {{background-color: #111;}}
      

      【讨论】:

      • 这不是有效的 CSS。
      猜你喜欢
      • 1970-01-01
      • 2021-03-10
      • 2013-05-13
      • 1970-01-01
      • 2019-06-27
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      相关资源
      最近更新 更多