css - 并排DIV不对齐

标签 css

我正在尝试创建几个并排放置的 DIV,每个占据容纳它们的 div 宽度的一半。并排使用 float: left 工作正常,但我的元素没有正确排列。我做了一个 jsfiddle 来告诉你我的意思。 http://jsfiddle.net/kzFqU/

如您所见,它看起来不错,但如果您将结果设置得非常宽,输入的正确对齐方式就会变得很糟糕。

<div class="container-narrow">
<div class="form_field">
    <label>Full Width</label>
    <input type="text" name="what">
</div>
<div class="form_field halfwidth">
    <label>Half Width</label>
    <input type="text" name="what">
</div>
<div class="form_field halfwidth">
    <label>Also Half Width</label>
    <input type="text" name="what">
</div>
<div class="form_field">
    <label>Full Width</label>
    <input type="text" name="what">
</div>

和CSS

body {
padding-top: 20px;
padding-bottom: 40px;
padding-left: 20px;
padding-right: 20px;
background:whitesmoke;
}

.container-narrow {
    margin: 0 auto;
    background:white;
    border-radius: 10px;
    padding:10px;
}

.form_field {
    padding: 10px;
}

.form_field input {
    width:100%;
}

.form_field label {
    margin-bottom:0px;
}

.form_field input {
    clear:both;
}

.halfwidth {
    width: 45%;
    float: left;
}

最佳答案

尝试清除半 Angular 元素后的 float ,例如:

.form_field:before,
.halfwidth:before {
    clear: both;
    display: block;
    content: "\0020";
    visibility: hidden;
}

上面的解决方案是解决问题的一些 self 清除的好方法!查看demo

关于css - 并排DIV不对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15673750/

相关文章:

html - 如何阻止空的 html 元素变得无量纲?

html - 使用 CSS 对齐图片

HTML 电子邮件 : How do I remove spacing between 2 tables? Gmail 特别

javascript - 剪刀石头布游戏的循环计数(潜在的 jquery 问题)

CSS如何定位2个属性?

css - 为什么 ID 选择器利用类选择器?

css - 如何整合多个CSS文件

css - 如何使用 Spring MVC 和 Tiles 配置 CSS

css - Magento CSS 属性背景图像 :url issue with data:image/svg+xml;base64

html - 谷歌网络字体变音符号显示来自其他字体