html - 当我将鼠标悬停在一个元素上时,为什么整个内容框不会改变颜色?左边有空位

标签 html css hover

我正在尝试创建一个响应式侧边菜单以滑入和滑出。我希望每个元素的悬停状态都可以扩展容器的整个宽度,但由于某种原因总是存在间隙,我无法让文本右对齐。我已经尝试了我所知道的一切,并尽我所能进行了研究,但一无所获。有人可以帮助我了解出了什么问题吗?

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="style.css">
    <link href="https://fonts.googleapis.com/css?family=Raleway:300,500,700,900" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Brushworks NW Inc.</title>
</head>
<body>
    <nav class="navbar animated fadeIn">
        <span class="open-slide">
            <a href="#" onclick="openSlideMenu()">
                <svg width="30" height="30">
                    <path d="M0,5 30,5" stroke="#000" stroke-width="5"/>
                    <path d="M0,14 30,14" stroke="#000" stroke-width="5"/>
                    <path d="M0,23 30,23" stroke="#000" stroke-width="5"/>
                </svg>
            </a>
        </span>
        <ul class="navbar-links">
            <li><a class="active" href="#">HOME</a></li>
            <li><a href="#">ABOUT US</a></li>
            <li><a href="#">SERVICES</a></li>
            <li><a href="#">GALLERY</a></li>
            <li><a href="#">CONTACT US</a></li>
            <li id="number">(360) 679-4444</li>
        </ul>
    </nav>
    <div class="side-hidden">

    </div>
    <div id="side-menu" class="side-nav">
        <ul>
            <li><a href="#" class="btn-close" onclick="closeSlideMenu()">&times;</a></li><br>
            <li class="nav-item"><a href="#">Home</a></li>
            <li class="nav-item"><a href="#">About</a></li>
            <li class="nav-item"><a href="#">Services</a></li>
            <li class="nav-item"><a href="#">Contact</a></li>
        </ul>
    </div>

    <script>
        function openSlideMenu(){
          document.getElementById('side-menu').style.width = '250px';
        }

        function closeSlideMenu(){
          document.getElementById('side-menu').style.width = '0';
        }
    </script>

</body>
</html>
body, html{
    font-family: 'Raleway', sans-serif;
    margin: 0;
    padding: 0;
    width: 100%;
}

.container{
    max-width: 980px;
    margin-left: auto;
    margin-right: auto;
}

/*-- ----------------------------------------------------------------------------------- -->
<!--                                  NAVIGATION                                         -->
<!-- ----------------------------------------------------------------------------------- --*/

.navbar {
    background-color: #FFFFFF;
    width: 100%;
    height: 100px;
    margin: 0;
    border-bottom: 8px solid #CE2026;
}
.navbar-links {
    max-width: 1090px;
    margin-left: auto;
    margin-right: auto;
    position: relative;
    bottom: 15px;
    left: 30px;
}

.navbar-links a:hover {
    background: #831517;
    color: #FFFFFF;
    transition: background 0.4s ease 0s;
    transition-property: background;
    transition-duration: 0.4s;
    transition-timing-function: ease;
    transition-delay: 0s;
 }

.navbar-links a {
    display: inline;
    color: #444444;
    text-decoration: none;
    text-align: center;
    margin: 0 5px 0 0;
    font-size: 15px;
    padding: 10px 17px;
    right: 25px;
}

nav .active {
    background: #CE2026;
    color: #FFFFFF;
 }

.navbar ul {
    margin: 0 auto;
    padding: 0;
    list-style: none;
    text-decoration: none;
    width: 100%;
    height: 100px;
}


ul li {
    list-style: none;
    display: inline-block;
    margin-top: 50px;
    font-size: 1.4em;
}


#number {
    float: right;
    padding-right: 0;
    margin-top: 50px;
    margin-right: 85px;
    font-size: 21px;
    font-weight: normal;
}

.side-hidden {
    background-color: #000;
    opacity: 0.6;
    height: 1000px;
    width: 100%;
    position: relative;
    top: 0;
    right: 250px;
    margin: 0;
    display: none;
}

/* Responsive Menu*/
.open-slide {
    float: right;
    z-index: 5;
    display: none;
}

.side-nav {
    width: 0;
    height: 100%;
    z-index: 1;
    position: fixed;
    background-color: #111;
    opacity: 0.9;
    transition: 0.5s;
    right: 0px;
    text-align: right;
    display: block;
    transition: 0.3s;
    z-index: 6;
}

.side-nav ul {
    display: block;
    text-decoration: none;
    color: #ccc;
    z-index: 6;
}

.side-nav ul a {
    position: relative;
    width: 100%;
    padding: 10px 130px 10px 100px;
    text-decoration: none;
    color: #ccc;
    margin-right: 20px;
    z-index: 6;
    right: 30px;
    text-align: right;
}

.side-nav .btn-close {
    position: absolute;
    top: 0;
    left: 22px;
    font-size: 36px;

}

.side-nav a:hover {
    color: #fff;
    background: #CE2026;
}

@media only screen and (max-width: 1000px) {
  .navbar-links {
    display: none;
  }

  .open-slide {
    display: block;
    margin-right: 5%;
    margin-top: 3.5%;
  }
}

首先最小化屏幕以切换移动 View 。当您单击汉堡菜单时,它会滑出。但是,当您将鼠标悬停在导航链接上时,只有一部分元素 block 高亮显示为红色,而不是整个宽度。并且突出显示 block 应该是一个在另一个之上,但是导航链接之间存在间隙。我不知道为什么我不能让它做我想做的事,我觉得我已经尝试了一切。

最佳答案

在#side-menu 中给你的无序列表一个 0px 的填充;然后从 .nav-item 中的 anchor 中删除 right: 30px;。我还调整了您的一些属性以使其工作。您可以在下面看到工作代码(查看完整页面 View ):

