PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 17, 2022

[FIXED] How to vertically align nested children in a horizontal flexbox?

 November 17, 2022     css, flexbox, html, vertical-alignment     No comments   

Issue

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>


Solution

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.



Answered By - razgraf
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing