Bootstrap内容与固定宽度侧边栏不能正确布局

时间:2015-10-22 18:09:53

标签: html css twitter-bootstrap clearfix

第一行列与第二行之间存在较大的白色间隙。

JSFiddle:https://jsfiddle.net/5o3ug26g/

<!DOCTYPE html>
<html>
<head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .content {
            margin-left: 170px;
            margin-right: 170px;
        }

        .sidebar { width: 160px; height: 600px; }
        .left { float: left; background: forestgreen; }
        .right { float: right; background: steelblue; }
    </style>
</head>


<body>
    <div>
        <div class="left sidebar"></div>

        <div class="right sidebar"></div>

        <div class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-6">Text on top of the page</div>
                    <div class="col-md-6">Other text on top of the page</div>
                </div>

                <div class="row">
                    <div class="col-md-6">I want this to be right below the "Text on top of the page" but it isn't...</div>
                    <div class="col-md-6">... and I want this to be right below the "Other text on top of the page" but it isn't</div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

如果删除row类,则差距消失,但使用Bootstrap navbar - s和其他容器:after { display: table; }时也会出现此问题,因此只需删除{{1类不是解决方案。

我尝试在一堆地方添加row,但无济于事。

这是一个更复杂的响应式布局的摘录,我宁愿避免重构。

最简单的解决办法是什么?

2 个答案:

答案 0 :(得分:1)

您可以绝对放置左右侧边栏而不是浮动它们。浮动正在干扰引导布局。

.left { background: forestgreen; position:absolute; left:0; top:0; }
.right { background: steelblue; position:absolute; right:0; top:0;}

来自您的实时示例:https://jsfiddle.net/5fk5900s/1/

答案 1 :(得分:1)

马塞洛是对的。您已经为内容添加了边距以考虑边栏。这是绝对定位变化的小提琴:

https://jsfiddle.net/5o3ug26g/1/

.content {
    margin-left: 170px;
    margin-right: 170px;
}

.sidebar { 
    position: absolute;
    width: 160px; 
    height: 600px; 
}
.left { 
    left: 0px; 
    background: 
    forestgreen; 
}
.right { 
    right: 0px; 
    background: steelblue; 
}
相关问题