3

I'm working on a website but I'm running into a small problem, I've got a table with a variable amount of squares in it, all those squares are exaclty the same size as the others. At the moment I'm displaying those squares in a table with 5 columns, however, on a mobile device I'd rather have 2 columns beacuse the size of those squares are to small for the content on a 720 pixels wide mobile screen, which causes the content to go outside of the square.

What I want is a variable amount of columns based on the size of the screen. How could I do such a thing?

3
  • 1
    What you need is Responsive Design : en.m.wikipedia.org/wiki/Responsive_web_design Commented Apr 20, 2015 at 17:59
  • 2
    Does it need to be a table? Use inline-blocks or floats. Commented Apr 20, 2015 at 18:00
  • @MrLister as long as it forms a grid and the squared are all equally sized squares not Commented Apr 20, 2015 at 19:52

3 Answers 3

5

If the blocks are all exactly the same size, it's no problem turning them into inline-blocks instead of table cells.
Inline blocks are blocks that behave like words in plain text, so they flow nicely when the window is resized, just the way you want it.

And all you need is

div {
    display:inline-block;
}

See Fiddle.

Sign up to request clarification or add additional context in comments.

2 Comments

damn that lp website is SLOW.
Thank you so much. Ive been trying forever. Coming from iPhone development so it's so hard to figure out what things in css do
1

For the below example table you could use the given CSS to hide the 3rd and 4th columns depending on the screen size.

HTML

<table>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>

CSS

@media screen and (max-width: 300px) {
    table td:nth-of-type(3) {
        display: none;
    }
}
@media screen and (max-width: 600px) {
    table td:nth-of-type(4) {
        display: none;
    }
}

Comments

1

If the rest of the squares should still be visible and just slide into new rows then i'm afraid u cant avoid JavaScript since the DOM would have to be altered ..

In case tables are not necessary i would reccomend using floats on the squares (like Mr Lister)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.