简体   繁体   English

最小宽度,最大宽度和缩小以适合父级

[英]Min-width, max-width, and shrinking to fit in parent

I have several items laid out horizontally that should: 我有几个横向布置的项目应该:

  1. Always stay in a single row (never break to new lines). 始终保持一排(永远不要破坏新线)。
  2. Not be a larger width than their content or their max-width, whichever is smaller. 宽度不应大于其内容或最大宽度,以较小者为准。
  3. Truncate with ellipses when max-width limits their width. 当max-width限制其宽度时,用省略号截断。
  4. Not be a smaller width than their min-width. 宽度不小于最小宽度。

These items share the same parent. 这些项目共享同一个父项。 If the parent is too small in width to contain the items using the above rules, then the items should shrink width-wise but never get thinner than their min-width. 如果父级的宽度太小而无法使用上述规则包含项目,则项目应按宽度方式缩小,但不得超过其最小宽度。

EDIT: In other words, if the parent is larger than the total width of the children, that's fine. 编辑:换句话说,如果父母大于孩子的总宽度,那很好。 The children should not expand to fill the parent. 孩子们不应该扩大以填补父母。 If the parent becomes smaller than the total width of the children as numbers 1, 2, 3, and 4 dictate, the children should shrink to stay within the new parent width. 如果父项小于子项的总宽度(数字1,2,3和4),则子项应缩小以保持在新的父项宽度内。 An example of this (and actually what I'm trying to reproduce) can be seen in Chrome's browser tabs. 在Chrome的浏览器标签中可以看到一个这样的例子(实际上我正在尝试重现的内容)。 The tabs have a max-width and a min-width. 标签具有最大宽度和最小宽度。 If you start shrinking the browser width-wise then it will get to a point where the tabs will no longer be able to fit so they start shrinking until they hit their min-width. 如果你开始缩小浏览器的宽度,那么它将达到标签不再适合的点,这样它们就会开始收缩,直到它们达到它们的最小宽度。

I've nailed down numbers 1, 2, 3, and 4 but I don't know how to get them to shrink to stay within the parent width. 我已经确定了数字1,2,3和4,但我不知道如何让它们缩小以保持在父宽度内。 Any help would be much appreciated. 任何帮助将非常感激。

Example HTML: 示例HTML:

<div class="container">
    <div class="tab"><span>Bookmark</span></div><div class="tab"><span>Finish</span></div><div class="tab"><span>A</span></div><div class="tab"><span>Kala kazoo A Roosky Fantastic</span></div>  
</div>

Example CSS: CSS示例:

.container {
    border: 1px solid blue;
    white-space: nowrap;
}

.tab {
    display: inline-block;
    border: 1px solid red;
}

.tab span {
    display: inline-block;
    width: 100%;
    min-width: 20px;
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

JSFiddle: http://jsfiddle.net/ug3en/ JSFiddle: http//jsfiddle.net/ug3en/

Target browsers: IE9+, modern Chrome, modern Firefox, and modern Safari. 目标浏览器:IE9 +,现代Chrome,现代Firefox和现代Safari。

If you add max-width:25% to the .tab class, it will ensure that the tabs don't overflow. 如果将max-width:25%添加到.tab类,它将确保选项卡不会溢出。

That still doesn't fill out the main bar, though, so I'd suggest you use a media query to target small widths: in small mode remove the .tab span { max-width: 100px } and allow each tab-span to fill out 1/4 of the container. 但是,仍然没有填写主栏,所以我建议你使用媒体查询来定位小宽度:在小模式下删除.tab span { max-width: 100px }并允许每个tab-span到填写容器的1/4。

Try this fiddle . 试试这个小提琴

Try below CSS : 试试下面的CSS

.container {
    border: 1px solid blue;
    white-space: nowrap;
    width:auto;
    position:absolute;
}

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

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