如何在页面内使两个并列的 div 中的表格居中?

huangapple go评论75阅读模式
英文:

How to center tables within page for two parallel div?

问题

以下是您要翻译的内容:

"I have 2 parallel div and within each div there are one or more stacked tables.

I'm trying to center the tables in the page, so for the table in left <div> should be moved to the right and table in right <div> should be moved to the left. I'd like a fixed horizontal separation between tables of 50px.

With my current CSS code I'm able to move the table in right <div> to the left, but I'm stuck in how to move the table in left <div> to the right. Thanks for any help.

Current output
如何在页面内使两个并列的 div 中的表格居中?

Output I'm looking for
如何在页面内使两个并列的 div 中的表格居中?

This is my code:

<!DOCTYPE html>
<html>
  <head>
    <title>Table Generator</title>
    <style>
      body {
        text-align: center;
      }

      #inputContainer {
        margin-bottom: 20px;
      }

      #tableContainer {
        display: flex;
        justify-content: space-between;
        margin: 0 -25px;
        padding: 40px;
        box-sizing: border-box;
      }

      .tableWrapperLeft {
        width: 50%;
        margin: 0 25px;
        border: 2px solid yellow;
      }

      .tableWrapperRight {
        width: 50%;
        margin: 0 25px;
        border: 2px solid yellow;
      }

      table {
        border-collapse: collapse;
        margin-bottom: 50px;
        width: 288px;
        height: 288px;
      }

      td {
        border: 1px dotted black;
        text-align: center;
      }

      td:first-child {
        border-left: 1px dotted black;
      }

      tr:first-child td {
        border-top: 1px dotted black;
      }

      .tableWrapperRight table {
        border: 2px solid green;
      }

      .tableWrapperLeft table {
        border: 2px solid green;
      }
    </style>
  </head>
  <body>
    <div id=&quot;tableContainer&quot;>
      <div class=&quot;tableWrapperLeft&quot;>
        <table>
          <tr>
            <td>V</td><td>W</td><td>E</td><td>G </td>
          </tr>
          <tr>
            <td>G</td><td>K</td><td>L</td><td>D </td>
          </tr>
          <tr>
            <td>W</td><td>F</td><td>U</td><td>N </td>
          </tr>
          <tr>
            <td>G</td><td>A</td><td>Q</td><td>L </td>
          </tr>
        </table>
      </div>
      <div class=&quot;tableWrapperRight&quot;>
        <table>
          <tr>
            <td>U</td><td>N</td><td>P</td><td>H </td>
          </tr>
          <tr>
            <td>Q</td><td>L</td><td>R</td><td>B </td>
          </tr>
          <tr>
            <td>J</td><td>B</td><td>I</td><td>N </td>
          </tr>
          <tr>
            <td>D</td><td>A</td><td>H</td><td>T </td>
          </tr>
        </table>
      </div>
    </div>
    </div>
  </body>
</html>

更新

使用@fatm提供的解决方案,如果每个<div>中有多个表格,左侧的<div>会出现问题,看起来像这样。

英文:

I have 2 parallel div and within each div there are one or more stacked tables.

I'm trying to center the tables in the page, so for the table in left &lt;div&gt; should be moved to the right and table in right &lt;div&gt; should be moved to the left. I'd like a fixed horizontal separation between tables of 50px.

With my current CSS code I´m able to move the table in right &lt;div&gt; to the left, but I'm stuck in how to move the table in left &lt;div&gt; to the right. Thanks for any help.

Current output
如何在页面内使两个并列的 div 中的表格居中?

Output I'm looking for
如何在页面内使两个并列的 div 中的表格居中?

