简体   繁体   English

在包装器div中div,最大宽度彼此相邻

[英]to divs in a wrapper div with max width next to each other

I've got a div that wrappes two other divs. 我有一个div,它包含了另外两个div。 One of those contains an image and the other some text. 其中一个包含图像,另一个包含一些文本。 The one with the image should be shown on the left hand side of the div containing the text. 带图像的那个应该显示在包含文本的div的左侧。

the wrapper div got a max width. 包装器div有一个最大宽度。 If this one is reached, the text should start to wrap inside of the text div. 如果到达此文本,文本应该开始包装在文本div内。

No matter what I tried (float, flex, inline-block) I could not manage to get a result that works. 无论我尝试了什么(float,flex,inline-block),我都无法获得有效的结果。 I could make it work until the text starts to wrap. 我可以让它工作,直到文本开始包装。 but then the two divs where all the sudden under each other again. 但随后两个div再次突然在彼此之下。

If someone could give me a hand here, I would be very glad. 如果有人能帮助我,我会很高兴。

HTML HTML

<div  class="toast" >
<div class="toastImg"><img style="float:left;margin-left:8px;width:24px" src="http://www.gravatar.com/avatar/09f4f4e7486e3a25f4c4883258fd32d6/?default=&amp;s=80"></div>
<div class="toastText">blablalsadosaoadblablalsadosaojdoad</div>
</div>

CSS CSS

.toast {
    width:auto;
    max-width:300px;
    height:auto;
    background-color: #383838;
    color: #F0F0F0;
}
.toastImg{
  float:left;
  width:10%;
}
.toastText{
    width:90%;
}

http://jsfiddle.net/egxtU/582/ http://jsfiddle.net/egxtU/582/

You can do this with Flexbox and word-break: break-all . 您可以使用Flexboxword-break: break-all执行此操作 word-break: break-all Here is Fiddle . 这是Fiddle Or instead of flexbox you can use display: table DEMO 或者代替flexbox,你可以使用display: table DEMO

 .toast { max-width:300px; background-color: #383838; color: #F0F0F0; display: flex; } .toastText { word-break: break-all; } 
 <div class="toast" > <div class="toastImg"><img style="float:left;margin-left:8px;width:24px" src="http://www.gravatar.com/avatar/09f4f4e7486e3a25f4c4883258fd32d6/?default=&amp;s=80"></div> <div class="toastText">blablalsadosaoadblablalsadosaojdoad</div> </div> 

.toast {
  display:flex;
}

the contained divs won't wrap unless you add "flex-wrap:wrap;" 除非你添加“flex-wrap:wrap;”,否则包含的div将不会换行 to .toast 敬酒

.toastImg{
  float:left; <-- delete this
}

Solution without flexbox (in case of IE); 没有flexbox的解决方案(如果是IE); https://jsfiddle.net/4mqazcyw/ https://jsfiddle.net/4mqazcyw/

CSS: CSS:

.toast {
    width:auto;
    max-width:300px;
    min-height: 24px;
    height:auto;
    background-color: #383838;
    color: #F0F0F0;
 }
.toastImg{  
   width:24px;
   float:left;
}
.toastText{
     width:270px;
     margin-left:30px;
     position:relative;  
}

HTML: HTML:

<div  class="toast" >
<div class="toastImg"><img style="width:24px"src="http://www.gravatar.com/avatar/09f4f4e7486e3a25f4c4883258fd32d6/?default=&amp;s=80"></div>
<div class="toastText">blablalsadosaoadblablalsadosaojdoad lsadosaojdoad lsadosaojdoad lsadosaojdoad</div>
</div>

Something like this? 像这样的东西?

HTML HTML

<div  class="toast" >
<div class="toastImg"><img style="float:left;margin-left:8px;width:24px" src="http://www.gravatar.com/avatar/09f4f4e7486e3a25f4c4883258fd32d6/?default=&amp;s=80"></div>
<div class="toastText">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce et eros turpis. Sed pharetra in tellus a rhoncus. Suspendisse in vulputate neque, eget egestas tellus. Nullam eleifend quis purus ac consequat. Vivamus quis ligula maximus dolor porttitor bibendum. Maecenas ornare pulvinar eros porta semper. Morbi a ante a enim auctor accumsan et eget quam. Pellentesque ac sagittis diam. Vestibulum volutpat quam nibh, et porttitor velit convallis quis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin semper nunc sit amet varius pellentesque. In nunc eros, consequat et ornare et, bibendum commodo ex. Phasellus quis turpis ut ante facilisis molestie porta sed arcu.</div>
</div>

CSS CSS

.toast {
    width:auto;
    max-width:300px;
    height:auto;
    background-color: #383838;
    color: #F0F0F0;
    border:solid black 3px;
    overflow:hidden;
}
.toastImg{
  float:left;
  width:30px;
  border:solid blue 3px;
}
.toastText{
    border: red solid 3px;
    overflow:hidden;
}

https://jsfiddle.net/vf02qojk/ https://jsfiddle.net/vf02qojk/

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

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