html - 拉伸(stretch)一个 div 使其与其下方 2 个 div 的宽度之和相匹配

标签 html css

我试图让 3 个 div 对齐,使顶部的 div 拉伸(stretch)到底部 2 个 div 的宽度之和。 Attached Image of the expected div positioning 我尝试使用 display : table-row 作为 div。

<div id="main_div" style="display: table-row">
        <div id='col1' style="width: 300px; display: table-cell;">
            <div style="display: table-row">
                <div  style="width: 300px; display: table-cell;text-align :right;border-style: groove;">
                    <h1>This is heading 1</h1>
                </div>
            </div>
            <div style="display: table-row">            
                <div  style="width: 150px; display: table-cell;white-space: nowrap;border-style: groove; border-color: green;">
                    abc: <input type="text" name="def">
                </div>
                <div  style="width: 150px; display: table-cell;white-space: nowrap;border-style: groove; border-color: red">
                    def: <input type="text" name="ghi">
                </div>                                                      
            </div>
        </div>
    </div>

谢谢

最佳答案

已更新

您也可以通过 div 元素通过使用 css 的 float: leftdisplay: inline-block 属性来实现这一点。

.row:before,.row:after{
  content:"";
  display:table;
}
.row:after{
  clear:both;
}
.row{
  border: 1px solid #000;
  text-align:center;
}
.full{
  width:100%;
  float:left;
  display:inline-block;
  padding: 20px;
  box-sizing:border-box;
}
.half{
  width:50%;
  float:left;
  display:inline-block;
  padding: 20px;
  box-sizing:border-box;
  border-top:1px solid #000;
  border-right: 1px solid #000;
}
.half:last-of-type{
  border-right:0;
}
<div class="row">
  <div class="full">
    DIV 1
  </div>
  <div class="half">
    DIV2
  </div>
  <div class="half">
    DIV3
  </div>
</div>

实现这种布局的最佳方式是使用table。请检查以下代码:

table{
  border-spacing: 0;
  border-collapse: collapse;
  text-align: center;
}
td{
  border: 1px solid #000;
  padding:30px;
}
<table>
  <tbody>
    <tr>
      <td colspan="2">DIV 1</td>
    </tr>
    <tr>
      <td>DIV 2</td>
      <td>DIV 3</td>
    </tr>
  </tbody>
</table>

关于html - 拉伸(stretch)一个 div 使其与其下方 2 个 div 的宽度之和相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44120943/

相关文章:

javascript - 如何从其他单词列表中随机显示一个单词?

javascript - 自动将指标添加到 bootstrap carousel

javascript - 如何删除特定窗口大小的数据切换 ="modal"?

php - 如何并排设置2个按钮

javascript - 如何获得高度为 :auto? 的元素的高度

html - 如何删除 Bootstrap3 容器的背景?

javascript - 将 jQuery UI 可放置元素还原为原始 div

html - 内容可编辑的不可点击的上下文菜单

javascript - 使用下拉列表更改单独表格单元格中的多个值

html - 链接不适用于简单的下拉菜单