我在不同的div中有多个元素,应该在某行对齐。我可以对其宽度进行硬编码(例如40%),但这看起来很丑。我还可以在页面加载时使用JS指定以像素为单位的精确值(但无法很好地调整大小)。
这是理想的结果(但是我必须进行硬编码width: 30%
才能使其工作):
标记:
<div class="panel-body">
<div class="input-group margin-bottom">
<span class="input-group-addon input-source-observer">sourceObserver_patchSize</span>
<input id="sourceObserver_patchSize" type="text" data-parsley-type="integer" min="1" max="1000000" class="form-control" data-parsley-trigger="change" placeholder="@Model.Default.PatchSize" initial-value="@Model.Custom.PatchSize" value="@Model.Custom.PatchSize.RenderIfNotEqual(Model.Default.PatchSize)">
<span class="input-group-btn">
<input type="button" class="btn btn-default" onclick="restoreInput(event)" value="Восстановить" />
<input type="button" class="btn btn-default" onclick="clearInput(event)" value="Очистить" />
</span>
</div>
<div class="input-group margin-bottom">
<span class="input-group-addon input-source-observer">sourceObserver_scanInterval</span>
<input id="sourceObserver_scanInterval" type="text" data-parsley-type="integer" min="1" max="1000000" class="form-control" data-parsley-trigger="change" placeholder="@Model.Default.ScanInterval" initial-value="@Model.Custom.ScanInterval" value="@Model.Custom.ScanInterval.RenderIfNotEqual(Model.Default.ScanInterval)">
<span class="input-group-btn">
<input type="button" class="btn btn-default" onclick="restoreInput(event)" value="Восстановить" />
<input type="button" class="btn btn-default" onclick="clearInput(event)" value="Очистить" />
</span>
</div>
<div class="input-group margin-bottom">
<span class="input-group-addon input-source-observer">sourceObserver_maxWaitingTime</span>
<input id="sourceObserver_maxWaitingTime" type="text" data-parsley-type="integer" min="1" max="1000000" class="form-control" data-parsley-trigger="change" placeholder="@Model.Default.MaxWaitingTime" initial-value="@Model.Custom.MaxWaitingTime" value="@Model.Custom.MaxWaitingTime.RenderIfNotEqual(Model.Default.MaxWaitingTime)">
<span class="input-group-btn">
<input type="button" class="btn btn-default" onclick="restoreInput(event)" value="Восстановить" />
<input type="button" class="btn btn-default" onclick="clearInput(event)" value="Очистить" />
</span>
</div>
<div class="input-group margin-bottom">
<span class="input-group-addon input-source-observer">sourceObserver_shutdownTimeout</span>
<input id="sourceObserver_shutdownTimeout" type="text" data-parsley-type="integer" min="1" max="1000000" class="form-control" data-parsley-trigger="change" placeholder="@Model.Default.ShutdownTimeout" initial-value="@Model.Custom.ShutdownTimeout" value="@Model.Custom.ShutdownTimeout.RenderIfNotEqual(Model.Default.ShutdownTimeout)">
<span class="input-group-btn">
<input type="button" class="btn btn-default" onclick="restoreInput(event)" value="Восстановить" />
<input type="button" class="btn btn-default" onclick="clearInput(event)" value="Очистить" />
</span>
</div>
<div class="input-group margin-bottom">
<span class="input-group-addon input-source-observer">sourceObserver_maximumBytes</span>
<input id="sourceObserver_maximumBytes" type="text" data-parsley-type="integer" min="1" max="1000000" class="form-control" data-parsley-trigger="change" placeholder="@Model.Default.MemoryLimit.MaximumBytes" initial-value="@Model.Custom.MemoryLimit.MaximumBytes" value="@Model.Custom.MemoryLimit.MaximumBytes.RenderIfNotEqual(Model.Default.MemoryLimit.MaximumBytes)">
<span class="input-group-btn">
<input type="button" class="btn btn-default" onclick="restoreInput(event)" value="Восстановить" />
<input type="button" class="btn btn-default" onclick="clearInput(event)" value="Очистить" />
</span>
</div>
<div class="input-group margin-bottom">
<span class="input-group-addon input-source-observer">sourceObserver_freeMemoryWaitMs</span>
<input id="sourceObserver_freeMemoryWaitMs" type="text" data-parsley-type="integer" min="1" max="1000000" class="form-control" data-parsley-trigger="change" placeholder="@Model.Default.MemoryLimit.FreeMemoryWaitMs" initial-value="@Model.Custom.MemoryLimit.FreeMemoryWaitMs" value="@Model.Custom.MemoryLimit.FreeMemoryWaitMs.RenderIfNotEqual(Model.Default.MemoryLimit.FreeMemoryWaitMs)">
<span class="input-group-btn">
<input type="button" class="btn btn-default" onclick="restoreInput(event)" value="Восстановить" />
<input type="button" class="btn btn-default" onclick="clearInput(event)" value="Очистить" />
</span>
</div>
<div class="input-group margin-bottom">
<span class="input-group-addon input-source-observer">sourceObserver_freeMemoryWaitCount</span>
<input id="sourceObserver_freeMemoryWaitCount" type="text" data-parsley-type="integer" min="1" max="1000000" class="form-control" data-parsley-trigger="change" placeholder="@Model.Default.MemoryLimit.FreeMemoryWaitCount" initial-value="@Model.Custom.MemoryLimit.FreeMemoryWaitCount" value="@Model.Custom.MemoryLimit.FreeMemoryWaitCount.RenderIfNotEqual(Model.Default.MemoryLimit.FreeMemoryWaitCount)">
<span class="input-group-btn">
<input type="button" class="btn btn-default" onclick="restoreInput(event)" value="Восстановить" />
<input type="button" class="btn btn-default" onclick="clearInput(event)" value="Очистить" />
</span>
</div>
</div>
就像添加以下CSS规则一样简单:
.input-group {
display: table-row;
}
例子:
.panel-body {
border-spacing: 0 1em;
}
.panel-body:before,
.panel-body:after {
display: none !important;
}
.input-group {
display: table-row !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="panel-body">
<div class="input-group margin-bottom">
<span class="input-group-addon input-source-observer">sourceObserver_patchSize</span>
<input id="sourceObserver_patchSize" type="text" data-parsley-type="integer" min="1" max="1000000" class="form-control" data-parsley-trigger="change" placeholder="@Model.Default.PatchSize" initial-value="@Model.Custom.PatchSize" value="@Model.Custom.PatchSize.RenderIfNotEqual(Model.Default.PatchSize)">
<span class="input-group-btn">
<input type="button" class="btn btn-default" onclick="restoreInput(event)" value="Восстановить" />
<input type="button" class="btn btn-default" onclick="clearInput(event)" value="Очистить" />
</span>
</div>
<div class="input-group margin-bottom">
<span class="input-group-addon input-source-observer">sourceObserver_scanInterval</span>
<input id="sourceObserver_scanInterval" type="text" data-parsley-type="integer" min="1" max="1000000" class="form-control" data-parsley-trigger="change" placeholder="@Model.Default.ScanInterval" initial-value="@Model.Custom.ScanInterval" value="@Model.Custom.ScanInterval.RenderIfNotEqual(Model.Default.ScanInterval)">
<span class="input-group-btn">
<input type="button" class="btn btn-default" onclick="restoreInput(event)" value="Восстановить" />
<input type="button" class="btn btn-default" onclick="clearInput(event)" value="Очистить" />
</span>
</div>
<div class="input-group margin-bottom">
<span class="input-group-addon input-source-observer">sourceObserver_maxWaitingTime</span>
<input id="sourceObserver_maxWaitingTime" type="text" data-parsley-type="integer" min="1" max="1000000" class="form-control" data-parsley-trigger="change" placeholder="@Model.Default.MaxWaitingTime" initial-value="@Model.Custom.MaxWaitingTime" value="@Model.Custom.MaxWaitingTime.RenderIfNotEqual(Model.Default.MaxWaitingTime)">
<span class="input-group-btn">
<input type="button" class="btn btn-default" onclick="restoreInput(event)" value="Восстановить" />
<input type="button" class="btn btn-default" onclick="clearInput(event)" value="Очистить" />
</span>
</div>
<div class="input-group margin-bottom">
<span class="input-group-addon input-source-observer">sourceObserver_shutdownTimeout</span>
<input id="sourceObserver_shutdownTimeout" type="text" data-parsley-type="integer" min="1" max="1000000" class="form-control" data-parsley-trigger="change" placeholder="@Model.Default.ShutdownTimeout" initial-value="@Model.Custom.ShutdownTimeout" value="@Model.Custom.ShutdownTimeout.RenderIfNotEqual(Model.Default.ShutdownTimeout)">
<span class="input-group-btn">
<input type="button" class="btn btn-default" onclick="restoreInput(event)" value="Восстановить" />
<input type="button" class="btn btn-default" onclick="clearInput(event)" value="Очистить" />
</span>
</div>
<div class="input-group margin-bottom">
<span class="input-group-addon input-source-observer">sourceObserver_maximumBytes</span>
<input id="sourceObserver_maximumBytes" type="text" data-parsley-type="integer" min="1" max="1000000" class="form-control" data-parsley-trigger="change" placeholder="@Model.Default.MemoryLimit.MaximumBytes" initial-value="@Model.Custom.MemoryLimit.MaximumBytes" value="@Model.Custom.MemoryLimit.MaximumBytes.RenderIfNotEqual(Model.Default.MemoryLimit.MaximumBytes)">
<span class="input-group-btn">
<input type="button" class="btn btn-default" onclick="restoreInput(event)" value="Восстановить" />
<input type="button" class="btn btn-default" onclick="clearInput(event)" value="Очистить" />
</span>
</div>
<div class="input-group margin-bottom">
<span class="input-group-addon input-source-observer">sourceObserver_freeMemoryWaitMs</span>
<input id="sourceObserver_freeMemoryWaitMs" type="text" data-parsley-type="integer" min="1" max="1000000" class="form-control" data-parsley-trigger="change" placeholder="@Model.Default.MemoryLimit.FreeMemoryWaitMs" initial-value="@Model.Custom.MemoryLimit.FreeMemoryWaitMs" value="@Model.Custom.MemoryLimit.FreeMemoryWaitMs.RenderIfNotEqual(Model.Default.MemoryLimit.FreeMemoryWaitMs)">
<span class="input-group-btn">
<input type="button" class="btn btn-default" onclick="restoreInput(event)" value="Восстановить" />
<input type="button" class="btn btn-default" onclick="clearInput(event)" value="Очистить" />
</span>
</div>
<div class="input-group margin-bottom">
<span class="input-group-addon input-source-observer">sourceObserver_freeMemoryWaitCount</span>
<input id="sourceObserver_freeMemoryWaitCount" type="text" data-parsley-type="integer" min="1" max="1000000" class="form-control" data-parsley-trigger="change" placeholder="@Model.Default.MemoryLimit.FreeMemoryWaitCount" initial-value="@Model.Custom.MemoryLimit.FreeMemoryWaitCount" value="@Model.Custom.MemoryLimit.FreeMemoryWaitCount.RenderIfNotEqual(Model.Default.MemoryLimit.FreeMemoryWaitCount)">
<span class="input-group-btn">
<input type="button" class="btn btn-default" onclick="restoreInput(event)" value="Восстановить" />
<input type="button" class="btn btn-default" onclick="clearInput(event)" value="Очистить" />
</span>
</div>
</div>
出于某种原因,Bootstrap会添加伪元素:before
和:after
on .panel-body
,这会在border-spacing
使用时导致额外的空间。简单地隐藏它们似乎可以解决问题。
同样!important
,上面示例中的任何内容仅对于示例在此处起作用是必需的。如果您在Bootstrap之后添加样式表,则不需要它们。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句