简体   繁体   English

删除由换行引起的空白

[英]Remove white-space caused by wrap

I have a fieldset containing multiple buttons with the same fixed width.我有一个包含多个具有相同固定宽度的按钮的字段集。 In a row/line it should fit as many buttons as possible.在一行/一行中,它应该适合尽可能多的按钮。 However during the linebreak it will cause a big white-space.然而,在换行期间,它会导致一个很大的空白。

I tried display: flex;我试过display: flex; in combination with flex-wrap .结合flex-wrap However the white-space remains or the box overflows但是空白仍然存在或框溢出

Also I tried css-grid by using grid-template-columns: repeat(auto-fit, minmax(10em, 1fr));我还尝试了使用grid-template-columns: repeat(auto-fit, minmax(10em, 1fr)); which doesn't work either.这也不起作用。

Anybody else with an idea how to size either size the box to the content to not leave a white-space or to size the buttons to fill up all the space without breaking the box max-width and fit as many as possible?还有其他人知道如何根据内容调整框的大小以不留下空白或调整按钮的大小以填充所有空间而不破坏框的最大宽度并尽可能多地适应吗?

I know why the white space there, but I can't figure out how to remove it.我知道为什么那里的空白,但我不知道如何删除它。 I'm aware that it possibly requires Javascript.我知道它可能需要 Javascript。 However related questions on SO mentioned this aswell but where unable to eleborate a Javascript solution to be understandable/reproduciable.然而,关于 SO 的相关问题也提到了这一点,但无法详细说明 Javascript 解决方案是可理解/可重现的。

 div { max-width: 80%; } button{ width: 10em; }
 <div> <fieldset> <legend>Color</legend> <button>1</button> <button>2</button> <button>3</button> <button>4</button> <button>5</button> <button>6</button> <button>7</button> <button>8</button> <button>9</button> </fieldset> </div>

If the sizes are fixed here is an idea using CSS grid.如果这里的尺寸是固定的,则使用 CSS 网格的想法。 The trick is that we define a grid using a repetition of the button width and we make the fieldset take all the columns.诀窍是我们使用重复的按钮宽度定义一个网格,并使字段集占据所有列。 This will for it to have a width equal to a N*width of button where N is dynamic and depend on the screen size这将使它的宽度等于N*width of button其中N是动态的并且取决于屏幕尺寸

 div.box { display:grid; font-size:14px; /*0.75em is the default padding of fieldset */ grid-template-columns:0.75em repeat(auto-fit,10em) calc(0.75em + 4px); grid-gap:4px; } fieldset { grid-column:1/-1; } button{ width: 10em; font-size:14px; /* define the font-size to avoid the default one*/ margin-right:4px; /* same as gap */ }
 <div class="box"> <fieldset> <legend>Color</legend> <button>1</button><button>2</button><button>3</button><button>4</button><button>5</button><button>6</button><button>7</button><button>8</button><button>9</button> </fieldset> </div>

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

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