[英]How can I prevent all of my flex-items from resizing when I resize my window?
I have tried using the following on my flex-container after clicking on my button to generate the content, but when I resize the windows, it keeps resizing my items. 单击按钮生成内容后,我尝试在flex容器上使用以下内容,但是当我调整窗口大小时,它会不断调整项目大小。 I want them to have a fixed width and height (ideally each item would be a square of 100x100 px)
我希望它们具有固定的宽度和高度(理想情况下,每个项目都是100x100像素的正方形)
flex-shrink: 0;
flex-grow: 0;
const sampleContent = ["stuff1", "stuff2", "stuff3", "stuff4", "stuff5"] const button = document.querySelector(".content"); const rootElement = document.querySelector("#root"); const widthDiv = document.querySelector("#width-value"); const resultContainer = document.querySelector("#root"); const widthValue = window.getComputedStyle(resultContainer, null).getPropertyValue("width"); button.addEventListener("click", () => { sampleContent.forEach((content) => { const newDiv = document.createElement("div"); console.log(newDiv); newDiv.textContent = content; newDiv.classList.add("result"); rootElement.appendChild(newDiv); }) }) widthDiv.textContent = widthValue;
.result-container { display: flex; width: 60%; height: 200px; border: 1px black solid; flex-wrap: nowrap; } .container { display: flex; justify-content: center; } .result { width: 100px; height: 100px; border: 1px dotted pink; margin: 1%; }
<div class="container"> <div id="root" class="result-container"> </div> </div> <button class="content"> Generate Content from API </button> <div id="width-value"> </div>
An initial setting on flex items is flex-shrink: 1
. flex项目的初始设置是
flex-shrink: 1
。 That means that items can shrink to avoid overflowing the container. 这意味着物品可以收缩以避免容器溢出。 Add
flex-shrink: 0
to the items and you're all set. 将
flex-shrink: 0
添加到项目中,一切就绪。
const sampleContent = ["stuff1", "stuff2", "stuff3", "stuff4", "stuff5"] const button = document.querySelector(".content"); const rootElement = document.querySelector("#root"); const widthDiv = document.querySelector("#width-value"); const resultContainer = document.querySelector("#root"); const widthValue = window.getComputedStyle(resultContainer, null).getPropertyValue("width"); button.addEventListener("click", () => { sampleContent.forEach((content) => { const newDiv = document.createElement("div"); console.log(newDiv); newDiv.textContent = content; newDiv.classList.add("result"); rootElement.appendChild(newDiv); }) }) widthDiv.textContent = widthValue;
.result-container { display: flex; width: 60%; height: 200px; border: 1px black solid; flex-wrap: nowrap; } .container { display: flex; justify-content: center; } .result { flex: 0 0 100px; /* fg: 0, fs: 0, fb: 100px */ /* width: 100px; */ height: 100px; border: 1px dotted pink; margin: 1%; }
<div class="container"> <div id="root" class="result-container"></div> </div> <button class="content">Generate Content from API</button> <div id="width-value"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.