[英]Using width vs using min-width & max-width
What is the best way to use the css width parameters when trying to keep an element at a set constant width even when the width of the page changes? 试图将元素保持在设置的恒定宽度(即使页面的宽度发生变化)时,使用css width参数的最佳方法是什么?
(ie width: 20rem; & min-width: 20rem;
vs min-width: 20rem; & max-width: 20rem;
) (即width: 20rem; & min-width: 20rem;
vs min-width: 20rem; & max-width: 20rem;
)
Using just width: 20rem;
使用 width: 20rem;
results in the sidebar shrinking from a change in width: 导致边栏因宽度变化而缩小:
Expected width of the sidebar: 侧边栏的预期宽度:
Sidebar shrinks when I would like it to remain the same size: 边栏缩小时,我希望它保持相同的大小:
However using width: 20rem; & min-width: 20rem;
但是使用 width: 20rem; & min-width: 20rem;
width: 20rem; & min-width: 20rem;
seems to solve this issue. 似乎解决了这个问题。
Expected width of the sidebar: 侧边栏的预期宽度:
Expected width of the sidebar remains: 侧边栏的预期宽度保持不变:
Also using min-width: 20rem; & max-width: 20rem;
还使用 min-width: 20rem; & max-width: 20rem;
min-width: 20rem; & max-width: 20rem;
seems to also solve this issue. 似乎也解决了这个问题。
Expected width using max-width: 使用最大宽度的预期宽度:
Expected width using max-width even when window size changes: 即使窗口大小更改,使用max-width的预期宽度:
My overall question is which solution is preferred and what are the consequences of each as they both seem relatively the same 我的总体问题是哪种解决方案更可取,每种解决方案的效果如何,因为它们看上去都相对相同
General css code 通用CSS代码
.messages-component {
.threads-sidebar {
@include box-shadow-helper(1);
background: white;
display: flex;
flex-direction: column;
width: 20rem;
.threads-sidebar-header {
@include box-shadow-helper(1);
display: flex;
min-height: 3em;
}
.threads-sidebar-body {
@include box-shadow-helper(1);
display: flex;
flex: 1;
flex-direction: column;
.test {
color: $mvn-btn-red;
}
}
}
}
Min-width and max-width specify constraints on how large/small a resizable object can go. 最小宽度和最大宽度指定可调整大小的对象可以移动的大小限制。 So if you want your element to not be resizable you should just use a constant width by using pixels(px) instead of rem. 因此,如果您希望元素的大小不可调整,则应通过使用pixel(px)而不是rem来使用恒定宽度。
Width and min-width can work together. 宽度和最小宽度可以一起使用。 Most people use two different units when using them example: width: 90%, min-width:600px. 大多数人在使用示例时会使用两个不同的单位:宽度:90%,最小宽度:600px。 It is also worth noting that min-width will overide width. 还值得注意的是,最小宽度将覆盖宽度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.