响应表不起作用

时间:2016-08-29 06:56:43

标签: html css html-table responsive

我不知道为什么表格不能按设备扩展。例如在移动设备上我有一个滚动但我希望表可以扩展。这是我的代码

<div class="table-responsive">
  <p class="title-table">text</p>
  <table class="table">
    <thead>
      <tr>
        <th class="table-header">txt</th>
        <th class="table-header">txt/th>
        <th class="table-header">txt</th>
      </tr>
    </thead>
    <tbody class="table-body-text">
      <tr class="table-bg-light">
        <td>txt</td>
        <td>txt</td>
        <td>stxt</td>
      </tr>
      <tr class="table-bg-dark">
        <td>txt/td>
        <td>1xttttt</td>
        <td>txt</td>
      </tr>
      <tr class="table-bg-light">
        <td>txt</td>
        <td>125dc</td>
        <td>Ptxtxt</td>
      </tr>
    </tbody>
  </table>
</div>

这是jsfiddle - https://jsfiddle.net/q23c5ckn/ 以下是我想要的示例 - https://jasonbradley.me/2013/09/05/bootstrap-responsive-tables/

4 个答案:

答案 0 :(得分:0)

编辑 - DEMO

.table {
  width:100%;
  background:red;
  word-break: break-word; /* will break word to fit */
}

如果我告诉你这是你需要的DEMO

CSS

.table {
  width:100%;
  background:red
}

.table-responsive {
  overflow:scroll;
}

答案 1 :(得分:0)

添加此标记

<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0"/>

2.使用媒体查询。

实施例: -

/* Smartphones (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 480px) {
/* Styles */
}

/* Smartphones (landscape) ----------- */
@media only screen 
and (min-width : 321px) {
/* Styles */
}

/* Smartphones (portrait) ----------- */
@media only screen 
and (max-width : 320px) {
/* Styles */
}

/* iPads (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) {
/* Styles */
}

/* iPads (landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape) {
/* Styles */
}

/* iPads (portrait) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : portrait) {
/* Styles */
}

/* Desktops and laptops ----------- */
@media only screen 
and (min-width : 1224px) {
/* Styles */
}

/* Large screens ----------- */
@media only screen 
and (min-width : 1824px) {
/* Styles */
}

/* iPhone 4 ----------- */
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}

答案 2 :(得分:0)

我认为你需要对表做出响应。

只需添加表格的样式=

即可
  

。表{       宽度:100%;       显示:块; }

我认为它会帮助你。

答案 3 :(得分:0)

以下是我如何解决这个问题。如果它运作良好,请告诉我。

链接:https://jsfiddle.net/q23c5ckn/1/

CSS:

table { 
width: 100%; 
border-collapse: collapse; 
}
th { 
background: #333; 
color: white; 
font-weight: bold; 
}
td, th { 
padding: 6px; 
border: 1px solid #ccc; 
}

@media only screen and (max-width: 760px),
{
table, thead, tbody, th, td, tr { 
display: block;
max-width: 100%;
}
thead tr { 
display: none;}

tr { border: 1px solid #ccc; }

td { 
border: none;
border-bottom: 1px solid #eee; 
position: relative;
padding-left: 50%;
margin-left: 10px;
overflow: auto;
text-align: center;   
}

td:before { 
text-align: left;
font-weight: bold;
position: absolute;
top: 6px;
left: 6px;
width: 45%; 
margin-right: 20px;
padding-right: 10px; 
white-space: nowrap;
}

td:nth-of-type(1):before { content: "heading 1"; }
td:nth-of-type(2):before { content: "heading 2"; }
td:nth-of-type(3):before { content: "heading 3"; }
}

HTML:

<table>
<thead>
<tr>
    <th>Heading 1</th>
    <th>Heading 2</th>
    <th>Heading 3</th>
</tr>
</thead>
<tbody>
<tr>
    <td>txt</td>
    <td>txt</td>
    <td>txt</td>
</tr>
<tr>
    <td>txt</td>
    <td>txt</td>
    <td>txt</td>
</tr>