简体   繁体   English

在内容超过div的高度后扩展div的宽度

[英]Expanding the width of div after the content exceeds the height of the div

I have been studying CSS flex recently and was trying out some simple tasks with it. 我最近一直在研究CSS flex,并尝试用它来完成一些简单的任务。 One of these tasks was to make a timeline with alternating elements being above or below a mid-line. 其中一项任务是制作一个时间轴,其中交替的元素位于中线之上或之下。

I would like to be able to set an initial width and a fixed height, so that when the content exceeds the height of the div and overflows, the width will expand to accommodate the content, and hopefully there will be no overflow. 我希望能够设置初始宽度和固定高度,这样当内容超过div的高度并溢出时,宽度将扩展以容纳内容,并且希望不会溢出。

https://codepen.io/anon/pen/roVOXB https://codepen.io/anon/pen/roVOXB

<div class="content">
  <div class="box">
    <div class="above">Content of the div</div>
    <div class="below"></div>
  </div>
  <div class="box">
    <div class="above"></div>
    <div class="below">Content of the div</div>
  </div>
  <div class="box">
    <div class="above">Content of the div</div>
    <div class="below"></div>
  </div>
  <div class="box">
    <div class="above"></div>
    <div class="below">Content of the div</div>
  </div>
  <div class="box">
    <div class="above">Content of the div</div>
    <div class="below"></div>
  </div>
</div>

.content {
  display: flex;
}

.box {
  display: flex;
  flex-direction: column;
}

.above, .below {
  height: 350px;
  display: flex;
  min-width: 300px;
  border: 1px solid #000;
}

.above {
  display: flex;
  align-items: flex-end;
  background: orange;
}

.below {
  display: flex;
  align-items: flex-start;
  background: aqua;
}

I know it might be a simple solution and its staring me right in the face, but I have tried everything with max-widths, max-heights, overflows and wrapping the text but nothing can help me achieve the desired outcome so far. 我知道这可能是一个简单的解决方案,它正好盯着我,但我已经尝试了所有的最大宽度,最大高度,溢出和包装文本但没有任何东西可以帮助我达到目前为止所期望的结果。

I would really appreciate any help. 我真的很感激任何帮助。 Thanks! 谢谢!

You can change the elements dynamically using the following JS: 您可以使用以下JS动态更改元素:

var allElements = document.querySelectorAll('.below, .above');
allElements.forEach((Ele) => {
  if(Ele.scrollHeight > 350){
    Ele.style.minWidth = (Ele.scrollHeight) + "px";
  }
});

What is does it iterate through all the elements with classes below and above , then checks if it uses more height than the height: 350px , if it is > 350px , then it will adjust accordingly. 什么是迭代所有具有belowabove类的元素,然后检查它是否使用比高度更高的height: 350px ,如果它> 350px ,那么它将相应地调整。

Just add the above JS code in your codepen JS editor, and you're good to go. 只需在您的codepen JS编辑器中添加上面的JS代码,就可以了。 Try adding more content to it and it will dynamically change the width w/o overflow. 尝试添加更多内容,它将动态更改宽度无溢出。

If the number of columns is static, you can remove the min-width from .above, .below . 如果列数是静态的,则可以从.above, .below删除min-width

Add min-width: 2700px; 添加min-width: 2700px; to .content . .content

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM