简体   繁体   English

强制父div扩大本身就是子div溢出

[英]Force parent div to enlarge itself is child div has overflow

I have this structure: 我有这个结构:

 .out { display: inline-block; border: 2px solid green; } .in { display: inline-block; max-width: 300px; border: 2px solid red; } 
 <div class="out"> <div class="in"> Sample text </div> </div> 

When div.in has overflow (for example, its content is SampletextSampletextSampletextSampletextSampletextSampletextSampletext ), it looks and behaves like this: div.in溢出时(例如,其内容为SampletextSampletextSampletextSampletextSampletextSampletextSampletext ),它的外观和行为如下:

 .out { display: inline-block; border: 2px solid green; } .in { display: inline-block; max-width: 300px; border: 2px solid red; overflow: visible; } 
 <div class="out"> <div class="in"> SampletextSampletextSampletextSampletextSampletextSampletextSampletext </div> </div> 

But I want it to look like this: 但我希望它看起来像这样:

我想要的是

How can I do this? 我怎样才能做到这一点?

It seems the div.out is expanding as it suppose to be. 似乎div.out正在扩大,正如它所设想的那样。 But the main issue is text overflowing from div.in . 但主要问题是来自div.in文本溢出。

You can either use word-wrap:break-word or overflow . 您可以使用word-wrap:break-wordoverflow

Below is a demo with word-wrap 下面是一个带有word-wrap的演示

.in {
  display: inline-block;
  max-width: 300px;
  border: 2px solid red;
  word-wrap: break-word;
}

JSFIDDLE 的jsfiddle

Do you require max-width:300px; 你需要最大宽度:300px; because if you remove that then your .in & .out div both will have equal widths irrespective of the content. 因为如果删除它,那么你的.in和.out div将具有相同的宽度,而不管内容如何。

You can also set width:100% for the .out div. 您还可以为.out div设置宽度:100%。

In your case:- 在你的情况下: -

    .out {
      display: inline-block;
      border: 2px solid green;
      width:100%;
    }

   .in {
     display: inline-block;
     max-width: 300px;
     border: 2px solid red;
     overflow: visible;
   }


   <div class="out">
     <div class="in">
       SampletextSampletextSampletextSampletextSampletextSampletextSampletext
     </div>
   </div>

i see that you got answers how to keep the text inside the 300px width of the .in div. 我看到你得到了如何将文本保持在.in div的300px宽度内的答案。 but i think you want the text to overflow outside the .in div and the .out div to wrap that text. 但我认为你希望文本溢出.in div和.out div以包装该文本。

so here is a solution to that : 所以这是一个解决方案:

 var text = $(".in").html() var newtext = $(".in").html("<span class='textwidth'>" + text + "</span>") var widthText = $(".textwidth").width() $(".out").width(widthText) 
 .out { display: inline-block; border: 2px solid green; } .in { display: block; max-width: 300px; border: 2px solid red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="out"> <div class="in"> SampletextSampletextSampletextSampletextSampletextSampletextSampletext </div> </div> 

A. first i got the html of the .in div using the html() function ( info here ) . 答:首先我得到了html中的.in使用div html()函数信息在这里 )。 this gave the result of SampletextSampletextSampletextSampletextSampletextSampletextSampletext 这给出了SampletextSampletextSampletextSampletextSampletextSampletextSampletext的结果

B. second, i changed the html found at point A by wrapping it withing a span so i can calculate its width B.第二,我通过用一个span包装来改变在A点找到的html ,这样我就可以计算它的width

C. third, in a variable i stocked the width of the new html element, the span which exceeds the width of the .in div C.第三,在变量i放养width新的html元件,所述span超过了width的的.in DIV

D. lastly i gave the span width to the .out element D.最后我给了.out元素的span宽度

so this solution works regardless of how much text you put inside .in div. 所以这个解决方案的工作,无论你多么文本放到里面.in格。 the .out div will always wrap around that text .out div将始终环绕该文本

let me know if it helps 让我知道它是否有帮助

Use the below css and it will work 使用下面的CSS,它会工作

    .out {
        display: inline-block;
        display: inline-block;
        border: 2px solid green;
        width:100px;
        height:100px;
        overflow:auto;
    }

    .in {
      display: inline-block;
      max-width: 20px;
      max-height:20px;
      border: 2px solid red;

    }

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

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