简体   繁体   English

CSS-如何防止子元素增加其父元素的高度,而是溢出?

[英]CSS - How can I prevent a child element from increasing the height of it's parent, and instead overflow?

I'm using Bootstrap 3 and want to create a layout kind of like this: 我正在使用Bootstrap 3,并希望创建如下布局:

图

The big red box is a large header for the page. 大红色框是该页面的大页眉。 I have wording inside this box that is spanning 8 columns, then a sidebar which spans 4. I want the sidebar to start inside the header box, then overflow it to trickle down to the rest of the page, like in the diagram. 我在这个跨越8列的框内有一个措辞,然后是一个跨越4列的侧边栏。我希望侧边栏在页眉框内开始 ,然后将其溢出以向下滚动到页面的其余部分,如图所示。

What is currently happening is that the blue sidebar is instead increasing the height of the red box, so the red box is always equal to the height of the blue sidebar. 当前正在发生的事情是,蓝色边栏正在增加红色框的高度,因此红色框始终等于蓝色边栏的高度。

Instead, I want the red box to be equal to the height of the text within it, and let the blue box overflow the red box in to the page below. 相反,我希望红色框等于其中文本的高度,然后让蓝色框将红色框溢出到下面的页面中。

How is this possible, using Bootstrap's grid system and columns? 使用Bootstrap的网格系统和列怎么可能?

Two practical options. 两种实用的选择。

A) Set a height fixed height for the red bar A)设置红色条的高度固定高度

#redBar {
    height:250px;
    overflow:visible;
}

#blueBar {
    height:400px; /*or whatever*/
    float:right;
}

Then place the blue bar inside the red one. 然后将蓝色条放在红色条中。

Option B) use absolute position. 选项B)使用绝对位置。 this doesn't require the red bar to have a fixed height! 这不需要红色的条具有固定的高度!

#redBar{
    position:relative;
    overflow:visible;
    /*any other css you want*/
}


#blueBar {
    position:absolute;
    top: 0;
    right:0;

}

Feel free to ask any questions 随意问任何问题

I don't know Bootstrap, but pure CSS solution is like: 我不知道Bootstrap,但是纯CSS解决方案就像:

#sidebar {
    float: right;
}

#content_which_should_appear_under_sidebar {
    display: block;
    clear: right;
}

You basically just need to unclear the clear-fix of bootstrap for the particular div elements. 基本上,您只需要清除特定div元素的bootstrap明确修补程序即可。 Then you can use bootstraps pull-right class to float the sidebar and add a min-height to the header. 然后,您可以使用bootstraps pull-right类浮动边栏并向标题添加min-height

Here is an example. 这是一个例子。

 @import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'); body { color: white; } .red-header { background-color: red; margin-top: 0; min-height: 120px; padding: 10px; } .blue-sidebar { background-color: blue; margin-top: 20px; padding: 10px; font-weight: bold; font-size: 18px; } .unclear:after { clear: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div class="page-header red-header"> <div class="container-fluid unclear"> <div class="row unclear"> <div class="col-sm-8"> <h1>Big heading text goes in this div and is really big and stuff, as you can see</h1> </div> <div class="col-sm-4 pull-right blue-sidebar"> <p>More stuff goes inside this div and fills up as it trickles down the page hopefully this diagram was really helpful to explain my point. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Officiis fugit modi ea quod adipisci eum a, eius neque molestiae alias quos commodi maiores inventore voluptatem sit numquam error impedit placeat!</p> </div> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM