I am creating a graph from divs. The outermost div is a flexbox and it has nested divs for nodes in it. They are nested because every node recursively creates its child nodes. I want the nodes to be horizontally next to each other, but for some reason each one is in its own row. Additionally, for some reason, the second node is not overlapping the first but every other is overlapping its parent a bit.
What am I doing wrong and is there a way to use flexbox for such a nested graph?
.graph {
display: flex;
flex-direction: row;
justify-content: center;
}
.node {
width: 50px;
height: 50px;
border-radius: 50%;
background-color: #ccc;
text-align: center;
font-size: 18px;
color: #000;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
cursor: pointer;
}
<div class="graph">
<div class="node">
<p>3</p>
<div class="node">
<p>2</p>
<div class="node">
<p>1</p>
<div class="node">
<p>0</p>
</div>
</div>
</div>
</div>
</div>
3
Answers
In my understanding you want something like this. The issue in your code is that you use flex on graph when node added in graph flex property apply on it but when next node added in node it don’t inherit flex property because it don’t contain flex property.
Sorry to say but there is no proper way to do it. Because you can’t use
Flexbox
orGrid
layout models here. So my suggestion is to useposition:absolute
property.I hope my answer is helpful to you.
You can make use of the CSS display: contents setting and still use flex.
This snippet moves the formatting to the child of each node and sets the node itself to
display: contents
. This means its children are considered by the flex.