在css中创建响应表

时间:2018-06-03 06:19:15

标签: css responsive-design responsive

我有一个tablular结构(没有th的表),看起来像:

--- 1 --- --- 2 ---

--- 3 --- --- 4 ---

我希望它像是移动屏幕:

--- 1 ---

--- 2 ---

--- 3 ---

--- 4 ---

无论我做什么,我都无法设计。我建议的代码:

   <table>
    <tbody style="width: 100%">
    <tr class="row">
        <td class="col-xs12 col-sm-6 display-block">1</td>
        <td class="col-xs12 col-sm-6 display-block">2</td>
    </tr>
    <tr class="row">
        <td class="col-xs12 col-sm-6 display-block">3</td>
        <td class="col-xs12 col-sm-6 display-block">4</td>
    </tr>
    </tbody>
    </table>

总是结果

1

2

3

4

且没有显示块总是结果

--- 1 --- --- 2 ---

--- 3 --- --- 4 ---

2 个答案:

答案 0 :(得分:3)

如果你使用bootstrap ,你必须设置col-xs-12 而不是 col-xs12(添加-

参见JSFiddle: https://jsfiddle.net/v09bzj89/

&#13;
&#13;
  
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  <table>
    <tbody style="width: 100%">
    <tr class="row">
        <td class="col-xs-12 col-sm-6 ">1</td>
        <td class="col-xs-12 col-sm-6 ">2</td>
    </tr>
    <tr class="row">
        <td class="col-xs-12 col-sm-6">3</td>
        <td class="col-xs-12 col-sm-6">4</td>
    </tr>
    </tbody>
    </table>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用媒体查询并仅在该块中添加display: block样式,以便它仅适用于特定分辨率。

// This style will be applied only below screen widths 600px
@media screen and (max-width: 600px) {
  .display-block {
       display: block !important;
  }
}

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
  @media screen and (max-width: 600px) {
  .display-block {
   display: block !important;
  }
}
  </style>
</head>
<body>
   <table>
  <tbody style="width: 100%">
     <tr class="row">
        <td class="col-xs12 col-sm-6 display-block">1</td>
        <td class="col-xs12 col-sm-6 display-block">2</td>
     </tr>
     <tr class="row">
        <td class="col-xs12 col-sm-6 display-block">3</td>
        <td class="col-xs12 col-sm-6 display-block">4</td>
     </tr>
  </tbody>
   </table>
</body>
</html>