html - 使这些文本框对齐的正确 css 宽度是多少?

标签 html css

我在 HTML 表格中有两行。这是它的简化 View :

#topTextbox {
  width: 98%;
}

#bottomTextbox {
  width: 693px;
}
<table>
  <tr>
    <td>
      <input type=text id=topTextbox />
    </td>
  </tr>
  <tr>
    <td>
      <select>
        <option value="0"></option>
        <option value="saab">Saab</option>
        <option value="mercedes">Mercedes</option>
      </select>
      <input type=text id=bottomTextbox />
    </td>
  </tr>
</table>

第一行有一个长文本框,第二行有一个下拉列表和一个文本框。我试图得到它,所以它们以相同的总宽度排列。问题是,随着新数据进入下拉列表,宽度会发生变化,所以我试图找出底部文本框的正确 css,以便它在右侧与顶部文本框对齐。

这是它在 Firefox 中的样子: enter image description here

这是它在 IE 中的样子: enter image description here

所以我今天遇到一个问题,即它们不会跨浏览器排列,而且随着元素被添加到下拉列表中,情况会变得更糟(因为底部的宽度是固定的)。

无论整个表格有多大以及向下拉列表中添加了新元素,保持这些文本框右对齐的正确方法是什么?

最佳答案

以下方法涉及 CSS3 box-sizingcalc() .它在 IE9+ 和所有现代浏览器上运行良好。

table {
  width: 100%;
}
input,
select {
  box-sizing: border-box;
  display: inline-block;
}
#topTextbox {
  width: 100%;
}
select {
  width: calc(30% - 4px);
  /* why 4px? http://stackoverflow.com/q/5078239/483779 */
}
#bottomTextbox {
  width: 70%;
}
<table>
  <tr>
    <td>
      <input type="text" id="topTextbox" />
    </td>
  </tr>
  <tr>
    <td>
      <select>
        <option value="0"></option>
        <option value="saab">Saab</option>
        <option value="mercedes">Mercedes</option>
      </select>
      <input type="text" id="bottomTextbox" />
    </td>
  </tr>
</table>

关于html - 使这些文本框对齐的正确 css 宽度是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29367056/

相关文章:

html - 创建自定义搜索框

css - 使占位符 div 可动态重叠

css - DIV 中的重叠图像

html - 在 flexbox Divs 中添加可滚动的 Div

jquery - 使用 Jquery 的类名将焦点设置为链接

html - 绝对居中文本但左对齐

javascript - 使用 $.ajax() 获取动态 <td> 值

css - durandal.js - 是否可以通过 View 按需加载 View 特定的 css?

css - 如何将第三个元素向左浮动?

css - 当一个 div 标签低于另一个时,如何使 div 标签扩展到内容