【问题标题】:unordered list-item and anchor borders overflow parent container无序列表项和锚边框溢出父容器
【发布时间】:2016-05-20 03:57:33
【问题描述】:

我在另外两个 div 旁边有一个简单的无序列表,所有 3 个都包含在一个父容器中。它的外观如下:https://jsfiddle.net/f0bz4hc0/1/。如果放大,您可以看到所有 3 个在它们周围都有边框,但是列表项和锚点周围的边框超出了容器的底部边框,而另外两个 div 是紧贴的。为什么会发生这种情况,我该如何解决它以使所有内容都在容器中?

    <!doctype html>
<html>
<head>
    <title>practice</title>
    <meta charset='utf-8' />
    <style type='text/css'>
    *{
        margin:0;
        padding:0;
        box-sizing:border-box;
    }
    .page{
        width:960px;
        height:300px;
        margin:0 auto;
        border:1px solid red;
    }
    .row{
        width:100%;
    }
    .header{
        background-color:black;
        height:40px;
    }
    .logo{
        background-color:orange;
        height:100%;
        width:40px;
        float:left;
        display: inline-block;
    }
    .home{
        display: inline-block;
        height:40px;
        width:40px;
        float:left;
        border:1px solid cyan;
    }
    .home img{
        width:100%;
        height:100%;
    }
    ul{
        float:left;
        height:100%;
    }
    ul li{
        display: inline-block;
        border:1px solid teal;
    }
    a{
        display: inline-block;
        color:white;
        height:40px;
        line-height: 40px;
        border:1px solid orange;
    }
    </style>
</head>
<body>
    <div class='page'>
        <div class='header row'>
            <div class='logo'></div>
            <div class='home'><img src='house.png' /></div>

            <ul>
                <li><a href="#">a</a></li>
                <li><a href="#">a</a></li>
                <li><a href="#">a</a></li>
            </ul>
        </div>
    </div>
</body>
</html>

【问题讨论】:

  • 你不能同时使用inline-block和float...选择一个或另一个...不能同时使用。
  • 知道了。删除了浮动,并与 display:inline-block 一起使用。

标签: css html-lists overflow border containers


【解决方案1】:

如果您删除 .header 上的设置高度并将其设置为 inline-block,您将看到锚点上的边框仍包含在其父项中。

.header {
  /*height: 40px;*/
  background-color:black;
  display: inline-block;
}

https://jsfiddle.net/q5mxbywp/

【讨论】:

  • 这绝对解决了问题,但是删除标题上的高度会摆脱向左浮动的橙色 div,并且锚看起来不像垂直居中 - 它们看起来更接近底部,即使没有填充。
  • 通过将所有内容放在 height:100% 上来修复橙色 div(出于某种原因,这可行),但未垂直对齐的锚仍然困扰着我:jsfiddle.net/q5mxbywp/6
猜你喜欢
  • 1970-01-01
  • 2014-08-05
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-14
  • 2019-01-14
相关资源
最近更新 更多