This is my code:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Table Generator&lt;/title&gt;
&lt;style&gt;
body {
text-align: center;
}
#inputContainer {
margin-bottom: 20px;
}
#tableContainer {
display: flex;
justify-content: space-between;
margin: 0 -25px;
padding: 40px;
box-sizing: border-box;
}
.tableWrapperLeft {
width: 50%;
margin: 0 25px;
border: 2px solid yellow;
}
.tableWrapperRight {
width: 50%;
margin: 0 25px;
border: 2px solid yellow;
}
table {
border-collapse: collapse;
margin-bottom: 50px;
width: 288px;
height: 288px;
}
td {
border: 1px dotted black;
text-align: center;
}
td:first-child {
border-left: 1px dotted black;
}
tr:first-child td {
border-top: 1px dotted black;
}
.tableWrapperRight table {
border: 2px solid green;
}
.tableWrapperLeft table {
border: 2px solid green;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;tableContainer&quot;&gt;
&lt;div class=&quot;tableWrapperLeft&quot;&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;V&lt;/td&gt;&lt;td&gt;W&lt;/td&gt;&lt;td&gt;E&lt;/td&gt;&lt;td&gt;G &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;G&lt;/td&gt;&lt;td&gt;K&lt;/td&gt;&lt;td&gt;L&lt;/td&gt;&lt;td&gt;D &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;W&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;&lt;td&gt;U&lt;/td&gt;&lt;td&gt;N &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;G&lt;/td&gt;&lt;td&gt;A&lt;/td&gt;&lt;td&gt;Q&lt;/td&gt;&lt;td&gt;L &lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;tableWrapperRight&quot;&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;U&lt;/td&gt;&lt;td&gt;N&lt;/td&gt;&lt;td&gt;P&lt;/td&gt;&lt;td&gt;H &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Q&lt;/td&gt;&lt;td&gt;L&lt;/td&gt;&lt;td&gt;R&lt;/td&gt;&lt;td&gt;B &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;J&lt;/td&gt;&lt;td&gt;B&lt;/td&gt;&lt;td&gt;I&lt;/td&gt;&lt;td&gt;N &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;D&lt;/td&gt;&lt;td&gt;A&lt;/td&gt;&lt;td&gt;H&lt;/td&gt;&lt;td&gt;T &lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

UPDATE

With solution provided by @fatm, I have issues in left "div", if each "div" has more than one table and looks like this.

如何在页面内使两个并列的 div 中的表格居中?

答案1

得分: 1

body {
  text-align: center;
}

#inputContainer {
  margin-bottom: 20px;
}

#tableContainer {
  display: flex;
  justify-content: space-between;
  margin: 0 -25px;
  padding: 40px;
  box-sizing: border-box;
}

.tableWrapperLeft {
  width: 50%;
  margin: 0 25px;
  border: 2px solid yellow;
  display: flex;
  justify-content: end;
}

.tableWrapperRight {
  width: 50%;
  margin: 0 25px;
  border: 2px solid yellow;
}

table {
  border-collapse: collapse;
  margin-bottom: 50px;
  width: 288px;
  height: 288px;
}

td {
  border: 1px dotted black;
  text-align: center;
}

td:first-child {
  border-left: 1px dotted black;
}

tr:first-child td {
  border-top: 1px dotted black;
}

.tableWrapperRight table {
  border: 2px solid green;
}

.tableWrapperLeft table {
  border: 2px solid green;
}
<div id="tableContainer">
  <div class="tableWrapperLeft">
    <table>
      <tr>
        <td>V</td>
        <td>W</td>
        <td>E</td>
        <td>G </td>
      </tr>
      <tr>
        <td>G</td>
        <td>K</td>
        <td>L</td>
        <td>D </td>
      </tr>
      <tr>
        <td>W</td>
        <td>F</td>
        <td>U</td>
        <td>N </td>
      </tr>
      <tr>
        <td>G</td>
        <td>A</td>
        <td>Q</td>
        <td>L </td>
      </tr>
    </table>
  </div>
  <div class="tableWrapperRight">
    <table>
      <tr>
        <td>U</td>
        <td>N</td>
        <td>P</td>
        <td>H </td>
      </tr>
      <tr>
        <td>Q</td>
        <td>L</td>
        <td>R</td>
        <td>B </td>
      </tr>
      <tr>
        <td>J</td>
        <td>B</td>
        <td>I</td>
        <td>N </td>
      </tr>
      <tr>
        <td>D</td>
        <td>A</td>
        <td>H</td>
        <td>T </td>
      </tr>
    </table>
  </div>
</div>
</div>
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
text-align: center;
}
#inputContainer {
margin-bottom: 20px;
}
#tableContainer {
display: flex;
justify-content: space-between;
margin: 0 -25px;
padding: 40px;
box-sizing: border-box;
}
.tableWrapperLeft {
width: 50%;
margin: 0 25px;
border: 2px solid yellow;
display: flex;
justify-content: end;
}
}
.tableWrapperRight {
width: 50%;
margin: 0 25px;
border: 2px solid yellow;
}
table {
border-collapse: collapse;
margin-bottom: 50px;
width: 288px;
height: 288px;
}
td {
border: 1px dotted black;
text-align: center;
}
td:first-child {
border-left: 1px dotted black;
}
tr:first-child td {
border-top: 1px dotted black;
}
.tableWrapperRight table {
border: 2px solid green;
}
.tableWrapperLeft table {
border: 2px solid green;
}

