使用 div 让两个表格并列排列

示例的 html 行数有点多,请见谅(不过至少可以直接拿去用浏览器打开,也还是很方便的)。

起因是这样的,我想将两个表格并列排列在同一行,最先的想法是像下面这么弄(弄两个 <div> 把 <table> 套进去,并设置各自 <div> 的 width 百分比)。

<html>
 <body>
  <h1 id="div2" align="center" style="margin-bottom:0px;padding-bottom:0px;font-size:1.625em;">so compare</h1><br>
  <div style="width:20%;hight:100%;float:left;">
  <table style="border-collapse:collapse;border:1px outset black;">
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">package</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">centos7.6</td>
    </tr>
    
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libc-2.17.so</td>
    </tr>
    
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libdl-2.17.so</td>
    </tr>
    
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libpthread-2.17.so</td>
    </tr>
    
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">pcre</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libpcre.so.1.2.0</td>
    </tr>
    </table>
  </div>
  <div style="width:80%;hight:100%;float:left;">
  <table style="border-collapse:collapse;border:1px outset black;">
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">package</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">openEuler20.03</td>
    </tr>
    
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libc-2.28.so</td>
    </tr>
    
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libdl-2.28.so</td>
    </tr>
    
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libpthread-2.28.so</td>
    </tr>
    
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libxcrypt</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libcrypt.so.1.1.0</td>
    </tr>
    
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">openssl-libs</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libcrypto.so.1.1.1f</td>
    </tr>
    
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">openssl-libs</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libssl.so.1.1.1f</td>
    </tr>
    
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">pcre</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libpcre.so.1.2.12</td>
    </tr>
    </table>
  </div><br><hr>
 </body>
</html>

效果如下:

除了分割线离表格底部比较近之外(可以在第二个 <table> 外边套一个 <form> </form> 来进行解决),其他好像没啥问题。

但是当我对网页进行缩放之后,问题就暴露出来了。

放大之后,这两个表格竟然有部分重叠(这可不是我希望的),所以只好再另外想想办法了。

于是乎,为了让两个 <table> 可以并列排列在同一行(不管怎么样,都不会重叠),我还是将两个 <table> 都包裹在了各自的 <div> 里边,并把样式设置为了 style="display:inline-block"。


<html>
<body>
  <h1 id="div2" align="center" style="margin-bottom:0px;padding-bottom:0px;font-size:1.625em;">so compare</h1><br>
  <div style="display:inline-block;margin-right:20px;float: left;">

  <table style="border-collapse:collapse;border:1px outset black;">
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">package</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">centos7.6</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libc-2.28.so</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libdl-2.28.so</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libpthread-2.28.so</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libxcrypt</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libcrypt.so.1.1.0</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">openssl-libs</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libcrypto.so.1.1.1f</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">openssl-libs</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libssl.so.1.1.1f</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">pcre</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libpcre.so.1.2.12</td>
    </tr>
    </table>
    </div>
    <div style="display:inline-block;float: none;">

  <table style="border-collapse:collapse;border:1px outset black;">
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">package</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">openEuler20.03</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libc-2.17.so</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libdl-2.17.so</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libpthread-2.17.so</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">pcre</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libpcre.so.1.2.0</td>
    </tr>
    </table>
    </div>
  <br><br><hr>
 </body>
</html>

上面的 html 可以实现如图效果: 

然鹅有个尴尬的问题就是,最后的 <hr> 分割线竟然没有贯穿整行,这可也不是我希望的(我当然希望分割线是在贯穿整行而且不会穿过表格)。

解决办法: 在两个 <div> 外边再套一层 <div> ,并将这个最外层的 <div> 的样式修改为 <div style="display:inline-block">。

最后的效果就是下面这样(你可以肆意缩放,它们都不会再有重叠的部分啦):


<html>
<body>
  <h1 id="div2" align="center" style="margin-bottom:0px;padding-bottom:0px;font-size:1.625em;">so compare</h1><br>
  <div style="display:inline-block">
      <div style="display:inline-block;margin-right:20px;float: left;">

  <table style="border-collapse:collapse;border:1px outset black;">
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">package</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">centos7.6</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libc-2.28.so</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libdl-2.28.so</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libpthread-2.28.so</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libxcrypt</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libcrypt.so.1.1.0</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">openssl-libs</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libcrypto.so.1.1.1f</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">openssl-libs</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libssl.so.1.1.1f</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">pcre</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libpcre.so.1.2.12</td>
    </tr>
    </table>
      </div>
   <div style="display:inline-block;float: none;">

  <table style="border-collapse:collapse;border:1px outset black;">
   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">package</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">openEuler20.03</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libc-2.17.so</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libdl-2.17.so</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">glibc</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libpthread-2.17.so</td>
    </tr>

   <tr>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">pcre</td>
    <td class="row" style="background-color:#eeeeee;font-weight:bold;text-align:left;font-size:0.94em;white-space:nowrap;border:1px inset gray;padding: 3px;">libpcre.so.1.2.0</td>
    </tr>
    </table>
   </div>
  </div>
  <br><br><hr>
 </body>
</html>

猜你喜欢

转载自blog.csdn.net/TomorrowAndTuture/article/details/115181661