gpt4 book ai didi

CSS:显示:内联 block 和定位:绝对

转载 作者:技术小花猫 更新时间:2023-10-29 10:05:45 24 4
gpt4 key购买 nike

请考虑以下代码:

<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                       | ...

(当然没有“|”)

最佳答案

当您使用 position:absolute; 时, 元素从正常的页面流中取出。因此它不再影响其容器元素的布局。所以容器元素不考虑它的高度,所以如果没有别的设置高度,那么容器将是零高度。

此外,设置 display:inline-block;对于 position:absolute; 的元素没有任何意义.同样,这是因为绝对定位使元素脱离了页面流。这与 inline-block 不一致。 ,它的存在只是为了影响元素如何适应页面流。所有 position:absolute; 的元素被自动视为 display:block ,因为这是绝对定位的唯一逻辑显示模式。

如果您需要绝对定位,那么解决您的高度问题的唯一方法是设置容器框的高度。

但是,我怀疑您可以不用绝对定位。

看起来您要做的是定位第二个 <span>在每个 block 中移动到 block 中的固定位置,使它们对齐。

这是一个经典的 CSS 问题。在“糟糕的过去”,网页设计师会使用表格来完成,表格单元格的宽度是固定的。这显然不是当今网页设计师的答案,但却引发了很多问题。

有很多方法可以使用 CSS 实现。

最简单的方法是同时设置 <span>元素 display:inline-block; , 并给它们一个固定的宽度。

例如:

<div class='section'>
<span class="element-left">some text</span>
<span class="element-right">some text</span>
</div>

使用以下 CSS:

.section span  {display:inline-block;}
.element-left {width:200px;}
.element-right {width:150px;}

[编辑]问题更新后

以下是我将如何实现您现在的要求:

<div class='section'>
<span class="element-left"><span class='indent-0'>some text</span></span>
<span class="element-right">some text</span>
</div>
<div class='section'>
<span class="element-left"><span class='indent-1'>some text</span></span>
<span class="element-right">some text</span>
</div>
<div class='section'>
<span class="element-left"><span class='indent-2'>some text</span></span>
<span class="element-right">some text</span>
</div>

使用以下 CSS:

.section span  {display:inline-block;}
.element-left {width:200px;}
.indent-1 {padding:10px;}
.indent-2 {padding:20px;}
.element-right {width:150px;}

少量的额外标记,但它确实达到了你想要的效果。

关于CSS:显示:内联 block 和定位:绝对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5042467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com