Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

html - Grid with overlapping rows?

I'm creating a card game and I want to use a grid to position the cards (div's). How can I create a grid where the rows overlap like in this image?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I did it with floats. At least it works in all browsers.

HTML:

<div class="card-set">
  <div class="row row-1">
    <!-- 1st row -->
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
  </div>
  <div class="row row-2">
    <!-- 2nd row -->
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
  </div>
  <div class="row row-3">
    <!-- 3rd row -->
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
  </div>
  <div class="row row-4">
    <!-- 4rd row -->
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
    <div class="col-1-of-10">
      <div class="card"></div>
    </div>
  </div>
</div>

SCSS

$grid-width: 1000px;
$gutter-horizontal: 15px;
$next-row-margin: 15px;
$card-height: 80px;
$card-gutter: 11px;
$row-number: row !default;

.row {
  max-width: $grid-width;
  margin: 0 auto;
  z-index: 0;
  position: absolute;

  // Clearfix
  &:after {
    content: "";
    display: table;
    clear: both;
  }

  [class^="col-"] {
    float: left;

    &:not(:last-child) {
      margin-right: $gutter-horizontal;
    }
  }
}

@for $i from 2 to 10 {
  .#{$row-number}-#{$i} {
    top: #{$card-gutter * $i};
    z-index: $i - 1;
  }
}

.col-1-of-10 {
  width: calc((100% - 9 * #{$gutter-horizontal})/10);
}

.card {
  position: relative;
  width: 80px;
  background-color: white;
  height: $card-height;
  border: 3px solid black;
  border-radius: 10px;
}

Here's a Codepen: https://codepen.io/alac1984/pen/YzzmRJK?editors=1100


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...