html - HTML 中的文本字段对齐错误

标签 html css

谁能告诉我为什么第一个文本框在下面的 HTML/CSS 中没有对齐? (JSFiddle here)。

<div class="gift_code_fields">
    <div class="gift_code_line">
        <div class="gift_code_line_header">
            <label for="1">1</label><span>:</span>
        </div>
        <input class="gift_code" id="gift_code_1" name="gift_code_1" type="text">

    </div>
    <div class="gift_code_line">
        <div class="gift_code_line_header">
            <label>2</label>
            <span>:</span>
        </div>
        <input type="text" class="gift_code">
    </div>
    <div class="gift_code_line">
        <div class="gift_code_line_header">
            <label>3</label>
            <span>:</span>
        </div>
        <input type="text" class="gift_code">
    </div>
    <div class="gift_code_line">
        <div class="gift_code_line_header">
            <label>4</label>
            <span>:</span>
        </div>
        <input type="text" class="gift_code">
    </div>
</div>​
.gift_label{
  width: 30px;
  display: inline-block;
  font-weight: bold;
}

.gift_code_section{
  margin-bottom: 30px;
}

.gift_code_choose{
  width: 100px;
  float: left;
}

.gift_code_fields{
  width: 300px;
  display: inline-block;
}

.add_gift_code{
  cursor: pointer;
}

.gift_code {
  display: inline;
}

.gift_code_line_header{
  display: inline;
  width: 20px;
  margin-right: 10px;
}​

最佳答案

那是因为你把<span>对于除第一个以外的所有内容的新行冒号:

<label for="1">1</label><span>:</span>

<label for="2">2</label>
<span>:</span> 

这是因为,根据 HTML 规范,任何空白字符序列都被视为单个空格。

关于html - HTML 中的文本字段对齐错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11158603/

相关文章:

javascript - 使用 :focus-within 时更新 aria-expanded 属性的最佳方法

javascript - 更新地址字段时无需重新加载页面即可导航到新内容

html - 图像未出现在显示 :inline-block 的同一行上

c++ - 使用 C++ 将 HTML 转换为 XML

html - Bootstrap 网格添加在左侧和右侧有边距?

css - 如何使图像出现在链接悬停上

jquery - 获得最高的div

当用户尝试离开页面时显示的 Javascript 弹出窗口不会用按钮关闭

html - CSS 和定义列表 - 选择特定的定义描述

javascript - 将测试选项与相应的 `radio` 按钮对齐