How can I achieve some overlapping layouts when there is not enough space in the parent container?


graphics

I have <div class="parent" style="position: relative; width: 100%; height: 25vh;">...</div>some <div class="child"></div>elements (their number varies between subpages). As you can see, the size .parentis known, but not fixed. The .childsize is fixed as eg 100px x 170px.

If there is enough room for all the children .parentin, I would like to place them one by one row by row (e.g. this is easy float: left;):enter image description here

However, when .parentgoing to be used by too many children , I'd like the extra elements to be placed "above" the other elements with a small offset (so we can see something under them - kind of like a game of Patience Solitaire ) ):

enter image description here

Note the order, these extra elements don't have to be on any "layer"/z-index, they should overlap the div in front of them.

Also, there is no fixed number of cards per row , as the width is client-side..parent100%

How can I achieve this behavior using CSS without JavaScript (I know I can calculate the absolute x/y based on the child's index in JavaScript)?

Matt Devio

I believe I found a pure CSS solution that might work for you. From what I understand, you are the Parent Div container and random child divs that represent cards.

Disclaimer : I'm using pixels for this to show the proof of concept, but if you do the math, you can achieve the same effect by using all percentages.

Basic HTML Layout

// I am using the <!-- comments --> to remove whitespace between divs
// This is important because we don't want to being floating elements
// So each pixel is going to count
<div class="parent">
     <div class="child">card 1</div><!--
  --><div class="child">card 2</div><!--
  --><div class="child">card 3</div><!--
  --><div class="child">card 4</div><!--
  --><div class="child">card 5</div><!--
  --><div class="child">card 6</div>
</div>

For demonstration purposes, I made these cards 100px by 150px. I think it helps to visualize the math. This is the basic CSS we need to layout the fields.

CSS

// normalize div elements
*{ box-sizing: border-box; margin: 0; padding: 0; }

.parent{
  position: relative;
  width: 480px; //4 cards wide with 10px of margin on each side (4*100)+(4*20)
  background: blue;
  height: 340px;
  overflow:hidden;
  margin: 0px auto;
}
.child{
  position: relative;
  width: 100px;
  height: 150px;
  border:1px solid #000;
  background: #333;
  color: white;
  margin: 10px;
  display: inline-block;
}

Now that the grid is set up, we just need to move the third and fourth rows up.

One of the great things about CSS is that you can apply styles to elements that might not exist on the page. Using the nth-child selector will help us locate these unknown elements.

MOAR CSS

.child:nth-child(n+9){
  // move cards from their origin up 330px and left 5px
  transform: translateY(-330px) translateX(-5px);
}

The only thing you need to do is create enough rules to match the many kinds of results you might have.

Example : I need to support 18 cards . Just add another rule!

.child:nth-child(n+17){
  // all we have to do is double the increments
  transform: translateY(-660px) translateX(-10px);
}

Hope that helps.

JSFiddle at work

Related


How can I achieve some kind of layout

Dennis What's the best way to have a layout like this for an Android app? When the row number is even or 0, the row should have 2 columns. Otherwise, the row should have only 1 column Rows must be dynamically generated. There should also be an image and some t

How can I achieve some kind of layout

Dennis What's the best way to have a layout like this for an Android app? When the row number is even or 0, the row should have 2 columns. Otherwise, the row should have only 1 column Rows must be dynamically generated. There should also be an image and some t

Docker eats /var space how can I save some space?

Dimitrios Desyllas Since there are some good comments on this question , I thought they might be good answers and useful for this particular question. So in order to be able to bring more benefits to the community, I think it's okay to get an answer here. In m