0

I'm trying to create a responsive CSS grid with the following specifications:

  • responsive
  • content blocks are left aligned
  • content wrapper fits the content blocks (this is the part that may not be possible)
  • content wrapper is vertically and horizontally centered
  • pure CSS

I have the following code:

* {
  padding: 0;
  margin: 0;
  line-height: 1;
  box-sizing: border-box;
}

.tiles,
.tile,
.tile a {
  padding: 10px;
}

body {
  background: blue;
}

.tiles {
  display: flex;
  flex-flow: flex;
  flex-wrap: wrap;
  background: red;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.tile {
  height: 100px;
  width: 100px;
  background: green;
}

.tile a {
  display: block;
  height: 100%;
  background: yellow;
}
<div class="tiles">

  <div class="tile">
    <a>Item</a>
  </div>

  <div class="tile">
    <a>Item</a>
  </div>

  <div class="tile">
    <a>Item</a>
  </div>

  <div class="tile">
    <a>Item</a>
  </div>

  <div class="tile">
    <a>Item</a>
  </div>

  <div class="tile">
    <a>Item</a>
  </div>

  <div class="tile">
    <a>Item</a>
  </div>

  <div class="tile">
    <a>Item</a>
  </div>

  <div class="tile">
    <a>Item</a>
  </div>

  <div class="tile">
    <a>Item</a>
  </div>

  <div class="tile">
    <a>Item</a>
  </div>

  <div class="tile">
    <a>Item</a>
  </div>

  <div class="tile">
    <a>Item</a>
  </div>

  <div class="tile">
    <a>Item</a>
  </div>

</div>

I've tried flex and floats, but I can't get the container DIV (.tiles - red) to resize to the content when the window is resized.

Is it possible without javascript at all?

Thanks!

isherwood
  • 58,414
  • 16
  • 114
  • 157
dai.hop
  • 741
  • 3
  • 11
  • 14

0 Answers0