我怎样才能将两个div对齐?

时间:2016-09-13 02:22:13

标签: html css

如何对齐两个内联div以使其相邻边缘居中?

所以我希望它看起来像这样:

    div1div1 div2div2
        div1 div2
div1div1div1 div2div2

我尝试使用如下所示的内联块,但它只是将每一行居中。

.container {
  text-align: center;
}
.left-div {
  display: inline-block;
}
.right-div {
  display: inline-block;
}
<div class="container">
  <div class="left-div">div1div1</div>
  <div class="right-div">div2div2</div>
</div>
<div class="container">
  <div class="left-div">div1</div>
  <div class="right-div">div2</div>
</div>
<div class="container">
  <div class="left-div">div1div1div1</div>
  <div class="right-div">div2div2</div>
</div>

1 个答案:

答案 0 :(得分:1)

https://jsfiddle.net/fkfmh7md/更改div之间的距离,在填充中更改5px:0 5px到任何其他值

&#13;
&#13;
.container {
  display: table;
  table-layout: fixed;
  width: 100%;
}
.container div {
  display: table-cell;
  padding: 0 5px;
}
.container div.left {
  text-align: right;
}
&#13;
<div class="container">
  <div class="left">div1div1</div>
  <div>div2div2</div>
</div>
<div class="container">
  <div class="left">div1</div>
  <div>div2</div>
</div>
<div class="container">
  <div class="left">div1div1div1</div>
  <div>div2div2</div>
</div>
&#13;
&#13;
&#13;