简体   繁体   English

Div溢出时Div最大宽度交替

[英]Div Max Width Alternate When Div Overflow

I am trying to limit the <div> max-width (without width ) ( max-width somewhere beyond screen length) when div gets a horizontal scrollbar (scrollbar when zoom or lengthy text in div). 当div获取水平滚动条(当div中的缩放或冗长的文本时,滚动条)时,我试图限制<div> max-width (无width )( max-width超出屏幕长度)。

It is not possible but, what exactly can work for me, is browser to treat div overflow as it is inside screen but "just add a horizontal scrollbar" then i can use 'max-width' (without width ) (somewhere outer screen length). 这是不可能的,但是,对我而言到底能起作用的是,浏览器将div溢出视为是在屏幕内部,但是“仅添加水平滚动条”,然后我可以使用“ max-width”(不使用width )(外部屏幕的长度) )。

I thought using only max-width (without width ) because many times there is very little amount of text in my div then use of width will make it very bad view and extra white space and always a scrollbar. 我认为只使用max-width (不使用width ),因为很多时候div中的文本数量很少,然后使用width会使它的显示效果变差,并留有多余的空白,并且始终带有滚动条。 And max-width (without width )(somewhere outside screen) is giving line-breaks inside screen where it ends so no horizontal scrollbar appearing and no. max-width (无width )(屏幕外的某个位置)在屏幕内的换行处终止,因此没有水平滚动条出现,也没有。 of text lines increasing than it should normally. 文本行数比正常情况下增加。 But i need a horizontal scrollbar (to keep natural height based on number of lines) + a max-width (not to make all text single line also when overflow) . 但是我需要一个水平滚动条(以保持基于行数的自然高度)+ max-width (不要在溢出时也使所有文本变成一行)。

I thought about white-space:nowrap but using this lines are not breaking at max-width all text will be single line. 我考虑过white-space:nowrap但是使用此行不会在max-width处中断,所有文本都是单行。

I am still learning, maybe if i combine some css to above css, or any other css to get the desired result... 我还在学习,也许如果我将一些CSS结合到以上CSS或任何其他CSS上以获得期望的结果...


HTML: HTML:

<div class='a'>
<div class='b'>
sometimes text is lengthy sometimes short
</div>
</div>

CSS: (not working for me) CSS :(不适用于我)

.a{ overflow-x:auto; }
.b{ max-width:1000px; }

http://jsfiddle.net/xm9yzvxz/ (max-width)(not adding horizontal scrollbar and changing height when zoom-in zoom-out, big problem for my case) http://jsfiddle.net/xm9yzvxz/ (最大宽度)(放大时不添加水平滚动条和更改高度,这对我来说是个大问题)

http://jsfiddle.net/6xfw6fh2/ (width)(adding horizontal scrollbar and constant no. of lines when zoom-in zoom-out (good), but whenever div have small amount of text, it will be problem for my case like white-space) http://jsfiddle.net/6xfw6fh2/ (宽度)(在放大缩小时添加水平滚动条和恒定的行数(很好),但是每当div文本较少时,对于我的情况来说就是问题像空白)

I believe this is what you need, You can give max-width 100%, it will take 100% of parent. 我相信这是您所需要的,您可以给max-width 100%,它将占用父级的100%。

 .b { max-width: 100%; word-wrap: break-word; } 
 <h1> Large text:</h1> <div class='b'> sometimes text is lengthy sometimes shortlwkerjweklrweklnfklwenflkwenflkwenflkwenfklwenflkewnflkwenflkwenfklwenflweknflkwenfwelkfnewlkfnwelkfnlkewfnlkwefnlwekfnlewknfklwenfklewfnklewfnlkewnflkewfnlkewnflkewfnlkwefnlkewnfklwefnlkwenflwenflkewnflwe </div> <h1> Less text:</h1> <div class='b'> sometimes text is lengthy sometimes </div> 

If you want to restrict vertical height (to avoid extra line break white space) add a min-height parameter to the a class. 如果你想限制垂直高度(以避免额外的行打破空白)一个最小高度参数添加到a类。 Use em for the height to make it relative to the font size - 2em appears to show one row of text (with the default padding and margins), which should be your minimum for a short string. 使用em表示高度,以使其相对于字体大小2em似乎显示一行文本(具有默认的填充和边距),这应该是短字符串的最小值。

CSS: CSS:

   .a{
       overflow:auto;
       min-height:2em;
       max-height:5em;
   }
   .b {
       width: 1000px;
   }

HTML: HTML:

<div class='a'>
    <div class='b'>
        sometimes text is lengthy sometimes short but in this case it is very long... blah blah blah. sometimes text is lengthy sometimes short but in this case it is very long... blah blah blah. sometimes text is lengthy sometimes short but in this case it is very long... blah blah blah. sometimes text is lengthy sometimes short but in this case it is very long... blah blah blah. sometimes text is lengthy sometimes short but in this case it is very long... blah blah blah. sometimes text is lengthy sometimes short but in this case it is very long... blah blah blah. sometimes text is lengthy sometimes short but in this case it is very long... blah blah blah. sometimes text is lengthy sometimes short but in this case it is very long... blah blah blah. sometimes text is lengthy sometimes short but in this case it is very long... blah blah blah. sometimes text is lengthy sometimes short but in this case it is very long... blah blah blah.
    </div>
</div>

Although your requirement in not clear still. 虽然您的要求还不清楚。 If you want all text in single line without white spaces then try this css. 如果您希望所有文本都在一行中且没有空格,请尝试使用此CSS。 Thanks. 谢谢。

CSS 的CSS

.a {
   overflow:auto;
   min-height:2em;
   max-height:5em;
}
.b {
   max-width: 1000px;
   white-space:nowrap;
}

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

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