【发布时间】:2013-12-29 05:54:22
【问题描述】:
我有一个固定宽度的 div,其中包含三个字段集,ID 分别为“first”、“second”、“third”:
现在我希望它们按如下方式定位:第一个字段集在左侧,第二个在中心,第三个在右侧。我希望他们保持在他们的位置,即使其中一个或所有其他人被隐藏,而不使用绝对或固定位置。
在我向您展示我已经尝试过的内容之前,我会给您一些代码。 HTML优先:
<div class="container_options">
<fieldset class="fieldset_first" id="first">
<legend>Konfiguration des offenen Endes</legend>
</fieldset>
<fieldset class="fieldset_second" id="second">
<legend>Verfügbare Optionen für Ihr Kabel</legend>
</fieldset>
<fieldset class="fieldset_third" id="third">
<legend>Konfiguration des offenen Endes</legend>
</fieldset>
</div>
现在的 CSS:
div.container_options {
margin: 15px;
clear: both;
width: 980px;
}
fieldset.fieldset_first{
width: 300px;
border: 1px solid black;
border-radius: 5px;
margin-left: 0;
margin-right: auto;
float: left;
margin-top: 10px;
}
.fieldset_first legend {
font-weight: bold;
}
.fieldset_third{
width: 300px;
border: 1px solid black;
border-radius: 5px;
margin-left: auto;
margin-right: 0;
float: right;
margin-top: 10px;
}
.fieldset_third legend {
font-weight: bold;
}
.fieldset_second {
width: 300px;
border: 1px solid black;
border-radius: 5px;
margin: auto;
float: right;
float: left;
margin-top: 10px;
}
您可以在以下JSFiddle 中测试此代码,并可以隐藏/显示字段集以查看它们的行为方式。
现在我将解决问题:
当所有三个字段集都显示出来时,一切都很好。
如果我隐藏正确的,一切都很好。
如果我隐藏左边的,中间的跳到左边,不关心margin: auto;
我在 Stackoverflow 上发现了一些类似的问题:
CSS: Positioning components using margins
div won't center with margin: 0 auto;
CSS Positioning Margin Trouble
我尝试了对他们有用的东西,但它们似乎对我不起作用。例如,一个答案说我不应该在尝试使用边距定位时使用float 属性。我创建了另一个 JSFiddle 我试图这样做,但结果不是我想要的。我做错了什么?
【问题讨论】:
标签: javascript html css