如何设置 div 高度来填充可用空间
我有一个 3 列布局,列下方有一些详细信息。
您会注意到其中一列的高度大于其他列的高度。我希望其他两个 div 自动填充剩余空间(直到蓝色 div)。文本将动态加载,因此我需要它与任何更大的列(并且任意数量)一起使用。
这可以通过 HTML/CSS 完成还是必须使用一些 JavaScript?
HTML 代码(相关部分):
<div id="content">
<div id="iconsHolder">
<div id="info">
<div id="info_content">
<p><?php echo img('images/man.png'); ?></p>
<h2>Some guy over here</h2>
<p class="light justify">It doesn't matter what is being said at the moment. Nothing is said here.</p>
</div>
</div>
<div id="comp">
<div id="comp_content">
<p><?php echo img('images/computer.png'); ?></p>
<h2>Computer Development</h2>
<p class="light justify">Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit... Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...</p>
</div>
</div>
<div id="story">
<div id="story_content">
<p><?php echo img('images/library.png'); ?></p>
<h2>Story telling</h2>
<p class="light justify">This is another short story.</p>
</div>
</div>
</div>
<div id="details">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
CSS 代码(相关部分):
#content {
width: 60em;
margin: 0px auto;
}
#info,#comp,#story {
width: 18em;
float: left;
padding-left: 1em;
padding-right: 1em;
padding-top: 2em;
background-color: #DDD;
height: 100%;
}
#info_content,#comp_content,#story_content {
text-align: center;
}
#details {
clear: both;
background-color: #EEF;
padding: 1em;
}
I have a 3 column layout with some details below the columns.
You will notice that one of the column's height is greater than the others'. I'd like the other two divs to automatically fill the remaining space (up to the blue div). The text will be loaded dynamically so I need this to work with any column being larger (and by an arbitrary amount).
Can this be done with HTML/CSS or do I have to use some JavaScript?
HTML code (relevant part of it):
<div id="content">
<div id="iconsHolder">
<div id="info">
<div id="info_content">
<p><?php echo img('images/man.png'); ?></p>
<h2>Some guy over here</h2>
<p class="light justify">It doesn't matter what is being said at the moment. Nothing is said here.</p>
</div>
</div>
<div id="comp">
<div id="comp_content">
<p><?php echo img('images/computer.png'); ?></p>
<h2>Computer Development</h2>
<p class="light justify">Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit... Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...</p>
</div>
</div>
<div id="story">
<div id="story_content">
<p><?php echo img('images/library.png'); ?></p>
<h2>Story telling</h2>
<p class="light justify">This is another short story.</p>
</div>
</div>
</div>
<div id="details">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
CSS code (relevant part of it):
#content {
width: 60em;
margin: 0px auto;
}
#info,#comp,#story {
width: 18em;
float: left;
padding-left: 1em;
padding-right: 1em;
padding-top: 2em;
background-color: #DDD;
height: 100%;
}
#info_content,#comp_content,#story_content {
text-align: center;
}
#details {
clear: both;
background-color: #EEF;
padding: 1em;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
CSS 解决方案是使用背景颜色设置外部容器的样式,这称为假 (faux) bakcground。
像这样:
这个方法(至少在这种情况下)保证所有人的背景都是相同的。
The CSS solution is to style the outer container with the background color, this is called a fake (faux) bakcground.
Like this:
This method (in this case at least) guarantees the background is the same for all.
您可以将它们绝对定位在
div#iconsHolder
中(您将使其成为position:relative
),而不是浮动 div。无论如何,您都在设置宽度,只需设置每个的左侧div 到适当的偏移量并给出每个top:0
和bottom:0
CSS 规则。Instead of floating the divs you could absolutely position them within
div#iconsHolder
(which you will makeposition:relative
. You're setting widths anyway, just set the left of each div to the appropriate offset and give eachtop:0
andbottom:0
CSS rules.你所做的方法非常令人困惑,并且会让 CSS 更难以控制。如果将其转换为表格,您会得到更好的结果。
然后,您需要从 CSS 第 8 行删除
float:left;
并将此代码插入到底部。The approach you made is very confusing and would make it harder to control with CSS. If you convert it to a table you'd have better results.
Then, you need to remove
float:left;
from line 8 of your CSS and insert this code at the bottom.一个相当简单的解决方案是将容器 div iconholder 作为背景,如下所示:
将其添加到您的 css 中,它应该适用于所有浏览器。
A fairly simply solution is to make the container div iconsholder as the background like:
Add that to your css and it should work in all browsers.
除了向容器添加背景颜色之外,您还需要使容器占据子容器的空间。
您可以将
float:left
添加到容器,例如 理查德的回答 或如果你不想让容器浮动,你可以在后面添加一个空的“clear”div。它在语义上不太正确,但如果您不能或不希望容器浮动,这是另一种选择。
JsFiddle:
http://jsfiddle.net/gvJrJ/4/
In addition to adding the background color to the container, you'll also need to make the container take up the space of the children.
You can add
float:left
to the container like Richard's answer orIf you don't want to make the container float, you can add an empty "clear" div afterwards. It's less semantically correct, but if you can't or don't want the container floated it's another option.
JsFiddle:
http://jsfiddle.net/gvJrJ/4/
.. 可以使用
display: table-cell
通过纯 CSS 来完成.. 但没有 iE7 及以下支持:(这是一个使用包含两种方法(jQuery* 和 CSS)的解决方案,jQuery当然,对于其他浏览器也会做同样的事情,但是这个解决方案意味着大多数用户将获得一个纯 CSS 解决方案,只有 IE7 及以下版本需要 jQuery 来“增强”,
如果您想对所有浏览器使用 jQuery 解决方案,那么您不需要
table-cell
或table-row
显示属性,并且您需要从 float 属性中删除!ie7
hack -浮动还需要跨浏览器包含,因此您可以将overflow: hide
添加到#iconsHolder
div - 并保留zoom: 1;
如果您需要在 IE6 中工作,请放在适当的位置;)CSS:
HTML:
jQuery: (在条件注释内,因此仅适用于 IE7 及以下版本看到它)
添加: JSfiddle
注意:小提琴没有添加 jQuery,因为我没有添加 jQuery不知道如何将其放入小提琴的条件注释中)
.. it can be done with pure CSS using
display: table-cell
.. but there is no iE7 and below support :(Here's a solution using encompassing both methods (jQuery* and CSS), the jQuery would of course do the same thing for other browsers, but this solution means that most users would be getting a pure CSS solution with only IE7 and below needing the jQuery for "enhancement"
if you want to use the jQuery solution for all browsers then you would not need the
table-cell
ortable-row
display properties and you would need to remove the!ie7
hack from the float property - the floats would also need to be contained cross-browser so you could addoverflow: hidden
to the#iconsHolder
div - and just leavezoom: 1;
in place if you need this to work in IE6 ;)CSS:
HTML:
jQuery: (inside a conditional comment so only IE7 and below see it)
Added: JSfiddle
Note: the fiddle doesn't have the jQuery added in as I don't know how to put that in a conditional comment on the fiddle)
正如您所说,在 #iconHolder 中使用人造背景的问题是在鼠标悬停时突出显示每一列。
因此,我的建议如下:
1) 将各个人造列绝对定位在与原始列相同的位置
您将使用
z-index
属性来确保内容在顶部HTML
CSS
2) 将您的 onmouseover/onclick 事件附加到人造列和普通列
如果您需要有关 javascript 的更多详细信息,请询问,我只是不知道不过,在 jQuery 等价物上。
是的,我意识到这有点黑客,但它可以完成工作,而无需计算高度或任何东西。希望这有帮助!
As you said, the problem with using a faux background with your #iconHolder is highlighting each column on mouseover.
So here's what I suggest:
1) make individual faux columns absolutely positioned at the same location as the original column
You'll use the
z-index
property to ensure the content is on topThe HTML
The CSS
2) attach your onmouseover/onclick events to both the faux column AND the normal column
If you need more details on the javascript, just ask, I just wouldn't have any idea on the jQuery equivalent, though.
Yes, I realize that this is a little bit hackish, but it gets the job done without having to calculate the height or anything. Hope this helps!
目前,您的列已跨越文本所能到达的范围。如果我正确地回答了您的问题,您希望列背景颜色/图像扩展到可用数据之外。如果这就是您想要的,那么您必须创建假列(假列),因为这些列中的数据不会跨越整个高度。
我认为最简单的方法是将重复的背景图像应用于跨越布局整个高度的元素。这可能是某种封装您的列的包装器。在您的情况下,您可以为信息、比较和故事 div 应用一个分为三个色带(每个色带 18em,每个色带映射到覆盖特定列)的薄图像。该图像的宽度为 60em,并且会在 y 轴上重复,例如:
这假设图像与 css 文件位于同一文件夹中(不推荐,images/fake-columns.gif 更好,以便从以下位置引用图像)图像文件夹)。 Repeat-y 将向下重复小图像,覆盖内容 div 覆盖的整个高度。左上确保图像从左上角开始平铺,这正是您通常想要做的!内容之前的井号似乎从我上面的示例 css 中消失了。
Currently your columns are spanning as far as the text can go. If I get your question correctly, you would like the columns background color/ image to extend beyond available data. If that is what you want, then you have to create fake columns (Faux Columns) since your data in those columns do not span the whole height.
I think the easiest way is to apply a repeating backgroung image to an element WHICH spans the full height of your layount. This could be some sort of wrapper that encloses your columns. In your case you may apply a thin image that is divided into three color bands (18em each and each mapped to cover a particular column) for the info, comp and story divs. That image would be 60em in width and would repeat in the y- axis e.g:
This assumes the image is in the same folder as the css file (not recomended, images/fake-columns.gif is better so that the image is referenced from the image folder). Repeat-y will repeat the tiny image downwards covering the whole height the content div covers. Left top ensures the image starts tiling from the top left corner which is what you would normally want to do! The pound symbol before content seems to dissapear from my example css above.