2 回答
TA贡献1875条经验 获得超5个赞
当然,使用 flexbox 会更容易:)
如果您可以使用表格,那么您可以执行以下操作:
/* Added */
#wrapper{
display: table;
width: 100%;
}
.a {
display: table-cell; /* Added */
vertical-align: top; /* Added */
/* display: inline-block; */
background-color: yellow;
width: 50%; /* Changed */
border: 1px solid black;
}
.b {
display: table-cell; /* Added */
vertical-align: top; /* Added */
/* display: inline-block; */
background-color: red;
width: 50%; /* Changed */
border: 1px solid black;
}
<div id="wrapper">
<div class='a'>Element A</div>
<div class='b'>Element B: Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. </div>
<div>
如果您需要不同的列,您可以添加一个空列并相应地设置不同的宽度:
#wrapper{
display: table;
width: 100%;
}
.a {
display: table-cell;
vertical-align: top;
background-color: yellow;
width: 45%; /* Changed */
border: 1px solid black;
}
.b {
display: table-cell;
vertical-align: top;
background-color: red;
width: 45%; /* Changed */
border: 1px solid black;
}
/*added*/
.gap{
display: table-cell;
vertical-align: top;
width: 10%; /* which is 100 - 2* 45 */
}
<div id="wrapper">
<div class='a'>Element A</div>
<!-- ------------------------Added------------------------------ -->
<div class='gap'></div>
<!-- ----------------------------------------------------------- -->
<div class='b'>Element B: Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. </div>
<div>
如果您可以使用 CSS 变量(请参阅 comptability):
#wrapper{
display: table;
width: 100%;
--column-width: 49%; /* so you can set any value between 0 & 50% and the gap will fill the rest */
--number-of-column: 2; /* minimun 2 */
--number-of-gap: calc(var(--number-of-column) - 1);
--column-gap: calc((100% - var(--column-width) * var(--number-of-column)) / var(--number-of-gap));
}
.columns{
display: table-cell;
vertical-align: top;
}
.column-content{
width: var(--column-width);
border: 1px solid black;
}
.column-gap{
width: var(--column-gap, 0%); /* If we set just one column, --column-gap is undefined (because it divises by 0), so we need a default value, which is 0% because in this case of only one column, we have no gap column */
}
.a {
background-color: yellow;
}
.b {
background-color: red;
}
<div id="wrapper">
<div class='columns column-content a'>Element A</div>
<div class='columns column-gap'></div>
<div class='columns column-content b'>Element B: Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. </div>
<div>
TA贡献1880条经验 获得超4个赞
使用 Flex 怎么样?
HTML:
<div class="row-test">
<div class='a'>Element A</div>
<div class='b'>Element B: Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. </div>
</div>
CSS:
.a {
display: inline-block;
background-color: yellow;
width: 45%;
border: 1px solid black;
flex: 1;
}
.b {
display: inline-block;
background-color: red;
width: 45%;
border: 1px solid black;
flex: 1;
}
.row-test {
display: flex
}
很棒的弹性指南: https ://css-tricks.com/snippets/css/a-guide-to-flexbox/
弹性支持: https ://caniuse.com/#feat=flexbox
添加回答
举报