<!-- language: lang-html -->

&lt;div id=&quot;tableContainer&quot;&gt;
&lt;div class=&quot;tableWrapperLeft&quot;&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;V&lt;/td&gt;
&lt;td&gt;W&lt;/td&gt;
&lt;td&gt;E&lt;/td&gt;
&lt;td&gt;G &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;G&lt;/td&gt;
&lt;td&gt;K&lt;/td&gt;
&lt;td&gt;L&lt;/td&gt;
&lt;td&gt;D &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;W&lt;/td&gt;
&lt;td&gt;F&lt;/td&gt;
&lt;td&gt;U&lt;/td&gt;
&lt;td&gt;N &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;G&lt;/td&gt;
&lt;td&gt;A&lt;/td&gt;
&lt;td&gt;Q&lt;/td&gt;
&lt;td&gt;L &lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;tableWrapperRight&quot;&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;U&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;P&lt;/td&gt;
&lt;td&gt;H &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Q&lt;/td&gt;
&lt;td&gt;L&lt;/td&gt;
&lt;td&gt;R&lt;/td&gt;
&lt;td&gt;B &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;J&lt;/td&gt;
&lt;td&gt;B&lt;/td&gt;
&lt;td&gt;I&lt;/td&gt;
&lt;td&gt;N &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;D&lt;/td&gt;
&lt;td&gt;A&lt;/td&gt;
&lt;td&gt;H&lt;/td&gt;
&lt;td&gt;T &lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

尝试更改特定divflex-direction,然后只需使用flex的起始或结束对齐来实现。

英文:

try to change the flex-direction of a certain div then just use flex start or end for alignment to able to achieve.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
text-align: center;
}
#inputContainer {
margin-bottom: 20px;
}
#tableContainer {
display: flex;
justify-content: space-between;
margin: 0 -25px;
padding: 40px;
box-sizing: border-box;
}
.tableWrapperLeft {
width: 50%;
margin: 0 25px;
border: 2px solid yellow;
display: flex;
flex-direction: column;
align-items: flex-end;
}
.tableWrapperRight {
width: 50%;
margin: 0 25px;
border: 2px solid yellow;
display: flex;
flex-direction: column;
align-items: flex-start;
}
table {
border-collapse: collapse;
margin-bottom: 50px;
width: 288px;
height: 288px;
}
td {
border: 1px dotted black;
text-align: center;
}
td:first-child {
border-left: 1px dotted black;
}
tr:first-child td {
border-top: 1px dotted black;
}
.tableWrapperRight table {
border: 2px solid green;
}
.tableWrapperLeft table {
border: 2px solid green;
}

<!-- language: lang-html -->

 &lt;body&gt;
&lt;div id=&quot;tableContainer&quot;&gt;
&lt;div class=&quot;tableWrapperLeft&quot;&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;V&lt;/td&gt;&lt;td&gt;W&lt;/td&gt;&lt;td&gt;E&lt;/td&gt;&lt;td&gt;G &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;G&lt;/td&gt;&lt;td&gt;K&lt;/td&gt;&lt;td&gt;L&lt;/td&gt;&lt;td&gt;D &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;W&lt;/td&gt;&lt;td&gt;F&lt;/td&gt;&lt;td&gt;U&lt;/td&gt;&lt;td&gt;N &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;G&lt;/td&gt;&lt;td&gt;A&lt;/td&gt;&lt;td&gt;Q&lt;/td&gt;&lt;td&gt;L &lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;tableWrapperRight&quot;&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;U&lt;/td&gt;&lt;td&gt;N&lt;/td&gt;&lt;td&gt;P&lt;/td&gt;&lt;td&gt;H &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Q&lt;/td&gt;&lt;td&gt;L&lt;/td&gt;&lt;td&gt;R&lt;/td&gt;&lt;td&gt;B &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;J&lt;/td&gt;&lt;td&gt;B&lt;/td&gt;&lt;td&gt;I&lt;/td&gt;&lt;td&gt;N &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;D&lt;/td&gt;&lt;td&gt;A&lt;/td&gt;&lt;td&gt;H&lt;/td&gt;&lt;td&gt;T &lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月20日 14:06:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76293749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定