简体   繁体   English

为什么带有文本的滚动div超出其容器?

[英]Why does my scrolling div with text extend beyond its container?

Why does my scrolling div with text extend beyond its container vertically? 为什么带有文本的滚动div垂直超出其容器?

http://www.blakearchive.org/blake/public/exhibits/test.html http://www.blakearchive.org/blake/public/exhibits/test.html

I'm not sure what code to show here. 我不确定要在此处显示什么代码。 That is a div (right column--it circles around, that's why it's right, not left) with 100% height inside a container 'columns' div with 100% height, but former is extending beyond the latter. 这是一个div(右列,它绕圈,这就是为什么它正确,而不是左边)在容器“列” div中具有100%高度的原因,该div具有100%的高度,但是前者超出了后者。

Thanks. 谢谢。

Two things: You are giving it 100% height and padding. 两件事:给它100%的高度填充。

The first is an issue because there are buttons above it, and therefore it cannot occupy 100% of the height of the window without forcing the window to scroll. 第一个是问题,因为它上面有按钮,因此在不强制窗口滚动的情况下无法占据窗口高度的100%。

You can fix this by using calc: 您可以使用calc来解决此问题:

height: calc(100% - height_of_button_containerpx);

Note : pay particular attention to the syntax. 注意 :特别注意语法。 Note that there is no space inbetween numbers and units, but there is a space between each value and the minus sign. 请注意,有插图中数字和单位没有空间, 每个值和减号之间的空间。 This is important! 这个很重要!

Note this too : calc() is CSS3 and is unsupported in really old browsers. 还要注意这一点calc()是CSS3, 在真正的旧浏览器中不受支持。


The second: padding is applied in addition to height. 第二: 除了高度以外,还使用填充。 You are telling the div to have Qpx additional space surrounding its 100% height, and so, since the height cannot be greater than 100%, the window must scroll. 您要告诉div在其100%高度周围有Qpx额外的空间,因此,由于高度不能大于100%,因此窗口必须滚动。

This can be fixed quite easily with box-sizing: 使用box-sizing可以很容易地解决此问题:

#myDiv
{
    box-sizing: border-box;
    -moz-box-sizing: border-box;
}

calculate the height of the div with ID menuBarLine. 使用ID menuBarLine计算div的高度。 and then 接着

#columns .right {
position: relative;
float: right;
width: 30%;
height: 96%;//(total height of the parent container of #right div -height of the menuBarLine)
overflow-y: auto;
padding: 2%;
box-sizing: border-box;
-moz-box-sizing: border-box;
text-align: justify;
}

To fix the extended container, use display: table . 要修复扩展容器,请使用display: table Set a css property table to the container and table-cell for their children, as follow: 将css属性table设置为其子项的容器和table-cell ,如下所示:

#columns {
    display: table
}

#menuBarLine {
    display: table-cell
}

#galleria {
    display: table-cell
}

#right {
    display: table-cell;
    box-sizing: border-box
}

adding this display property will fix your problem. 添加此display属性将解决您的问题。

just keep in mind that anytime you have a split column, use display: table property as much as possible because display: table gives you flexibility in setting up the container width and height. 只要记住,只要您有一个拆分列,就尽可能使用display: table属性,因为display: table使您可以灵活地设置容器的宽度和高度。 If the container is like "grids", I will encourage to use display: table . 如果容器像“网格”,我将鼓励使用display: table

box-sizing: border-box changes the way width and height calculate. box-sizing: border-box更改了widthheight计算方式。 Now, width and height will include the padding of the container. 现在, widthheight将包括容器的padding Therefore, by using box-sizing: border-box , you will almost certainly get the value that you want. 因此,通过使用box-sizing: border-box ,您几乎可以肯定会获得所需的值。 I think this should be the default value of the css property width and height . 我认为这应该是CSS属性widthheight的默认值。 READ MORE 阅读更多

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

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