简体   繁体   English

调整窗口大小时,如何防止所有弹性项目调整大小?

[英]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.

相关问题 当我切换菜单以关闭窗口并调整窗口大小时,如何防止徽标消失? - How do I prevent my logos from disappearing when I toggle my menu to close and resize my window? 当我调整窗口大小时,如何让我的 Flex 框侧边栏全屏显示? - How to make my Flex box sidebar go full screen when i resize the window? 调整浏览器窗口大小时如何防止页脚更改其位置? - how can i prevent the footer from changing its position when resizing the browser window? 调整大小超过最大大小后,如何防止关闭Bootstrap模式对话框? - How can I prevent the closure of my Bootstrap modal dialog when I resize past its maximum size? 如何使特定的幻灯片放映窗口调整大小? - How can I make my specific slideshow resize with the window? 如何在调整窗口大小时覆盖我的数组? - How can I overwrite my array on window resize? 阻止页面的所有内容调整大小 - Prevent all of my contents of the page from resizing 我需要根据窗口调整大小来调整搜索栏的大小,并且需要为搜索栏调整大小的代码提供功能 - I need to resize my search bar based on the window resizing and I need make a function for the search bar resizing code 我想阻止我的浏览器无法调整大小 - i want to prevent my browser can't able to resize 如何在窗口调整大小或在角度6中失去焦点时防止重新渲染 - How can I prevent re-render when window resize or lost focus in angular 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM