How can I make a bulma table responsive?
Asked Answered
C

3

11

I am using the Bulma CSS framework and specifically I am trying to make the table in it responsive.

I have tried giving it a width: 100%; and applying overflow-x: auto; but it doesn't seem to work. Here is the demo: http://104.236.64.172:8081/#/pricing

Code:

<div class="panel-block">
    <table class="table is-bordered pricing__table">
        <thead>
            <tr>
                <th>Travellers</th>
                <th>Standard</th>
                <th>Delux</th>
                <th>Premium</th>
                <th>Luxury</th>
            </tr>
        </thead>

        <tbody>
            <tr>
                <td>Per Person Cost</td>
                <td>₹ 70,523.90</td>
                <td>₹ 70,523.90</td>
                <td>₹ 70,523.90</td>
                <td>₹ 70,523.90</td>
            </tr>
            <tr>
                <td>
                    Extra Person <br>
                    (> 12 yrs)
                </td>
                <td>₹ 70,523.90</td>
                <td>₹ 70,523.90</td>
                <td>₹ 70,523.90</td>
                <td>₹ 70,523.90</td>
            </tr>
            <tr>
                <td>
                    Extra Child <br>
                    (> 12 yrs)
                </td>
                <td>₹ 70,523.90</td>
                <td>₹ 70,523.90</td>
                <td>₹ 70,523.90</td>
                <td>₹ 70,523.90</td>
            </tr>
            <tr>
                <td>Total Cost</td>
                <td>₹ 70,523.90</td>
                <td>₹ 70,523.90</td>
                <td>₹ 70,523.90</td>
                <td>₹ 70,523.90</td>
            </tr>
        </tbody>
    </table>
</div>

Relevant CSS:

.pricing__table {
    width: 100%;
    overflow-x: auto;
}
Cookstove answered 5/12, 2017 at 7:30 Comment(2)
I'm unable to reproduce the problem, as it is described.Saluki
Just look at the demo. The problem's there.Cookstove
I
10

You could wrap the table in a container, and apply the overflow property there instead.

Also, you can use the is-fullwidth modifier on table, instead of declaring width in the CSS.

fiddle

.table__wrapper {
  overflow-x: auto;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.1/css/bulma.css" rel="stylesheet" />
<div class="table__wrapper">
  <table class="table is-bordered pricing__table is-fullwidth">
    <tbody>
      <tr>
        <td>Per Person Cost</td>
        <td>₹ 70,523.90</td>
        <td>₹ 70,523.90</td>
        <td>₹ 70,523.90</td>
        <td>₹ 70,523.90</td>
      </tr>
      <tr>
        <td>
          Extra Person <br> (> 12 yrs)
        </td>
        <td>₹ 70,523.90</td>
        <td>₹ 70,523.90</td>
        <td>₹ 70,523.90</td>
        <td>₹ 70,523.90</td>
      </tr>
      <tr>
        <td>
          Extra Child <br> (> 12 yrs)
        </td>
        <td>₹ 70,523.90</td>
        <td>₹ 70,523.90</td>
        <td>₹ 70,523.90</td>
        <td>₹ 70,523.90</td>
      </tr>
      <tr>
        <td>Total Cost</td>
        <td>₹ 70,523.90</td>
        <td>₹ 70,523.90</td>
        <td>₹ 70,523.90</td>
        <td>₹ 70,523.90</td>
      </tr>
    </tbody>
  </table>
</div>

Update as per comment

In your case, you also need to add the width property to .pricing

updated fiddle

.table__wrapper {
  overflow-x: auto;
}

.pricing {
  width: 100%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.1/css/bulma.css" rel="stylesheet"/>
<div class="panel">
  <div class="panel-block">
    <div class="pricing">
      <div class="table__wrapper">
        <table class="table is-bordered pricing__table is-fullwidth">
          <tbody>
            <tr>
              <td>Per Person Cost</td>
              <td>₹ 70,523.90</td>
              <td>₹ 70,523.90</td>
              <td>₹ 70,523.90</td>
              <td>₹ 70,523.90</td>
            </tr>
            <tr>
              <td>
                Extra Person
                <br> (> 12 yrs)
              </td>
              <td>₹ 70,523.90</td>
              <td>₹ 70,523.90</td>
              <td>₹ 70,523.90</td>
              <td>₹ 70,523.90</td>
            </tr>
            <tr>
              <td>
                Extra Child
                <br> (> 12 yrs)
              </td>
              <td>₹ 70,523.90</td>
              <td>₹ 70,523.90</td>
              <td>₹ 70,523.90</td>
              <td>₹ 70,523.90</td>
            </tr>
            <tr>
              <td>Total Cost</td>
              <td>₹ 70,523.90</td>
              <td>₹ 70,523.90</td>
              <td>₹ 70,523.90</td>
              <td>₹ 70,523.90</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
Incest answered 5/12, 2017 at 9:18 Comment(7)
Hey, thanks. There is a problem when I apply this solution to the running website here: 104.236.64.172:8081/#/pricing Maybe it has something to do with flexbox or the fact that I am using a table inside a panel-block of bulma. It doesn't seem to work. Can you help me debug it? Just apply these changes to the live site using chrome inspector and see for yourself.Cookstove
Are you able to change the markup? Try wrapping the table in its own div like in the snippet.Incest
I did. You can check for yourself. In the inspector. Just right click the <table> node and Edit as HTML. Then you wrap the table in a div.Cookstove
I wasn't aware you could edit the HTML like that.. learn something new every day! I got it working by removing flex on the panel-block. I think you'll need to restructure your HTML. It would be easier to help if you edit your question with the relevent HTML and CSS, thanksIncest
Did you remove display: flex from the panel-block? It didn't fix for me. Can you elaborate on the changes you made?Cookstove
BTW it's working just fine without .is-fullwidth on table.Cookstove
Bulma has a built-in solution bulma.io/documentation/elements/table/#table-containerAkvavit
Y
9

use .table-container in parent div:

<div class="table-container">
  <table class="table">
    <!-- Your table content -->
  </table>
</div>

https://bulma.io/documentation/elements/table/#table-container

Yingyingkow answered 16/12, 2019 at 22:16 Comment(1)
Right, just don't wrap the .table-container in a columns neither column classManakin
B
4

I found a great alternative that turns horizontal elements into vertical ones:

@media
only screen and (max-width: 1500px) {
  table, thead, tbody, th, td, tr {
    display: block;
  }
  thead tr {
    position: absolute;
    top: -9999px;
    left: -9999px;
  }
  tr { border: 1px solid #ccc; }
  td {
    border: none;
    border-bottom: 1px solid #eee;
    position: relative;
    padding-left: 200px;
    margin-left: 150px;
  }
  td:before {
    position: absolute;
    top: 12px;
    left: 6px;
    width: 200px;
    padding-right: 40px;
    white-space: nowrap;
    margin-left: -150px;
  }
  td:nth-of-type(1):before { content: "Option"; }
  td:nth-of-type(2):before { content: "Description"; }
  td:nth-of-type(3):before { content: "Type"; }
  td:nth-of-type(4):before { content: "Default";}
}

If you don't need the headers, remove the td:before modifiers.

Bandaranaike answered 22/6, 2019 at 8:13 Comment(1)
This is awesome!Comedietta

© 2022 - 2024 — McMap. All rights reserved.