我有一个<div>
需要根据其中的内容自动调整的功能。我怎样才能做到这一点?现在,我的内容来自<div>
我用于div的类如下
box-centerside {
background:url("../images/greybox-center-bg1.jpg") repeat-x scroll center top transparent;
float:left;
height:100px;
width:260px;
}
我有一个<div>
需要根据其中的内容自动调整的功能。我怎样才能做到这一点?现在,我的内容来自<div>
我用于div的类如下
box-centerside {
background:url("../images/greybox-center-bg1.jpg") repeat-x scroll center top transparent;
float:left;
height:100px;
width:260px;
}
Answers:
尝试使用以下标记,而不是直接指定高度:
.box-centerside {
background: url("../images/greybox-center-bg1.jpg") repeat-x scroll center top transparent;
float: left;
min-height: 100px;
width: 260px;
}
<div class="box-centerside">
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
This is sample content<br>
</div>
只需输入“最小高度:XXX;” 而“溢出:隐藏”;&你将摆脱这个问题
min-height: 100px;
overflow: hidden;
overflow: hidden;
让我看到更多东西?(在我的情况下,如果没有它,包含的块将缩小到其高度,并且内容在底部被截断。)
我已经为我的项目做了一些自动调整高度的工作,我想我找到了解决方案
[CSS]
overflow: auto;
overflow-x: hidden;
overflow-y: hidden;
这可以附加到css文件中的质数div
(例如,warpper,但不附加body
或html
导致页面无法向下滚动),并由其他子类继承并写入它们的overflow: inherit;
属性。
注意:奇怪的是,我的netbeans 7.2.1 IDE突出显示overflow: inherit;
为,Unexpected value token inherit
但是所有现代浏览器都很好地读取了此属性。
该解决方案非常适合
我没有在这些浏览器的早期版本上对其进行测试。如果有人检查它,那就太好了。
只需使用以下
height:auto;
height
将暗示auto
。
不要设定height
。使用min-height
和max-height
代替。
简单的答案是使用overflow: hidden
和min-height: x(any) px
将自动调整div的大小。
该height: auto
会无法正常工作。
<div>
应该会自动展开。我想问题是您使用的是fixed height:100px;
,请尝试将其替换为min-height:100px;
.
或#
之前box-centerside
要匹配类或ID。目前,您的代码与节点名的元素匹配box-centerside
-其中不存在任何元素。
我通过将div中元素的位置设置为相对来解决了我的问题。
height:59.55%; //首先指定您的身高,然后使溢出自动溢出:自动;
对我来说,以下所有CSS都可以正常工作:
float: left; width: 800px;
在将元素放入CSS文件之前,请先检查chrome以尝试使用chrome。
这些大多数都是很好的答案,但我认为它们遗漏了Web开发的一个不可避免的方面,即正在进行的页面更新。如果您想过来向该div添加内容怎么办?您是否应该总是来调整div的最小高度?好吧,在尝试回答这两个问题时,我尝试了以下代码:
.box-centerside {
background: url("../images/greybox-center-bg1.jpg") repeat-x scroll center top transparent;
float: left;
height: auto; /* adjusts height container element according to content */
width: 260px;
}
此外,仅出于某种奖励,如果您在此div周围具有诸如的属性position: relative;
,则它们将因此堆叠在此div上,因为它具有属性float: left;
为了避免这种堆叠,我尝试了以下代码:
.parent {
display: grid;
grid-template-columns: repeat(3, 1fr); /* number depends on how many columns you want for a particular screen width. */
height: auto; /* adjusts height container element according to content */
}
.box-centerside {
background: url("../images/greybox-center-bg1.jpg") repeat-x scroll center top transparent;
height: auto; /* adjusts height container element according to content */
width: 100%;
}
.sibling 1 {
/* Code here */
}
.sibling 2 {
/* Code here */
}
我的意见是,我发现这种网格方法可以显示更多适合响应式网站的内容。否则,有许多方法可以实现相同的目标。但是编程的一些重要目标是使编码更简单,更易读和更容易理解。