简体   繁体   English

自动将内容调整为div

[英]Auto resize content to div

I have a <div class="row"> which will contain dynamically generated buttons (basically I need boxes) 我有一个<div class="row"> ,它将包含动态生成的按钮(基本上我需要使用框)

I need the parent div to be of fixed size but need its contents to resize (to fit everything in one row) automatically based on number of buttons inserted. 我需要父div的大小固定,但需要根据插入的按钮数自动调整其内容的大小(以适应一行中的所有内容)。

Consider the following layout: 考虑以下布局:

在此处输入图片说明

All of the solutions I have tried need fixed width or the boxes get plotted in new line. 我尝试过的所有解决方案都需要固定宽度,否则将在新行中绘制方框。

My last option would be using ng-style and setting the width to 100/number of boxes. 我的最后一个选择是使用ng-style并将宽度设置为100 /箱数。 I need some other solution. 我需要其他解决方案。

One of the things I have tried so far: 到目前为止我尝试过的一件事:

.wrap {
    width:80%;
    margin:0 auto;
}
.wrap div {
    width:23%;
    padding-bottom:23%;
    margin:1%;
    float:left;
    background:gold;
}

<div class="wrap">
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>

Use flexbox 使用flexbox

 div { display: flex; } button { flex: 1 1; } 
 <div> <button>1</button> <button>2</button> <button>3</button> <button>4</button> </div> 

Ex. 例如 with your code. 与您的代码。

 .wrap { width: 80%; margin: 0 auto; display: flex; } .wrap div { flex: 1 1; padding-bottom: 23%; background: gold; border: 1px solid; } 
 <div class="wrap"> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </div> 

Just using flexbox won't cut it. 仅使用flexbox不会削减它。 If you need the box to maintain the square shapes on adding more boxes, you need to use a bit of javascript as well. 如果您需要在添加更多框时保持框的正方形形状,则还需要使用一些JavaScript。

What are we doing? 我们在做什么?

  • We're adding more boxes and changing their height by maintaing an iterative variable(i) which checks how many boxes are present and divides the height according to the width. 我们要添加更多的盒子并通过维护一个迭代变量(i)来更改它们的高度,该变量检查存在多少盒子并根据宽度划分高度。 Since, the width is fixed if 3 boxes are present, the height should be equal to width of each item present. 由于如果有3个盒子,则宽度是固定的,因此高度应等于每个项目的宽度。
  • You can implement a function which counts the number of boxes on load and resizes the flex container height according to it to have the boxes keep the square dimension. 您可以实现一个功能,该功能可以计算加载时的盒子数量,并根据此大小调整伸缩容器的高度,以使盒子保持正方形尺寸。
     var flex = document.getElementById("container"); var box = document.getElementById("box1"); var i=1; function add() { i++; var cln = box.cloneNode(true); cln.removeAttribute("id"); flex.appendChild(cln); flex.style.height=(flex.clientWidth/i) + 'px'; } 
     .flexc { display: flex; width: 400px; height:400px; } .box { flex: 1; border-radius: 20px; background: olive; margin:1px; } 
     <button id="add" onclick="add()">Add more boxes</button> <div class="flexc" id="container"> <div class="box" id="box1"></div> </div> 

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

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