简体   繁体   English

如何防止元素被 DOM 包裹

[英]How to Prevent Elements from Wrapping with DOM

I can't seem to stop these blocks from wrapping.我似乎无法阻止这些块包装。 The grid seems to work fine for inputs less than 50.对于小于 50 的输入,网格似乎工作正常。

This is my HTML but there's not much too it, even tried the whitespace property, inline.这是我的 HTML,但没有太多,甚至尝试了内联的空白属性。 The CSS, which I have made a parent div and it seems to work for the colour, however not for turning off the wrap. CSS,我已经制作了一个父 div,它似乎适用于颜色,但不适用于关闭包装。

How can I get the rows of boxes to continue?我怎样才能让一排排的盒子继续?

enter image description here在此处输入图片说明

 let makeGrid = function(numberOfRows) { let y = 0; let x = 0; while (y < numberOfRows) { x = 0; let makeBox = function(_parentBox, _sizeOfBox) { let box = document.createElement('div'); document.body.appendChild(box); box.style.width = '28px'; box.style.height = '28px'; box.style.border = '1px solid white'; box.style.display = 'inline-block'; return box; }; let makeRowBox = function(parentBox) { let box = document.createElement('div'); parentBox.appendChild(box); //box.style.border = '1px solid black'; return box; }; rowBox = makeRowBox(document.body); while (x < numberOfRows) { makeBox(rowBox, 400); x = x + 1; } y = y + 1; } }; makeGrid(50);
 div { background-color: rgb(68, 157, 230); white-space: nowrap; overflow: hidden; } div>div { display: inline-block; border: 2px solid white; border: '1px solid black'; margin-right: 4px; }
 <div style="white-space: nowrap;"></div>

You can style the boxRows using display: inline-flex , so the rows will expand to fit their contents.您可以使用display: inline-flex设置 boxRows 的样式,因此行将扩展以适应其内容。

I removed all the inline CSS that were set in javascript.我删除了在 javascript 中设置的所有内联 CSS。 Instead, I added a class to the boxes.相反,我在框中添加了一个类。

You also added all the blue boxes to body instead of .row (rowBox).您还将所有蓝色框添加到body而不是.row (rowBox)。

I needed the combination of float and clear to make smaller grids row break.我需要floatclear的组合来使较小的网格行中断。

 let makeGrid = function(numberOfRows) { let y = 0; let x = 0; while (y < numberOfRows) { x = 0; let makeBox = function(_parentBox) { let box = document.createElement('div'); box.classList.add("box"); //document.body.appendChild(box); _parentBox.appendChild(box); /* added */ //return box; }; let makeRowBox = function(parentBox) { let box = document.createElement('div'); box.classList.add("row"); parentBox.appendChild(box); return box; }; rowBox = makeRowBox(document.body); while (x < numberOfRows) { makeBox(rowBox, 400); x = x + 1; } y = y + 1; } }; makeGrid(30);
 .row { display: inline-flex; border: 1px solid black; clear: both; float: left; } .box { --box-spacing: 2px; width: 28px; height: 28px; background-color: rgb(68, 157, 230); border: var(--box-spacing) solid white; flex-basis: 100%; }

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

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