CSS:显示:内联块和定位:绝对
请考虑以下代码:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<style type="text/css">
.section {
display: block;
width: 200px;
border: 1px dashed blue;
margin-bottom: 10px;
}
.element-left,
.element-right-a,
.element-right-b {
display: inline-block;
background-color: #ccc;
vertical-align: top;
}
.element-right-a,
.element-right-b {
max-width: 100px;
}
.element-right-b {
position: absolute;
left: 100px;
}
</style>
<title>test</title>
</head>
<body>
<div class="section">
<span class="element-left">some text</span>
<span class="element-right-a">some text</span>
</div>
<div class="section">
<span class="element-left">some text</span>
<span class="element-right-a">some more text to force line wrapping</span>
</div>
<div class="section">
<span class="element-left">some text</span>
<span class="element-right-b">some text</span>
</div>
<div class="section">
<span class="element-left">some text</span>
<span class="element-right-b">some more text to force line wrapping</span>
</div>
<div class="section">
<span class="element-left">some text</span>
<span class="element-right-b">some more text to force line wrapping</span>
</div>
</body>
</html>
具有绝对定位的元素显然使包含框“忘记”了他需要的高度。
我需要“部分”框中的绝对定位,是否有其他解决方案?
编辑
使用表格并不是真正的选择,我需要某种“多级”/“嵌套”布局,其中第二列始终位于同一位置:(
| some text in first column | some text in 2nd column | some indented text | 2nd column | also indented | 2nd col | even more indent | 2nd column with a lot of text that | makes it wrap | text | ... | blah blah | ...
当然没有“|” “s)
Please consider the following code:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<style type="text/css">
.section {
display: block;
width: 200px;
border: 1px dashed blue;
margin-bottom: 10px;
}
.element-left,
.element-right-a,
.element-right-b {
display: inline-block;
background-color: #ccc;
vertical-align: top;
}
.element-right-a,
.element-right-b {
max-width: 100px;
}
.element-right-b {
position: absolute;
left: 100px;
}
</style>
<title>test</title>
</head>
<body>
<div class="section">
<span class="element-left">some text</span>
<span class="element-right-a">some text</span>
</div>
<div class="section">
<span class="element-left">some text</span>
<span class="element-right-a">some more text to force line wrapping</span>
</div>
<div class="section">
<span class="element-left">some text</span>
<span class="element-right-b">some text</span>
</div>
<div class="section">
<span class="element-left">some text</span>
<span class="element-right-b">some more text to force line wrapping</span>
</div>
<div class="section">
<span class="element-left">some text</span>
<span class="element-right-b">some more text to force line wrapping</span>
</div>
</body>
</html>
The element with absolute positioning apparantly makes the containing box "forget" which height he needs.
I need the absolute positioning inside the "section" box, is there another solution for this?
edit
Using tables is not really an option, I need some sort of "multi-level"/"nested" layout, where the second col is always on the same position:
| some text in first column | some text in 2nd column | some indented text | 2nd column | also indented | 2nd col | even more indent | 2nd column with a lot of text that | makes it wrap | text | ... | blah blah | ...
(of course whithout the "|"s)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您使用
position:absolute;
时,该元素将从正常页面流中取出。因此它不再影响其容器元素的布局。所以容器元素不考虑它的高度,所以如果没有其他东西来设置高度,那么容器的高度将为零。此外,设置
display:inline-block;
对于position:absolute;
的元素没有任何意义。同样,这是因为绝对定位使元素脱离了页面流。这与inline-block
不一致,后者的存在只是为了影响元素如何适应页面流。所有position:absolute;
元素都会自动被视为display:block
,因为这是绝对定位的唯一逻辑显示模式。如果您需要绝对定位,那么解决高度问题的唯一方法是设置容器框的高度。
不过,我怀疑你可以不用绝对定位。
看起来您想要做的是将每个块中的第二个
定位到块中的固定位置,以便它们对齐。
这是一个经典的 CSS 问题。在“糟糕的旧时代”,网页设计师会使用表格来完成此操作,表格单元格的宽度固定。这显然不是当今网页设计师的答案,但却引起了很多问题。
使用 CSS 有多种方法可以做到这一点。
最简单的方法是将两个
元素设置为
display:inline-block;
,并为它们指定固定宽度。例如:
使用以下 CSS:
[编辑]问题更新后
以下是我如何实现您现在所要求的:
使用以下 CSS:
少量的额外标记,但它确实实现了你想要的效果。
When you use
position:absolute;
, the element is taken out of the normal page flow. Therefore it no longer affects the layout of its container element. So the container element does not take into account its height, so if there's nothing else to set the height, then the container will be zero height.Additionally, setting
display:inline-block;
does not make any sense for an element that isposition:absolute;
. Again, this is because absolute positioning takes the element out of the page flow. This is at odds withinline-block
, which only exists to affect how the element fits into the page flow. All elements that areposition:absolute;
are automatically treated asdisplay:block
, since that's the only logical display mode for absolute positioning.If you need absolute positioning, then the only solution to your height problem is to set the height of the container box.
However, I suspect that you could do without the absolute positioning.
It looks like what you're trying to do is position the second
<span>
in each block to a fixed position in the block, so that they line up.This is a classic CSS problem. In the "bad-old-days", web designers would have done it using a table, with fixed widths on the table cells. This obviously isn't the answer for today's web designers, but it is something that causes a lot of questions.
There are a number of ways to do it using CSS.
The easiest is to set both the
<span>
elements todisplay:inline-block;
, and give them both a fixed width.eg:
with the following CSS:
[EDIT]after question has been updated
Here's how I would achieve what you're asking now:
with the following CSS:
A small amount of extra markup, but it does achieve the effect you want.
不。
您可以绝对定位,然后在节框中使用
visible: none
保存该元素的副本,但绝对定位根据定义使其成为“浮动”元素,不会与其上方的元素交互。假设您的页面布局是固定的,您可以使用
padding-left: #px;
来实现您的目标,因为我认为相对定位不是您想要的。或者,您可以使用
display: table-*
强制其保留更严格的形式,而不影响文档结构,如下所示 此处但是,根据您是否希望单元格流动,您可能需要将
.section
div 更改为display: table-row
如果您不喜欢预定的宽度设置并希望单独的 .section 对齐。No.
You could position absolutely then have a copy of the element inside the section box with
visible: none
but absolute positioning by definition makes it a "floating" element that doesn't interact with elements above it.Assuming your page layout is fixed you could use
padding-left: #px;
to achieve your goal, as I don't think relative positioning is what you want.Alternatively, you could use
display: table-*
to force it to retain a stricter form without affecting the document structure as shown hereHowever depending on whether you want the cells to be fluid you may need to alter the
.section
divs intodisplay: table-row
if you don't like a predetermined width setup and want the separate .section's to line up.事实上,这可以通过 div 轻松简单地完成。您只需在内联或块显示 div 内放置一个带有position:relative 的 div 即可。将其宽度和高度设置为与包含的 div 相同。然后你会发现你可以将另一个 div 绝对放置在其中。
This can in fact be done easily and simply with divs. You just need to place a div with position:relative inside the inline or block display div. Set its width and height to the same as the containing div. You will then find you can position another div absolutely inside it.