0

As you can see in the snippet below, the dots under foo-wrapper do not follow the vertical alignment of the text labels under bar-wrapper. But I was expecting this, as per the :nth-child(i) rules.

How can I make the dots, which are the children of a different container which is subsequently a child of a horizontal flexbox (the parent-wrapper), follow the vertical alignment of the children from the other container?

Do I have to renounce flexbox altogether and use something like the grid layout?

<!DOCTYPE html>

<head>
  <style>
    .parent-wrapper {
      align-items: stretch;
      display: flex;
      flex-flow: row nowrap;
      height: 256px;
      width: 100%;
    }
    
    .foo-wrapper {
      background-color: orange;
      margin: 1.5rem;
      width: 2px;
    }
    
    .dot {
      background-color: #ebf0ff;
      border-radius: 0.5rem;
      height: 1rem;
      width: 1rem;
    }
    
    .foo-wrapper div:nth-child(1) {
      margin-top: 1rem;
    }
    
    .foo-wrapper div:nth-child(2) {
      margin-top: 1rem;
    }
    
    .foo-wrapper div:nth-child(3) {
      margin-top: 1rem;
    }
    
    .bar-wrapper {
      border: 1px solid green;
      display: flex;
      flex-flow: column nowrap;
      margin: 1.5rem;
      flex-grow: 1;
    }
       
    .bar-wrapper div:nth-child(1) {
      margin-top: 1rem;
    }
    
    .bar-wrapper div:nth-child(2) {
      margin-top: 2rem;
    }
    
    .bar-wrapper div:nth-child(3) {
      margin-top: 4rem;
    }
  </style>
</head>

<body>
  <div class="parent-wrapper">
    <div class="foo-wrapper">
      <div class="dot"></div>
      <div class="dot"></div>
      <div class="dot"></div>
    </div>
    <div class="bar-wrapper">
      <div>Lorem</div>
      <div>ipsum</div>
      <div>dolores</div>
    </div>
  </div>
</body>
Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114

1 Answers1

1

I find no easy way of doing this, apart from measuring each bar-wrapper child and assigning a height dynamically (js). The most feasible solution right now seems to be restructuring your layout.

Case 1: Mix the bar-wrapper content and foo-wrapper content into a single div.

.wrapper{
   display: flex;
   align-items: flex-start;
}
...
<div class="wrapper">
   <div class="dot"/>
   <div class="content">
      <p>text</p>
   </div>
<div>

Case 2: If you want this exact design, keep the line from the left side separate (either as a simple <div/> or as a border-left of the parent container and add the dot as a pseudo-element e.g.

.content::before{
   position: absolute;
   content:"";
   height: 14px;
   width: 14px;
   border-radius: 7px;
   background: #000000;
   margin-left: -18px;
}
.content{
  position: relative;
  padding-left: 18px;
}

The padding and margin are optional, depending on your layout.

razgraf
  • 221
  • 1
  • 11