I have a <div class="row">
which will contain dynamically generated buttons (basically I need boxes)
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.
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. 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
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. If you need the box to maintain the square shapes on adding more boxes, you need to use a bit of javascript as well.
What are we doing?
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.