HTML/CSS : absolute positioning of elements within <fieldset>

标签 html css css-position

“绝对”到底是不是绝对的?

我正在尝试制作一个包含嵌套元素的输入表单,每个元素都包含其他元素,但它无法正确显示(根据屏幕标尺(和肉眼))。

HTML 是有效的,那么这是“嗯,它是绝对的,但仅相对于它包含的文件夹”或类似的情况?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head>
<body>
<form action="C:\temp\a_test.php" method="get"><div class="TGroupBox" id="GroupBox1">
<fieldset style="position: absolute; top:24px; left:24px; width: 449px; height: 473px;">
<legend>GroupBox1</legend>
<div class="TPanel" id="Panel1">
<fieldset style="position: absolute; top:64px; left:64px; width: 361px; height: 217px;">
<legend></legend>
<div class="TComboBox" id="ComboBox1" style="position: absolute; top:88px; left: 256px; width: 145px; height: 21px;">
  <select name="ComboBox1">
    <option value="- one -" selected="selected">- one -</option>
    <option value="- two -">- two -</option>
    <option value="- three -">- three -</option>
</select>
</div>
<div class="TGroupBox" id="GroupBox2">
<fieldset style="position: absolute; top:80px; left:88px; width: 145px; height: 177px;">
<legend>GroupBox2</legend>
<div class="TCheckBox" id="CheckBox1" style="position: absolute; top:112px; left: 104px; width: 97px; height: 17px;">CheckBox1<input type="checkbox" name="CheckBox1" value="CheckBox1Checked"></div>
<div class="TCheckBox" id="CheckBox2" style="position: absolute; top:152px; left: 112px; width: 97px; height: 17px;">CheckBox2<input type="checkbox" name="CheckBox2" value="CheckBox2Checked"checked="checked"></div>
</fieldset>
</div>
<div class="TRadioGroup" id="RadioGroup2">
<fieldset style="position: absolute; top:128px; left: 264px; width: 145px; height: 137px;"><legend>RadioGroup2</legend>

eins: <input type="radio" name="RadioGroup2" value="eins" checked><br>

zwei: <input type="radio" name="RadioGroup2" value="zwei"><br>

drei: <input type="radio" name="RadioGroup2" value="drei"><br>
</fieldset>
</div>
</fieldset>
</div>
<div class="TMemo" id="Memo1"><textarea name="Memo1" rows="8" cols="13" style="position: absolute; top:320px; left: 88px; width: 185px; height: 89px;">
</textarea>
</div>
<div class="TComboBox" id="ComboBox2" style="position: absolute; top:328px; left: 296px; width: 145px; height: 21px;">
  <select name="ComboBox2">
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d" selected="selected">d</option>
    <option value="e">e</option>
</select>
</div>
</fieldset>
</div>
<div class="TPanel" id="Panel2">
<fieldset style="position: absolute; top:32px; left:520px; width: 425px; height: 449px;">
<legend></legend>
<div class="TPanel" id="Panel3">
<fieldset style="position: absolute; top:64px; left:552px; width: 345px; height: 185px;">
<legend></legend>
<div class="TMemo" id="Memo2"><textarea name="Memo2" rows="8" cols="13" style="position: absolute; top:88px; left: 584px; width: 185px; height: 89px;">
You may wish to leave this memo emptyOr perpahaps give instructions aboout what should be written here</textarea>
</div>
<div class="TEdit" id="Edit1" style="position: absolute; top:200px; left: 600px; width: 121px; height: 21px;"><input type="text" name="Edit1"value="Insert text here"></div>
</fieldset>
</div>
<div class="TGroupBox" id="GroupBox3">
<fieldset style="position: absolute; top:272px; left:552px; width: 345px; height: 185px;">
<legend>GroupBox3</legend>
<div class="TPanel" id="Panel4">
<fieldset style="position: absolute; top:304px; left:584px; width: 177px; height: 137px;">
<legend></legend>
<div class="TRadioGroup" id="RadioGroup1">
<fieldset style="position: absolute; top:312px; left: 600px; width: 97px; height: 105px;"><legend>RadioGroup1</legend>

one: <input type="radio" name="RadioGroup1" value="one"><br>

two: <input type="radio" name="RadioGroup1" value="two" checked><br>

three: <input type="radio" name="RadioGroup1" value="three"><br>
</fieldset>
</div>
</fieldset>
</div>
<div class="TEdit" id="Edit2" style="position: absolute; top:320px; left: 776px; width: 105px; height: 21px;"><input type="text" name="Edit2"></div>
</fieldset>
</div>
</fieldset>
</div>
</form>
</body>
</html>

最佳答案

“绝对”是指绝对,相对于最近的“亲属”。相对父元素是具有 position:relative 的元素,如果没有,则为文档。

此外,使元素成为绝对元素具有某些含义,例如将其从文档流中删除。 DOM 的布局就像绝对元素不存在一样。这意味着容器不会环绕绝对元素,也不会将任何元素推开。

CSS 定位看起来像是黑魔法,直到发现它遵循这些非常简单的规则 - 无一异常(exception)! (这是一件好事)

编辑: 您在这里所做的具体操作取决于您要完成的任务。你为什么首先使用绝对定位?绝对定位具有最简单的规则,但也非常强大,强大的工具也更难,需要更多的手动工作,并且更有可能发生坏事。这就是权衡。例如,如果一个元素变大而没有移动其他元素以考虑到这一点,它们可能会重叠。绝对定位取决于您在每个元素上按比例保持值,以确保它们都正确排列。如果您想要一个环绕元素,则需要您始终更新 heightwidth 属性以使其足够大以呈现环绕效果。

要利用文档流提供的功能 - 换行、自动对齐等 - 您可以使用 float 、边距和填充。根据我对您在这里尝试执行的操作的有限理解,我将设置容器 fieldset 的填充以反射(reflect)子字段应放置的位置,并为每个字段留出边距。对于应该位于上一行而不是下一行右侧的字段,请使用 float:leftclear:left

关于HTML/CSS : absolute positioning of elements within <fieldset>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2970815/

相关文章:

php - 使 HTML 表格显示在一行中

html - 如何获得 HTML 属性文本的真实高度

CSS六 Angular 里,怎么样?

css - 如何居中左对齐文本?

CSS:带有 position:absolute 的 UL 显示带有 position:fixed 和 overflow:hidden 的外部容器

CSS 边框环绕子菜单和父选项卡。

html - 当提供非零无单位值(例如,底部)时,固定定位如何工作?

html - 为什么容器内的容器在 float 时会失去背景颜色?

html - 所需的输入属性正在影响 css 转换

css - 最后一个 child 不上课。最后一个类型也不起作用