html - 为什么这个 anchor 标记没有占用其父 div 的 100% 可用空间?

标签 html anchor css

我的理解使我相信将 <a>display:block; <div> 内的 CSS 属性应该会导致整个 div“成为”链接(即我在 div 中悬停的任何地方,它被认为是链接,因为链接占据了 100% 的高度和宽度)。

该行为在下面的示例中似乎不起作用。如果你愿意 inspect element并用实时代码四处看看,您可以查看网站@:http://shayla.phasesolutions.ca/ .有关我正在描述的问题,请参阅页面顶部的导航。

HTML:

    <header>
        <div class="logo"><a href="#"></a></div>
        <div class="nav-homepage"><a href="#"></a></div>
        <div class="nav-webdesign"><a href="#"></a></div>
        <div class="nav-graphicdesign"><a href="#"></a></div>
        <div class="nav-miscartwork"><a href="#"></a></div>
        <div class="nav-aboutme"><a href="#"></a></div>
        <div class="nav-contactme"><a href="#"></a></div>
    </header>

CSS:

header {
    .site-section;
    height: 125px;

    div {
        float: left;
        position: relative;
        top: 10px;

        a {
            display: block;
        }
    }

    div:first-child {
        position: relative;
        top: 0px;
    }
}
.nav-aboutme, .nav-contactme, .nav-graphicdesign, .nav-homepage, .nav-miscartwork, .nav-webdesign, .shadow, .homepage-nav-aboutme, .homepage-nav-contactme, .homepage-nav-graphicdesign, .homepage-nav-miscartwork, .homepage-nav-webdesign, .logo{
    background: url(/resource/img/sprites.png) no-repeat;
}

.nav-aboutme {
    background-position: -81px -361px ;
    width: 76px;
    height: 105px;
    margin-right: 49px;
}

.nav-contactme {
    background-position: 0 -360px ;
    width: 76px;
    height: 105px;
}

.nav-graphicdesign {
    background-position: -79px -246px ;
    width: 89px;
    height: 105px;
    margin-right: 49px;
}

.nav-homepage {
    background-position: 0 -245px ;
    width: 76px;
    height: 105px;
    margin-right: 49px;
}

.nav-miscartwork {
    background-position: -79px -132px ;
    width: 85px;
    height: 105px;
    margin-right: 49px;
}

.nav-webdesign {
    background-position: 0 -132px ;
    width: 76px;
    height: 105px;
    margin-right: 49px;
}

最佳答案

我相信是因为 block 元素里面没有内容。如果在 anchor 链接内添加一个字母, anchor 链接将变为 div 宽度的 100%。

如果添加 width:100% 和 height:100% 它将占据整个 div。

关于html - 为什么这个 anchor 标记没有占用其父 div 的 100% 可用空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18324819/

相关文章:

css - html5 样板文件不包括 reset.css?

javascript - 使 div 跟随另一个 div 的位置

html - 如何在不影响 Bootstrap 中的其他表的情况下使 1 个表可滚动并带有固定标题

html - 如何让这张响应式卡片变得更大?

html - Play Framework 中的客户端表单验证

html - 如何将 anchor 链接到具有确切位置的新页面

html - Internet Explorer 8 添加第二个列组

delphi - 如何使用 FireMonkey 设置控制 anchor ?

angular - [attr.disabled] ="true"在 anchor 标记中不起作用

.net - @font-face 在服务器(.net 应用程序)中不起作用