将 Div/Layers 与字段集/图例结合使用
我正在构建一个表单。我使用具有绝对定位的 div 来确定表单的布局。我现在尝试在每组 div 周围放置一个表单集,问题是表单集创建一条线,但它不会延伸到所有 div - 我猜测 b/c 它们是绝对定位的。这是我的代码的示例:
<div>
<fieldset id="BasicInfo">
<legend>Basic Info.</legend>
<div id="first_name" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 13px; top: 82px">
First Name:</div>
<div id="first_name_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 136px; top: 82px">
</div>
<div id="last_name" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 313px; top: 82px">
Last Name:</div>
<div id="last_name_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 435px; top: 82px">
</div>
<div id="gender" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 613px; top: 82px">
Gender:</div>
<div id="gender_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 738px; top: 82px">
</div>
<div id="dob" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 14px; top: 120px">
Date of Birth:
</div>
<div id="dob_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 136px; top: 120px">
</div>
<div id="age" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 313px; top: 120px">
Age:</div>
<div id="age_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 435px; top: 120px">
</div>
<div id="intended_major" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 613px; top: 120px">
Intended Major:</div>
<div id="intended_major_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 738px; top: 120px">
</div>
<div id="email" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 13px; top: 162px">
Email:</div>
<div id="email_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 136px; top: 162px">
</div>
<div id="phone" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 313px; top: 162px; right: 561px;">
Phone:</div>
<div id="phone_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 435px; top: 162px">
</fieldset>
</div>
I am building a form. I've used divs with absolute positioning to determine the layout of my form. I'm now trying to put a formset around each group of divs, problem is that the formset creates a line but it doesn't stretch over all the divs - I'm guessing b/c they are positioned absolutely. Here is an example of my code:
<div>
<fieldset id="BasicInfo">
<legend>Basic Info.</legend>
<div id="first_name" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 13px; top: 82px">
First Name:</div>
<div id="first_name_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 136px; top: 82px">
</div>
<div id="last_name" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 313px; top: 82px">
Last Name:</div>
<div id="last_name_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 435px; top: 82px">
</div>
<div id="gender" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 613px; top: 82px">
Gender:</div>
<div id="gender_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 738px; top: 82px">
</div>
<div id="dob" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 14px; top: 120px">
Date of Birth:
</div>
<div id="dob_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 136px; top: 120px">
</div>
<div id="age" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 313px; top: 120px">
Age:</div>
<div id="age_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 435px; top: 120px">
</div>
<div id="intended_major" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 613px; top: 120px">
Intended Major:</div>
<div id="intended_major_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 738px; top: 120px">
</div>
<div id="email" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 13px; top: 162px">
Email:</div>
<div id="email_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 136px; top: 162px">
</div>
<div id="phone" style="position: absolute; width: 118px; height: 17px; z-index: 2; left: 313px; top: 162px; right: 561px;">
Phone:</div>
<div id="phone_edit" style="position: absolute; width: 172px; height: 17px; z-index: 3; left: 435px; top: 162px">
</fieldset>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你猜对了。绝对定位使元素脱离正常流,并将其定位在相对于设置了
position:relative
的容器父级的top:0;left:0
位置- 如果没有设置position:relative,它是相对于body的。如果您没有为父 div 定义高度,它将获得 0(或 1px 或默认字体大小高度 - 取决于您使用的浏览器)。我不认为需要绝对定位。尝试这样的事情:
我经常使用这种布局来构建复杂的表单,因为您可以轻松地包含验证器、提示和其他附加元素(如果需要)。
一旦你必须改变设计,编写内联 CSS 也会变得非常糟糕。使用类来代替。
You are guessing correct. Absolute positioning takes the element out of normal flow and it positions it on it's own level at
top:0;left:0
relative the container parent that has aposition:relative
set - if no position:relative is set it's relative to the body. If you don't define a height for the parent div it will get a 0 (or 1px or default font-size height - depending in which browser you look in). I don't see the need for absolute positioning.Try something like this:
I often use this kind of layout to structure complex forms since you can easly include validators, hints and other additional elements, if you need them.
Also writing inline css can get really nasty once you have to change the design. Use classes instead.