Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two vertical blocks with dynamic content

Tags:

css

Looking for a pure CSS solution to achieve the following scenario:

  • Two vertical blocks.
  • Each block grows with its content as much as possible.
  • Sum of heights can never be greater than 100% of the blocks' container.
  • Each block has the right to 50% of the vertical space but can borrow unused space from the other.

Consider the following example:

enter image description here

Panel 1. Blocks A and B are just tall enough to fit their content.

Panel 2: As block A adds more content, it is allowed to grow as much as it can without pushing block B over the edge. Block B is just tall enough to fit its content. Once the sum of the heights of blocks A and B reaches 100%, block A starts scrolling.

Panel 3: Now block B adds content, and takes back some of the space borrowed by block A. Block A is still scrolling. Block B is not scrolling and is just tall enough to fit its content.

Panel 4: Once block B reaches 50% of the total height, both blocks A and B scroll.

like image 622
ryan Avatar asked Oct 30 '22 16:10

ryan


1 Answers

You could use flex and its other properties to accomplish your ask.

Here is what you could do. It may require some tweaks depending on your content and its size. But this could be a good starting point for a CSS only solution.

.container {
  display: flex;
  flex-direction: row;
  flex-basis: 25%;
  height: 600px;
  border: 1px solid red;
}
.column {
  flex: 1 1;
  width: 25%;
  display: flex;
  flex-direction: column;
  flex-basis: auto;
  margin: 1px;
}
.class-a,
.class-b {
  background-color: cyan;
  border: 1px solid black;
  overflow: auto;
  align-items: flex-start;
  justify-content: flex-start;
}
.column:nth-child(3) .class-a {
  flex: 1 1 50%;
}
.column:nth-child(3) .class-b {
  flex: 0 0 auto;
}
<div class="container">
  <div class="column">
    <div class="class-a">
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
    </div>
    <div class="class-b">It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
    </div>
  </div>
  <div class="column">
    <div class="class-a">
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
      survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown
      printer took a galley of type and scrambled it to make a type specimen book. It
    </div>
    <div class="class-b">
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
    </div>
  </div>
  <div class="column">
    <div class="class-a">
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
      survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown
      printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.Lorem Ipsum is simply dummy text of the printing
      and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also
      the leap into electronic typesetting, remaining essentially unchanged.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer
      took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
    </div>
    <div class="class-b">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
    </div>
  </div>
  <div class="column">
    <div class="class-a">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text
      ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. Lorem Ipsum
      is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived
      not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
    </div>
    <div class="class-b">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text
      ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. Lorem Ipsum
      is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived
      not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
    </div>
  </div>

</div>
like image 124
Sreekanth Avatar answered Nov 20 '22 14:11

Sreekanth