【问题标题】:HTML and CSS Troubles with Hover悬停时的 HTML 和 CSS 问题
【发布时间】:2016-07-04 17:43:43
【问题描述】:

我正在做一个网站。耶!嗯是的。我在右上角有一个导航栏,但是当我尝试将鼠标悬停在它上面时,悬停不起作用,只有当我将鼠标悬停在某个部分时:就在单词的顶部。其余的链接虽然有效。 这是HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>My Blog</title>
        <meta charset="utf-8"/>
        <link rel="stylesheet" type="text/css" href="blog.css">
        <link rel="icon" href="http://images4.fanpop.com/image/photos/22600000/Smiley-Face-smiley-faces-22608094-1000-1000.png">

        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="jquery.mmenu.js"></script>
        <script type="text/javascript" src="app.js"></script>

        <link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">
        <link type="text/css" rel="stylesheet" href="jquery.mmenu.css" />
    </head>
    <body>
        <div class="header">
            <div class="navbar">
                <ul>
                    <li><a id="strawpoll" href="#"> Strawpoll </a></li>
                    <li><a id="previousblogs" href="#"> Previous Blogs </a></li>
                    <li><a id="aboutme" href="#"> About Me </a></li>
                </ul>
            </div>
            <h1><a href="#">My Life</a></h1>
        </div>
        <div>
            <h2>Recent Blogs</h2>
            <h3><a href="#">Week 1</a></h3>
            <a href="file:///E:/Week%201/week%201.html"><img style='border:5px solid #FFFFFF' width="400" height="300" src="http://wallpapercave.com/wp/YQ9Usfh.jpg"></a>
            <h4>Short Description................................................................................................</h4>
            <h3><a href="#">Week 2</a></h3>
            <img style='border:5px solid #FFFFFF' width="400" height="300" src="http://wallpoper.com/images/00/17/67/51/anime-forest_00176751.jpg">
            <h4>Short Description................................................................................................</h4>
            <h3><a href="#">Week 3</a></h3>
            <img style='border:5px solid #FFFFFF' width="400" height="300" src="http://wallpapercave.com/wp/WWFeR4k.jpg">
            <h4>Short Description................................................................................................</h4>
        </div>
        <footer class="container">
            <div class="pictures" >
                <a href="https://twitter.com/"><img width="30" src="https://s3.amazonaws.com/codecademy-content/projects/make-a-website/lesson-4/twitter.svg"></a>
                <a href="https://www.facebook.com/"><img width="30" src="https://s3.amazonaws.com/codecademy-content/projects/make-a-website/lesson-4/facebook.svg"></a>
                <a href="https://www.instagram.com/"><img width="30" src="https://s3.amazonaws.com/codecademy-content/projects/make-a-website/lesson-4/instagram.svg"></a>
            </div>
        </footer>
    </body>
</html>

这是 CSS:

#navbar ul li a:hover {
    text-decoration: underline;
}
.header {
    position: fixed;
    top: -20px;
    left: 0px;
    width: 100%;
    background: #000000;
    border-left: 5px solid white;
}
.header ul {
    list-style-type: none;
}
.header ul li a {
    color: white;
    position: fixed;
    top: 13px;
    font-weight: bold;
}
.header ul li a:hover {
    color: royal-blue;
}
a#strawpoll {
    right: 215px;
}
a#previousblogs {
    right: 95px;
}
a#aboutme {
    right: 15px;
}
h1 {
    text-align: left;
    position: relative;
    left: 10px;
    top: 15px;
    color: white;
}
body {
    position: relative;
    top: 60px;
    font-family: 'Raleway', sans-serif;
    background-image: url('https://cms-images.idgesg.net/images/article/2015/11/black-100630491-orig.jpg');
    background-size: cover;
    color: white;
    text-align: center;
}
a:link {
    color: white;
    text-decoration: none;
}
.header a:hover {
    text-decoration: underline;
}
a:visited {
    color: white;
    text-decoration: none;
}
h3 a:hover {
    text-decoration: underline;
}
div .pictures {
    position: relative;
    left: 610px;
    top: 40px;
}

【问题讨论】:

  • 又是什么“测试”墙……?是否只是为了绕过 more-text-need-for-this-much-code 过滤器?
  • 哇,学习更好地格式化问题
  • 作为建议,我可以在您的 CSS 中看到您使用链接选择器作为示例,如下所示:a#divid。不要在选择器的开头包含a,只写id。
  • 通常,代码本身很好,但上下文错误。发布指向您的网站草稿的链接或在 jsfiddle 等在线工具中重新创建问题(谷歌它!)。如果到那时,您还没有自己解决问题,请回来,发布问题,定义预期和实际行为并向我们展示您的尝试。

标签: html css hover


【解决方案1】:

我根据您的代码制作了小提琴(有一些修复):https://jsfiddle.net/wka034en/1/

问题是,您的 H1 标题与您的导航重叠。

只是添加:

.header ul li a {
    /* ... */
    z-index: 10;
}

...用您的代码解决您的问题:https://jsfiddle.net/wka034en/2/


但是,这与良好的做法相去甚远。 为什么要单独定位每个导航链接?你可以只定位整个列表:https://jsfiddle.net/wka034en/3/

.header ul {
    list-style-type: none;
    position: fixed;
    right: 10px;
    top: 10px;
    z-index: 10;
}
.header ul li {
    display: inline-block;
    margin: 0 8px;
}
.header ul li a {
    color: white;
    font-weight: bold;
}

大多数浏览器都有开发工具 - 或者您可以安装 Firebug 等插件。这些工具可以通过按F12 或通过菜单激活。在一些浏览器(如 Safari)中,您需要先在设置中启用开发者模式。
您可以右键单击一个元素并从上下文菜单中选择类似 "inspect" 的内容,以便跳转到开发人员窗口中正确的 DOM 位置。
使用这些,您可以在下次自己快速确定原因的根源。


关于水平滚动条的其他问题:

那是因为你的H1 默认是块类型。您将其相对移动 10px 从左侧定位。所以你的整个内容占据了 100% 宽度加上 10px 的位置。这就是需要滚动条的原因。你可以在你的身体上使用overflow-x:hidden,或者只是避免这种相对定位这样的元素的不良做法。
只需使用边距和内边距即可。

将您的 CSS 更改为:

h1 {
    /* ... */
    padding-left: 10px;    /* was: 'left: 10px;' */
    /* ... */
}

见:https://jsfiddle.net/Ldcjmkn8/


如需进一步指导,我建议您阅读有关重置 CSS 的内容:http://cssreset.com/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-28
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 2011-07-11
    • 1970-01-01
    相关资源
    最近更新 更多