简体   繁体   English

防止在CSS Flexbox中包装

[英]Prevent wrapping in CSS Flexbox

I have a re-sizable sidebar using Javascript. 我有一个可调整大小的侧边栏,使用Javascript。 The problem is when the size of the sidebar is less than the content's wrapping occurs. 问题是当边栏的大小小于内容的换行时。 (i think because of flex-box). (我认为是由于flex-box)。

My grid is based on Rows and Columns kinda like Bootstrap. 我的网格基于行和列,有点像Bootstrap。

.column {
    flex-basis: 0;
    max-width: 100%;
    flex-grow: 1;
}

.row {
    display: flex;
    flex-wrap: no-wrap;
    margin: 0 -1rem;
}

What I want to achieve is truncating the content (without shrinking) indicating that there is more. 我要实现的是截断内容(不缩小)以指示还有更多内容。

Here is a fiddle with what i achieved so far. 这里是一个小提琴与我取得了这么远。

When it comes to Flexbox, you need to set min-width: 0 or overflow: hidden to every level, down from the child of the element with the width constraint, to the one having ellipsis. 当涉及到Flexbox时,您需要为每个级别设置min-width: 0overflow: hidden ,从具有宽度限制的元素的子元素到具有省略号的元素的子元素。

So in this case, from the sidebar 's child, and the reason is that a flex item's min-width defaults to auto , which mean it can't be smaller than its content. 因此,在这种情况下,从sidebar的子项开始,原因是flex项的min-width默认为auto ,这意味着它不能小于其内容。

To make it easy to follow how I mean, I created a flex-ellipsis class and added it to all elements down that chain. 为了易于理解我的意思,我创建了一个flex-ellipsis类并将其添加到该链中的所有元素。

Note, I also removed the negative margin, margin: 0 -1rem; 注意,我还删除了负边距, margin: 0 -1rem; , from the row class, or else one can't see the ellipsis. row类中的内容,否则看不到省略号。

Updated fiddle 更新的小提琴

Stack snippet 堆栈片段

 const resizeHandle = document.getElementsByClassName("vertical-resize")[0]; const navbar = document.getElementById('sidebar'); function resizeNavbar(e) { let size = (e.pageX + 5); navbar.style.width = (e.pageX + 5) + "px"; } function removeEvents() { document.removeEventListener('mousemove', resizeNavbar); document.removeEventListener('mouseup', resizeNavbar); } resizeHandle.addEventListener('mousedown', () => { document.addEventListener('mousemove', resizeNavbar); document.addEventListener('mouseup', removeEvents); }); 
 * { user-select: none; color: #dadada; } .flex-ellipsis { min-width: 0; /* added */ /* or overflow: hidden; */ } p, h6 { text-overflow: ellipsis; white-space: nowrap; overflow: hidden; } .rounded-circle { border-radius: 50% !important; } .no-margin { margin: 0 !important; } .column { flex-basis: 0; max-width: 100%; flex-grow: 1; background: #4a4a4a; } .column-1 { flex: 0 0 8.33333%; max-width: 8.33333%; } .row { display: flex; flex-wrap: nowrap; /*Even though no-wrap it still wraps as shown in picture.*/ /*margin: 0 -1rem;*/ } .custom { margin: auto; padding: 10px 0px; flex-wrap: nowrap; } .vertical-resize { position: relative; right: 0; top: 0; cursor: col-resize; width: 5px; background: aquamarine; height: 100vh; } 
 <div class="row"> <div id="sidebar" style="width: 570px;"> <div class="align-items-center row custom flex-ellipsis" id="sidebar"> <div style="max-width: 2.5rem;" class="column-1"> <img class="rounded-circle" style="max-width: 28px; min-width: 28px;" alt="Avatar" src="https://d30y9cdsu7xlg0.cloudfront.net/png/138926-200.png"> </div> <div class="column flex-ellipsis"> <div class="row"> <div class="column"> <p class="no-margin">Ahmed Tarek</p> </div> </div> <div class="row flex-ellipsis"> <div class="column flex-ellipsis"> <h6 style="font-size: 11px;" class="no-margin">01 January, 0001</h6> </div> </div> </div> </div> </div> <div class="vertical-resize"></div> <div class="column"> </div> </div> 

You need to use text-overflow property along with other properties inside a media query or per your needs. 您需要在媒体查询中或根据需要使用text-overflow属性以及其他属性。

eg 例如

 .list { width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 48px; /* just for test */ } 
 <div class="list"> this is a ver long list this is a ver long list this is a ver long list. </div> 

Hope this will help you out. 希望这对您有所帮助。

As you have not shared full code with us, it will be difficult for us to provide you working solution. 由于您尚未与我们共享完整的代码,因此我们很难为您提供有效的解决方案。 But you can add this css to your code with you want to truncate. 但是您可以通过截断将此CSS添加到代码中。

 width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 

This will help you with text truncate with "..." 这将帮助您用“ ...”截断文本

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

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