body, html{
    font-family: 'Raleway', sans-serif;
    margin: 0;
    padding: 0;
    width: 100%;
}

.container{
    max-width: 980px;
    margin-left: auto;
    margin-right: auto;
}

/*-- ----------------------------------------------------------------------------------- -->
<!--                                  NAVIGATION                                         -->
<!-- ----------------------------------------------------------------------------------- --*/

.navbar {
    background-color: #FFFFFF;
    width: 100%;
    height: 100px;
    margin: 0;
    border-bottom: 8px solid #CE2026;
}
.navbar-links {
    max-width: 1090px;
    margin-left: auto;
    margin-right: auto;
    position: relative;
    bottom: 15px;
    left: 30px;
}

.navbar-links a:hover {
    background: #831517;
    color: #FFFFFF;
    transition: background 0.4s ease 0s;
    transition-property: background;
    transition-duration: 0.4s;
    transition-timing-function: ease;
    transition-delay: 0s;
 }

.navbar-links a {
    display: inline;
    color: #444444;
    text-decoration: none;
    text-align: center;
    margin: 0 5px 0 0;
    font-size: 15px;
    padding: 10px 17px;
    right: 25px;
}

nav .active {
    background: #CE2026;
    color: #FFFFFF;
 }

.navbar ul {
    margin: 0 auto;
    padding: 0;
    list-style: none;
    text-decoration: none;
    width: 100%;
    height: 100px;
}


ul li {
    list-style: none;
    display: inline-block;
    margin-top: 50px;
    font-size: 1.4em;
}


#number {
    float: right;
    padding-right: 0;
    margin-top: 50px;
    margin-right: 85px;
    font-size: 21px;
    font-weight: normal;
}

.side-hidden {
    background-color: #000;
    opacity: 0.6;
    height: 1000px;
    width: 100%;
    position: relative;
    top: 0;
    right: 250px;
    margin: 0;
    display: none;
}

/* Responsive Menu*/
.open-slide {
    float: right;
    z-index: 5;
    display: none;
}

.side-nav {
    width: 0;
    height: 100%;
    z-index: 1;
    position: fixed;
    background-color: #111;
    opacity: 0.9;
    transition: 0.5s;
    right: 0px;
    text-align: right;
    display: block;
    transition: 0.3s;
    z-index: 6;
}

.side-nav ul {
    display: block;
    text-decoration: none;
    color: #ccc;
    z-index: 6;
    /* Added this */
    padding: 0;
}

/* Added this */
.nav-item {
    display:block;
}

.side-nav ul a {
    position: relative;
    width: 100%;
    /* Changed this */
    /* padding: 10px 130px 10px 100px;*/
    padding: 10px 15px;
    text-decoration: none;
    color: #ccc;
    margin-right: 20px;
    z-index: 6;
    /* Removed this */
    /* right: 30px; */
    text-align: right;
    /* Added these */
    display: block;
    box-sizing: border-box;
}

.side-nav .btn-close {
    position: absolute;
    top: 0;
    /* Changed this */
    /* left: 22px; */
    left: 0;
    font-size: 36px;

}

.side-nav a:hover {
    color: #fff;
    background: #CE2026;
}

@media only screen and (max-width: 1000px) {
  .navbar-links {
    display: none;
  }

  .open-slide {
    display: block;
    margin-right: 5%;
    margin-top: 3.5%;
  }
}
<nav class="navbar animated fadeIn">
        <span class="open-slide">
            <a href="#" onclick="openSlideMenu()">
                <svg width="30" height="30">
                    <path d="M0,5 30,5" stroke="#000" stroke-width="5"/>
                    <path d="M0,14 30,14" stroke="#000" stroke-width="5"/>
                    <path d="M0,23 30,23" stroke="#000" stroke-width="5"/>
                </svg>
            </a>
        </span>
        <ul class="navbar-links">
            <li><a class="active" href="#">HOME</a></li>
            <li><a href="#">ABOUT US</a></li>
            <li><a href="#">SERVICES</a></li>
            <li><a href="#">GALLERY</a></li>
            <li><a href="#">CONTACT US</a></li>
            <li id="number">(360) 679-4444</li>
        </ul>
    </nav>
    <div class="side-hidden">

    </div>
    <div id="side-menu" class="side-nav">
        <ul>
            <li><a href="#" class="btn-close" onclick="closeSlideMenu()">&times;</a></li><br>
            <li class="nav-item"><a href="#">Home</a></li>
            <li class="nav-item"><a href="#">About</a></li>
            <li class="nav-item"><a href="#">Services</a></li>
            <li class="nav-item"><a href="#">Contact</a></li>
        </ul>
    </div>

    <script>
        function openSlideMenu(){
          document.getElementById('side-menu').style.width = '250px';
        }

        function closeSlideMenu(){
          document.getElementById('side-menu').style.width = '0';
        }
    </script>

关于html - 当我将鼠标悬停在一个元素上时,为什么整个内容框不会改变颜色?左边有空位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56096203/

相关文章:

html - 更改此 img 正方形并在卡内响应

c# - Asp.net 从代码后面添加表不能按预期工作

html - 将鼠标悬停在父级上而不覆盖子级背景

CSS Transform - 保持悬停状态的值(value)

javascript - 通过 javascript 和本地存储具有明显安全性的 Web 应用程序

html - 使用 Bootstrap 折叠导航菜单后如何对齐导航链接

JavaScript/HTML5/angularjs 更改 html 标签 var 的值

jquery - 隐藏最后一个元素时防止删除圆 Angular

firefox - css3 flexbox 在绝对 div 中水平对齐

具有圆形边缘且无溢出的 CSS 色调图像