I want to create a page where my elements are displayed in a Grid. I want to align the items by line. I want to achieve visually the following result, but I don't know how: https://codepen.io/shirkit/pen/KvZKOE/
And I currently have this:
var back = ["red","blue","gray","black","green","cyan","brown","magenta"];
var i = 0;
$('#container .card').children().each(function() {
$(this).css('background-color',back[i++]);
if (i == back.length) i = 0;
});
#container {
display: flex;
flex-direction: row;
}
.card {
max-width: 200px;
width: 100%;
}
.card .image {
height: 150px;
}
.card .text {
height: 100px;
}
.card .list {
height: 50px;
}
#z1 .image {
height: 175px;
}
#z2 .text {
height: 120px;
}
#z3 .list {
height: 60px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div id="z1" class="card">
<div class="image"></div>
<div class="text"></div>
<div class="list"></div>
</div>
<div id="z2" class="card">
<div class="image"></div>
<div class="text"></div>
<div class="list"></div>
</div>
<div id="z3" class="card">
<div class="image"></div>
<div class="text"></div>
<div class="list"></div>
</div>
</div>
Clarification: the heights on the ID selectors #z1 .image, #z2.text #z3 .list
are there just for simulating the height that the components would have. Therefore, it's NOT an option to change those values. Also note that height values of those elements/containers are not known in advance, so setting margin is also NOT an option.
The current code shown is not how it's currently, it's just for show how visually currently looks, but it should follow that structure, a container
for everything, a card
for the element and the items inside the card.
Now I know how to do this if I don't put the items in the .card
element, and then using the CSS display: grid
in #container
. I would have the elements .image/.text/.list
as direct children from #container
, but this is the last resort, I wanted an alternative.
I'm currently looking for a solution that allows for me to keep the .card
elements, and have possible fallbacks.
Edit: The proposed answers that were marked as duplicates does not solves the issue I presented here, or I haven't found any others related.